Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Nov 9, 2023
1 parent cac0f18 commit 2e0c6da
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/gas-price/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './gas-price';
28 changes: 20 additions & 8 deletions src/update-feeds/update-feeds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as utilsModule from '../utils';

import * as dapiDataRegistryModule from './dapi-data-registry';
import * as updateFeedsModule from './update-feeds';
import * as updateTransactionModule from './update-transactions';

jest.mock('../state');

Expand Down Expand Up @@ -145,19 +146,19 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {

it('fetches other batches in a staggered way and logs errors', async () => {
// Prepare the mocked contract so it returns three batches (of size 1) of dAPIs and the second batch fails to load.
const firstBatch = generateReadDapiWithIndexResponse();
const thirdBatch = generateReadDapiWithIndexResponse();
const firstDapi = generateReadDapiWithIndexResponse();
const thirdDapi = generateReadDapiWithIndexResponse();
const dapiDataRegistry = generateMockDapiDataRegistry();
jest
.spyOn(dapiDataRegistryModule, 'getDapiDataRegistry')
.mockReturnValue(dapiDataRegistry as unknown as DapiDataRegistry);
dapiDataRegistry.interface.decodeFunctionResult.mockImplementation((_fn, value) => value);
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({
successes: [true, true],
returndata: [[ethers.BigNumber.from(3)], firstBatch],
returndata: [[ethers.BigNumber.from(3)], firstDapi],
});
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [false], returndata: [] });
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true], returndata: [thirdBatch] });
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true], returndata: [thirdDapi] });
const sleepCalls = [] as number[];
const originalSleep = utilsModule.sleep;
jest.spyOn(utilsModule, 'sleep').mockImplementation(async (ms) => {
Expand All @@ -174,7 +175,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
signedApiUrlStore: { '31337': { 'some-test-provider': ['url-one'] } },
signedApiStore: {},
gasPriceStore: {
'123': {
'31337': {
'some-test-provider': {
gasPrices: [],
sponsorLastUpdateTimestampMs: {
Expand All @@ -185,7 +186,13 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
},
})
);
jest.spyOn(updateFeedsModule, 'getFeedsToUpdate').mockImplementation(() => []);
jest
.spyOn(updateFeedsModule, 'getFeedsToUpdate')
.mockImplementation(() => [
allowPartial<updateTransactionModule.UpdateableDapi>({ dapiInfo: firstDapi }),
allowPartial<updateTransactionModule.UpdateableDapi>({ dapiInfo: thirdDapi }),
]);
jest.spyOn(updateTransactionModule, 'updateFeeds').mockResolvedValue([null, null]);

await updateFeedsModule.runUpdateFeed(
'provider-name',
Expand All @@ -197,7 +204,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
DapiDataRegistry: '0xDD78254f864F97f65e2d86541BdaEf88A504D2B2',
},
}),
'123'
'31337'
);

// Expect the contract to fetch the batches to be called with the correct stagger time.
Expand All @@ -212,7 +219,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
'Failed to get active dAPIs batch',
new Error('One of the multicalls failed')
);
expect(logger.debug).toHaveBeenCalledTimes(6);
expect(logger.debug).toHaveBeenCalledTimes(7);
expect(logger.debug).toHaveBeenNthCalledWith(1, 'Fetching first batch of dAPIs batches');
expect(logger.debug).toHaveBeenNthCalledWith(2, 'Processing batch of active dAPIs', expect.anything());
expect(logger.debug).toHaveBeenNthCalledWith(3, 'Fetching batches of active dAPIs', {
Expand All @@ -226,5 +233,10 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
batchIndex: 2,
});
expect(logger.debug).toHaveBeenNthCalledWith(6, 'Processing batch of active dAPIs', expect.anything());
expect(logger.debug).toHaveBeenNthCalledWith(7, 'Finished processing batches of active dAPIs', {
batchesCount: 3,
errorCount: 4,
successCount: 0,
});
});
});
2 changes: 1 addition & 1 deletion src/update-feeds/update-feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { range, size, zip } from 'lodash';
import { calculateMedian, checkUpdateConditions } from '../condition-check';
import type { Chain } from '../config/schema';
import { INT224_MAX, INT224_MIN } from '../constants';
import { clearSponsorLastUpdateTimestampMs } from '../gas-price/gas-price';
import { clearSponsorLastUpdateTimestampMs } from '../gas-price';
import { logger } from '../logger';
import { getStoreDataPoint } from '../signed-data-store';
import { getState, updateState } from '../state';
Expand Down
2 changes: 1 addition & 1 deletion src/update-feeds/update-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Api3ServerV1 } from '@api3/airnode-protocol-v1';
import { go } from '@api3/promise-utils';
import { ethers } from 'ethers';

import { getAirseekerRecommendedGasPrice } from '../gas-price/gas-price';
import { getAirseekerRecommendedGasPrice } from '../gas-price';
import { logger } from '../logger';
import { getState, updateState } from '../state';
import type { SignedData, ChainId, ProviderName } from '../types';
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/gas-price.feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
multiplyGasPrice,
initializeGasStore,
clearExpiredStoreGasPrices,
} from '../../src/gas-price/gas-price';
} from '../../src/gas-price';
import { getState, updateState } from '../../src/state';
import { init } from '../fixtures/mock-config';

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/update-feeds.feature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers';
import { omit } from 'lodash';

import { initializeGasStore } from '../../src/gas-price/gas-price';
import { initializeGasStore } from '../../src/gas-price';
import { logger } from '../../src/logger';
import * as stateModule from '../../src/state';
import { runUpdateFeed } from '../../src/update-feeds';
Expand Down

0 comments on commit 2e0c6da

Please sign in to comment.