Skip to content

Commit

Permalink
Fix issue with flaky candle test (#2430)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfraser authored Oct 2, 2024
1 parent 1085e59 commit 672169b
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions indexer/services/ender/__tests__/lib/candles-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,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,
Expand All @@ -502,39 +508,39 @@ describe('candleHelper', () => {

await OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '10005');

// 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(
Expand Down Expand Up @@ -576,11 +582,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,
Expand Down Expand Up @@ -611,8 +622,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,
Expand Down

0 comments on commit 672169b

Please sign in to comment.