Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Aug 6, 2024
1 parent 2406df3 commit 8a7c91b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('UnfinalizedBlocksService', () => {
});

afterEach(() => {
(unfinalizedBlocksService as unknown as any).unfinalizedBlocks = {};
(unfinalizedBlocksService as unknown as any)._unfinalizedBlocks = {};
});

it('can set finalized block', () => {
Expand Down
14 changes: 5 additions & 9 deletions packages/node/src/indexer/api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ jest.mock('@polkadot/api', () => {
consts: jest.fn(),
disconnect: jest.fn(),
}));
return { ApiPromise, WsProvider: jest.fn() };
return { ApiPromise, WsProvider: jest.fn(() => ({ send: jest.fn() })) };
});

const testNetwork = {
endpoint: { 'ws://kusama.api.onfinality.io/public-ws': {} },
endpoint: ['ws://kusama.api.onfinality.io/public-ws'],
types: {
TestType: 'u32',
},
Expand Down Expand Up @@ -108,13 +108,9 @@ describe('ApiService', () => {
nodeConfig,
);
const { version } = require('../../package.json');
expect(WsProvider).toHaveBeenCalledWith(
Object.keys(testNetwork.endpoint)[0],
2500,
{
'User-Agent': `SubQuery-Node ${version}`,
},
);
expect(WsProvider).toHaveBeenCalledWith(testNetwork.endpoint[0], 2500, {
'User-Agent': `SubQuery-Node ${version}`,
});
expect(createSpy).toHaveBeenCalledWith({
provider: expect.anything(),
throwOnConnect: expect.anything(),
Expand Down
27 changes: 21 additions & 6 deletions packages/node/src/indexer/project.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import {
ProjectUpgradeService,
upgradableSubqueryProject,
DsProcessorService,
DynamicDsService
DynamicDsService,
} from '@subql/node-core';
import { SubstrateDatasourceKind, SubstrateHandlerKind } from '@subql/types';
import { GraphQLSchema } from 'graphql';
import { BlockchainService } from '../blockchain.service';
import { SubqueryProject } from '../configure/SubqueryProject';
import { ApiService } from './api.service';
import { RuntimeService } from './runtime/runtimeService';

function testSubqueryProject(): SubqueryProject {
return {
Expand Down Expand Up @@ -140,7 +141,11 @@ describe('ProjectService', () => {
},
{
provide: ProjectService,
useFactory: (apiService: ApiService, project: SubqueryProject, blockchainService: BlockchainService) =>
useFactory: (
apiService: ApiService,
project: SubqueryProject,
blockchainService: BlockchainService,
) =>
new TestProjectService(
{
validateProjectCustomDatasources: jest.fn(),
Expand All @@ -166,24 +171,34 @@ describe('ProjectService', () => {
} as unknown as DynamicDsService,
null as unknown as any,
null as unknown as any,
blockchainService
blockchainService,
),
inject: ['APIService', 'ISubqueryProject', BlockchainService,],
inject: ['APIService', 'ISubqueryProject', 'IBlockchainService'],
},
EventEmitter2,
{
provide: 'APIService',
useFactory: ApiService.create,
inject: ['ISubqueryProject', ConnectionPoolService, EventEmitter2, NodeConfig]
inject: [
'ISubqueryProject',
ConnectionPoolService,
EventEmitter2,
NodeConfig,
],
},
{
provide: ProjectUpgradeService,
useValue: projectUpgrade,
},
{
provide: 'RuntimeService',
useFactory: (apiService) => new RuntimeService(apiService),
inject: ['APIService'],
},
{
provide: 'IBlockchainService',
useClass: BlockchainService,
}
},
],
imports: [EventEmitterModule.forRoot()],
}).compile();
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/indexer/x-provider/cachedProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { LRUCache } from 'lru-cache';
const MAX_CACHE_SIZE = 200;
const CACHE_TTL = 60 * 1000;

export function createCachedProvider(
provider: ProviderInterface,
): ProviderInterface {
export function createCachedProvider<
P extends ProviderInterface = ProviderInterface,
>(provider: P): P {
const cacheMap = new LRUCache<string, Promise<any>>({
max: MAX_CACHE_SIZE,
ttl: CACHE_TTL,
Expand Down
44 changes: 19 additions & 25 deletions packages/node/src/indexer/x-provider/x-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,60 @@ describe('ApiPromiseConnection', () => {
let httpProvider: HttpProvider;

beforeEach(async () => {
wsProvider = await new WsProvider(
'wss://kusama.api.onfinality.io/public-ws',
).isReady;
httpProvider = new HttpProvider('https://kusama.api.onfinality.io/public');
const ws = await new WsProvider('wss://kusama.api.onfinality.io/public-ws')
.isReady;
const http = new HttpProvider('https://kusama.api.onfinality.io/public');

jest.spyOn(wsProvider, 'send');
jest.spyOn(httpProvider, 'send');
wsProvider = createCachedProvider(ws);
httpProvider = createCachedProvider(http);

// TODO these don't work any more because the send method gets replaced
jest.spyOn(ws, 'send');
jest.spyOn(http, 'send');
});

afterEach(async () => {
await Promise.all([wsProvider?.disconnect(), httpProvider?.disconnect()]);
});

it('should not make duplicate requests for state_getRuntimeVersion on wsProvider', async () => {
const cachedProvider = createCachedProvider(wsProvider);

await Promise.all([
cachedProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
cachedProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
wsProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
wsProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
]);

expect(wsProvider.send).toHaveBeenCalledTimes(1);
});

it('should not make duplicate requests for chain_getHeader on wsProvider', async () => {
const cachedProvider = createCachedProvider(wsProvider);
await Promise.all([
cachedProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
cachedProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
wsProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
wsProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
]);
expect(wsProvider.send).toHaveBeenCalledTimes(1);
});

it('should not make duplicate requests for state_getRuntimeVersion on httpProvider', async () => {
const cachedProvider = createCachedProvider(httpProvider);

await Promise.all([
cachedProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
cachedProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
httpProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
httpProvider.send('state_getRuntimeVersion', [TEST_BLOCKHASH]),
]);
expect(httpProvider.send).toHaveBeenCalledTimes(1);
});

it('should not make duplicate requests for chain_getHeader on httpProvider', async () => {
const cachedProvider = createCachedProvider(httpProvider);

await Promise.all([
cachedProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
cachedProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
httpProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
httpProvider.send('chain_getHeader', [TEST_BLOCKHASH]),
]);
expect(httpProvider.send).toHaveBeenCalledTimes(1);
});

it('should not cache requests if there are no args', async () => {
const cachedProvider = createCachedProvider(httpProvider);

const result1 = await cachedProvider.send('chain_getHeader', []);
const result1 = await httpProvider.send('chain_getHeader', []);
// Enough time for a new block
await delay(7);
const result2 = await cachedProvider.send('chain_getHeader', []);
const result2 = await httpProvider.send('chain_getHeader', []);

expect(httpProvider.send).toHaveBeenCalledTimes(2);
expect(result1).not.toEqual(result2);
Expand Down

0 comments on commit 8a7c91b

Please sign in to comment.