Skip to content

Commit

Permalink
Fix test runner not setting last processed height (#2569)
Browse files Browse the repository at this point in the history
* Fix test runner not setting last processed height

* Update changelog

* Fix comment

* Fix bypass blocks test
  • Loading branch information
stwiname authored Oct 14, 2024
1 parent 8c388df commit 9b7ded4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Issues with setting a large block range for bypass blocks (#2566)
- Test runner not setting lastProcessedHeight leading to data not being flushed (#2569)

## [14.1.5] - 2024-09-25
### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/node-core/src/indexer/fetch.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ describe('Fetch Service', () => {

await fetchService.init(1);

expect((fetchService as any).getDatasourceBypassBlocks()).toEqual([`301-500`]);
expect((fetchService as any).getDatasourceBypassBlocks()).toEqual([`301-499`]);
});

it('checks chain heads at an interval', async () => {
Expand Down
17 changes: 7 additions & 10 deletions packages/node-core/src/indexer/storeCache/cacheModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CachedModel<
T extends {id: string; __block_range?: (number | null)[] | Fn} = {
id: string;
__block_range?: (number | null)[] | Fn;
},
}
>
extends Cacheable
implements ICachedModel<T>, ICachedModelControl
Expand Down Expand Up @@ -352,16 +352,13 @@ export class CachedModel<
}

private filterRemoveRecordByHeight(blockHeight: number, lessEqt: boolean): Record<string, RemoveValue> {
return Object.entries(this.removeCache).reduce(
(acc, [key, value]) => {
if (lessEqt ? value.removedAtBlock <= blockHeight : value.removedAtBlock > blockHeight) {
acc[key] = value;
}
return Object.entries(this.removeCache).reduce((acc, [key, value]) => {
if (lessEqt ? value.removedAtBlock <= blockHeight : value.removedAtBlock > blockHeight) {
acc[key] = value;
}

return acc;
},
{} as Record<string, RemoveValue>
);
return acc;
}, {} as Record<string, RemoveValue>);
}

private filterRecordsWithHeight(blockHeight: number): FilteredHeightRecords<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export class StoreCacheService extends BaseCacheService {
try {
// Get the block height of all data we want to flush up to
const blockHeight = await this.metadata.find('lastProcessedHeight');
assert(blockHeight !== undefined, 'Cannot flush cache without lastProcessedHeight');
// Get models that have data to flush
const updatableModels = Object.values(this.cachedModels).filter((m) => m.isFlushable);
if (this._useCockroachDb) {
Expand Down
2 changes: 1 addition & 1 deletion packages/node-core/src/indexer/storeCache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ICachedModelControl {
isFlushable: boolean;
hasAssociations?: boolean;
flushableRecordCounter: number;
flush(tx: Transaction, blockHeight?: number): Promise<void>;
flush(tx: Transaction, blockHeight: number): Promise<void>;
flushOperation?(i: number, tx: Transaction): Promise<void>;
/**
*
Expand Down
13 changes: 9 additions & 4 deletions packages/node-core/src/indexer/test.runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {TestRunner} from './test.runner';

jest.mock('@subql/x-sequelize');

const mockStoreCache = {
flushCache: jest.fn().mockResolvedValue(undefined),
metadata: {set: jest.fn()},
};

describe('TestRunner', () => {
let testRunner: TestRunner<any, any, any, any>;
let sequelizeMock: jest.Mocked<Sequelize>;
Expand All @@ -22,7 +27,7 @@ describe('TestRunner', () => {
storeServiceMock = {
setBlockHeight: jest.fn(),
getStore: jest.fn().mockReturnValue({}),
storeCache: {flushCache: jest.fn().mockResolvedValue(undefined)},
storeCache: mockStoreCache,
};

sandboxMock = {
Expand Down Expand Up @@ -83,7 +88,7 @@ describe('TestRunner', () => {
(testRunner as any).storeService = {
getStore: () => storeMock,
setBlockHeight: jest.fn(),
storeCache: {flushCache: jest.fn().mockResolvedValue(undefined)},
storeCache: mockStoreCache,
} as any;

await testRunner.runTest(testMock, sandboxMock, indexBlock);
Expand Down Expand Up @@ -118,7 +123,7 @@ describe('TestRunner', () => {
(testRunner as any).storeService = {
getStore: () => storeMock,
setBlockHeight: jest.fn(),
storeCache: {flushCache: jest.fn().mockResolvedValue(undefined)},
storeCache: mockStoreCache,
} as any;

await testRunner.runTest(testMock, sandboxMock, indexBlock);
Expand Down Expand Up @@ -175,7 +180,7 @@ describe('TestRunner', () => {
(testRunner as any).storeService = {
getStore: () => storeMock,
setBlockHeight: jest.fn(),
storeCache: {flushCache: jest.fn().mockResolvedValue(undefined)},
storeCache: mockStoreCache,
} as any;

await testRunner.runTest(testMock, sandboxMock, indexBlock);
Expand Down
6 changes: 5 additions & 1 deletion packages/node-core/src/indexer/test.runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class TestRunner<A, SA, B, DS> {
const [block] = await this.apiService.fetchBlocks([test.blockHeight]);

this.storeService.setBlockHeight(test.blockHeight);
// Ensure a block height is set so that data is flushed correctly
this.storeService.storeCache.metadata.set('lastProcessedHeight', test.blockHeight - 1);
const store = this.storeService.getStore();
sandbox.freeze(store, 'store');

Expand Down Expand Up @@ -110,7 +112,9 @@ export class TestRunner<A, SA, B, DS> {
return value;
};
failedAttributes.push(
`\t\tattribute: "${attr}":\n\t\t\texpected: "${fmtValue(expectedAttr)}"\n\t\t\tactual: "${fmtValue(actualAttr)}"\n`
`\t\tattribute: "${attr}":\n\t\t\texpected: "${fmtValue(expectedAttr)}"\n\t\t\tactual: "${fmtValue(
actualAttr
)}"\n`
);
}
});
Expand Down

0 comments on commit 9b7ded4

Please sign in to comment.