Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky Candles test #2619

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix flaky test
  • Loading branch information
adamfraser committed Nov 27, 2024
commit 76796967c2b297db38b396d425339c85a422c98d
72 changes: 41 additions & 31 deletions indexer/services/ender/__tests__/lib/candles-generator.test.ts
Original file line number Diff line number Diff line change
@@ -485,10 +485,16 @@ describe('candleHelper', () => {
const usdVolume: string = Big(existingPrice).times(baseTokenVolume).toString();
const orderbookMidPriceClose = '7500';
const orderbookMidPriceOpen = '8000';
// Set candle start time to be far in the past to ensure all candles are new
const startTime: IsoString = helpers.calculateNormalizedCandleStartTime(
testConstants.createdDateTime.minus({ minutes: 100 }),
CandleResolution.ONE_MINUTE,
).toISO();

Comment on lines +488 to +493
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor repeated startTime calculation into a helper function

The calculation of startTime by subtracting 100 minutes is duplicated in multiple tests (lines 488-493 and 591-595). Extracting this logic into a helper function will enhance code maintainability and adhere to the DRY principle.

Here is a suggested refactor:

+function getPastStartTime(minutesAgo: number): IsoString {
+  return helpers.calculateNormalizedCandleStartTime(
+    testConstants.createdDateTime.minus({ minutes: minutesAgo }),
+    CandleResolution.ONE_MINUTE,
+  ).toISO();
+}

Then replace in your tests:

-const startTime: IsoString = helpers.calculateNormalizedCandleStartTime(
-  testConstants.createdDateTime.minus({ minutes: 100 }),
-  CandleResolution.ONE_MINUTE,
-).toISO();

with:

+const startTime: IsoString = getPastStartTime(100);

Committable suggestion skipped: line range outside the PR's diff.

await Promise.all(
_.map(Object.values(CandleResolution), (resolution: CandleResolution) => {
return CandleTable.create({
startedAt: previousStartedAt,
startedAt: startTime,
ticker: testConstants.defaultPerpetualMarket.ticker,
resolution,
low: existingPrice,
@@ -508,40 +514,39 @@ describe('candleHelper', () => {

setCachePrice('BTC-USD', '10005');
await OrderbookMidPriceMemoryCache.updateOrderbookMidPrices();

// Add two trades for BTC-USD market
const publisher: KafkaPublisher = new KafkaPublisher();
publisher.addEvents([
defaultTradeKafkaEvent,
defaultTradeKafkaEvent2,
]);

// Create new candles, with trades
await runUpdateCandles(publisher).then(async () => {

// Verify previous candles have orderbookMidPriceClose updated
const previousExpectedCandles: CandleFromDatabase[] = _.map(
Object.values(CandleResolution),
(resolution: CandleResolution) => {
return {
id: CandleTable.uuid(previousStartedAt, defaultCandle.ticker, resolution),
startedAt: previousStartedAt,
ticker: defaultCandle.ticker,
resolution,
low: existingPrice,
high: existingPrice,
open: existingPrice,
close: existingPrice,
baseTokenVolume,
usdVolume,
trades: existingTrades,
startingOpenInterest,
orderbookMidPriceClose: '10005',
orderbookMidPriceOpen,
};
},
);
await verifyCandlesInPostgres(previousExpectedCandles);
});
await runUpdateCandles(publisher);

// Verify previous candles have orderbookMidPriceClose updated
const previousExpectedCandles: CandleFromDatabase[] = _.map(
Object.values(CandleResolution),
(resolution: CandleResolution) => {
return {
id: CandleTable.uuid(startTime, defaultCandle.ticker, resolution),
startedAt: startTime,
ticker: defaultCandle.ticker,
resolution,
low: existingPrice,
high: existingPrice,
open: existingPrice,
close: existingPrice,
baseTokenVolume,
usdVolume,
trades: existingTrades,
startingOpenInterest,
orderbookMidPriceClose: '10005',
orderbookMidPriceOpen,
};
},
);
await verifyCandlesInPostgres(previousExpectedCandles);

// Verify new candles were created
const expectedCandles: CandleFromDatabase[] = _.map(
@@ -583,11 +588,16 @@ describe('candleHelper', () => {
const usdVolume: string = Big(existingPrice).times(baseTokenVolume).toString();
const orderbookMidPriceClose = '7500';
const orderbookMidPriceOpen = '8000';
// Set candle start time to be far in the past to ensure all candles are new
const startTime: IsoString = helpers.calculateNormalizedCandleStartTime(
testConstants.createdDateTime.minus({ minutes: 100 }),
CandleResolution.ONE_MINUTE,
).toISO();

await Promise.all(
_.map(Object.values(CandleResolution), (resolution: CandleResolution) => {
return CandleTable.create({
startedAt: previousStartedAt,
startedAt: startTime,
ticker: testConstants.defaultPerpetualMarket.ticker,
resolution,
low: existingPrice,
@@ -619,8 +629,8 @@ describe('candleHelper', () => {
Object.values(CandleResolution),
(resolution: CandleResolution) => {
return {
id: CandleTable.uuid(previousStartedAt, defaultCandle.ticker, resolution),
startedAt: previousStartedAt,
id: CandleTable.uuid(startTime, defaultCandle.ticker, resolution),
startedAt: startTime,
ticker: defaultCandle.ticker,
resolution,
low: existingPrice,