Skip to content

Commit

Permalink
Fix flaky Candles test (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfraser authored Nov 27, 2024
1 parent 6ca9b4e commit 35ecc0a
Showing 1 changed file with 41 additions and 31 deletions.
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
Expand Up @@ -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();

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 @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 35ecc0a

Please sign in to comment.