forked from subquery/subql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init monitor service add init test fix bug Update packages/node-core/src/indexer/monitor.service.ts Co-authored-by: Scott Twiname <[email protected]> address comments 1 use cache file stats tidy up address comments worker write improve logs, forceclean reindex and fix tests fix inject Fix block height not being applied to certain historical queries (subquery#2398) * Fix block height not being applied to certain historical queries * Update changelog * Update snapshot [SKIP CI] Prerelease [release] 20240509 (subquery#2400) Move cache clear after tx committed (subquery#2386) * Move cache clear after tx committed * Always flush with a block height and remove flushAll option * Update changelog --------- Co-authored-by: Scott Twiname <[email protected]> [SKIP CI] Prerelease Escape commit message (subquery#2403) fix load chainTypes sandbox missing modules (subquery#2404) * fix load chainTypes sandbox missing modules * export SANDBOX_DEFAULT_BUILTINS [SKIP CI] Prerelease make monitorService optional tidy up fix bug address comments tidy up update default monitorFileSize to 0 Admin api (subquery#2415) * draft * rewind * poi * add tests * add test for stop poi stop sync * Update packages/node-core/src/indexer/blockDispatcher/base-block-dispatcher.ts Co-authored-by: Scott Twiname <[email protected]> * address comment * monitor service add exit * Update packages/node-core/src/process.ts Co-authored-by: Scott Twiname <[email protected]> * address comments * change write * update * improve exit error * fix rewind height issue --------- Co-authored-by: Scott Twiname <[email protected]> set default monitor size to 200 adjust monitor file logic address comments add monitor log for project upgrade, unique monitor file name * tidy up * tidy up * fix tests
- Loading branch information
Showing
70 changed files
with
1,933 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import {HttpException, HttpStatus} from '@nestjs/common'; | ||
import {EventEmitter2} from '@nestjs/event-emitter'; | ||
import {Test, TestingModule} from '@nestjs/testing'; | ||
import {TargetBlockPayload, RewindPayload, AdminEvent} from '../events'; | ||
import {MonitorService, PoiService, ProofOfIndex} from '../indexer'; | ||
import {AdminController, AdminListener} from './admin.controller'; | ||
import {BlockRangeDto} from './blockRange'; | ||
|
||
describe('AdminController', () => { | ||
let adminController: AdminController; | ||
let monitorService: MonitorService; | ||
let poiService: PoiService; | ||
let eventEmitter: EventEmitter2; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AdminController], | ||
providers: [ | ||
{ | ||
provide: MonitorService, | ||
useValue: { | ||
getBlockIndexHistory: jest.fn(), | ||
getForkedRecords: jest.fn(), | ||
getBlockIndexRecords: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: PoiService, | ||
useValue: { | ||
plainPoiRepo: { | ||
getStartAndEndBlock: jest.fn(), | ||
getPoiBlocksByRange: jest.fn(), | ||
}, | ||
PoiToHuman: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: EventEmitter2, | ||
useValue: { | ||
emitAsync: jest.fn(), | ||
once: jest.fn(), | ||
emit: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
adminController = module.get<AdminController>(AdminController); | ||
monitorService = module.get<MonitorService>(MonitorService); | ||
poiService = module.get<PoiService>(PoiService); | ||
eventEmitter = module.get<EventEmitter2>(EventEmitter2); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(adminController).toBeDefined(); | ||
}); | ||
|
||
describe('getIndexBlocks', () => { | ||
it('should return block index history', async () => { | ||
const result = ['block1', 'block2']; | ||
jest.spyOn(monitorService, 'getBlockIndexHistory').mockImplementation(() => result); | ||
|
||
await expect(adminController.getIndexBlocks()).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(monitorService, 'getBlockIndexHistory').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getIndexBlocks()).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getIndexForks', () => { | ||
it('should return forked records', async () => { | ||
const result = ['fork1', 'fork2']; | ||
jest.spyOn(monitorService, 'getForkedRecords').mockImplementation(() => Promise.resolve(result)); | ||
|
||
await expect(adminController.getIndexForks()).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(monitorService, 'getForkedRecords').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getIndexForks()).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getIndexBlockRecord', () => { | ||
it('should return block index records', async () => { | ||
const result = ['record1', 'record2']; | ||
jest.spyOn(monitorService, 'getBlockIndexRecords').mockImplementation(() => Promise.resolve(result)); | ||
|
||
await expect(adminController.getIndexBlockRecord('1')).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(monitorService, 'getBlockIndexRecords').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getIndexBlockRecord('1')).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getPoiRange', () => { | ||
it('should return POI range', async () => { | ||
const result = {startBlock: 1, endBlock: 10}; | ||
jest.spyOn(poiService.plainPoiRepo, 'getStartAndEndBlock').mockImplementation(() => Promise.resolve(result)); | ||
|
||
await expect(adminController.getPoiRange()).resolves.toEqual(result); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
jest.spyOn(poiService.plainPoiRepo, 'getStartAndEndBlock').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.getPoiRange()).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('getPoisByRange', () => { | ||
it('should return POIs by range', async () => { | ||
const pois: ProofOfIndex[] = [ | ||
{ | ||
id: 1, | ||
chainBlockHash: new Uint8Array(), | ||
hash: new Uint8Array(), | ||
parentHash: new Uint8Array(), | ||
operationHashRoot: new Uint8Array(), | ||
}, | ||
]; | ||
const blockRange = new BlockRangeDto(1, 10); | ||
jest.spyOn(poiService.plainPoiRepo, 'getPoiBlocksByRange').mockImplementation(() => Promise.resolve(pois)); | ||
await expect(adminController.getPoisByRange(blockRange)).resolves.toEqual([ | ||
{ | ||
chainBlockHash: '0x', | ||
hash: '0x', | ||
id: 1, | ||
operationHashRoot: '0x', | ||
parentHash: '0x', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const blockRange = new BlockRangeDto(1, 10); | ||
jest.spyOn(poiService.plainPoiRepo, 'getPoiBlocksByRange').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(() => adminController.getPoisByRange(blockRange)).rejects.toThrow(HttpException); | ||
}); | ||
|
||
it('should throw an error if startBlock is greater than endBlock', async () => { | ||
const blockRange = new BlockRangeDto(10, 1); | ||
|
||
await expect(adminController.getPoisByRange(blockRange)).rejects.toThrow(HttpException); | ||
}); | ||
}); | ||
|
||
describe('rewindTarget', () => { | ||
it('should return successful rewind payload', async () => { | ||
const rewindData = {height: 1} as TargetBlockPayload; | ||
const result = {success: true, height: 1} as RewindPayload; | ||
jest.spyOn(eventEmitter, 'emitAsync').mockImplementation(() => Promise.resolve([])); | ||
jest.spyOn(eventEmitter, 'once').mockImplementation((event, callback) => { | ||
callback(result); | ||
return eventEmitter; // Ensure that it returns the eventEmitter instance | ||
}); | ||
await expect(adminController.rewindTarget(rewindData)).resolves.toEqual(result); | ||
}); | ||
|
||
it('should return failure if rewind times out', async () => { | ||
const rewindData = {height: 1} as TargetBlockPayload; | ||
|
||
jest.spyOn(eventEmitter, 'emitAsync').mockImplementation(() => Promise.resolve([])); | ||
jest.spyOn(eventEmitter, 'once').mockImplementation(() => { | ||
throw new Error('timeout'); | ||
}); | ||
|
||
const expectError = new HttpException( | ||
{ | ||
status: HttpStatus.INTERNAL_SERVER_ERROR, | ||
error: 'Rewind failed:', | ||
height: rewindData.height, | ||
success: false, | ||
}, | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
); | ||
await expect(adminController.rewindTarget(rewindData)).rejects.toThrow(expectError); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const rewindData = {height: 1} as TargetBlockPayload; | ||
|
||
jest.spyOn(eventEmitter, 'emitAsync').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
await expect(adminController.rewindTarget(rewindData)).rejects.toThrow( | ||
new HttpException( | ||
{ | ||
status: HttpStatus.INTERNAL_SERVER_ERROR, | ||
error: 'Rewind failed:', | ||
height: rewindData.height, | ||
success: false, | ||
}, | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
) | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('AdminListener', () => { | ||
let adminListener: AdminListener; | ||
let eventEmitter: EventEmitter2; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
AdminListener, | ||
{ | ||
provide: EventEmitter2, | ||
useValue: { | ||
emit: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
adminListener = module.get<AdminListener>(AdminListener); | ||
eventEmitter = module.get<EventEmitter2>(EventEmitter2); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(adminListener).toBeDefined(); | ||
}); | ||
|
||
describe('handleRewindSuccess', () => { | ||
it('should emit RewindTargetResponse event on rewind success', () => { | ||
const payload = {height: 1, success: true} as RewindPayload; | ||
|
||
adminListener.handleRewindSuccess(payload); | ||
|
||
expect(eventEmitter.emit).toHaveBeenCalledWith(AdminEvent.RewindTargetResponse, { | ||
...payload, | ||
message: `Rewind to block ${payload.height} successful`, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('handleRewindFailure', () => { | ||
it('should emit RewindTargetResponse event on rewind failure', () => { | ||
const payload = {height: 1, success: false} as RewindPayload; | ||
|
||
adminListener.handleRewindFailure(payload); | ||
|
||
expect(eventEmitter.emit).toHaveBeenCalledWith(AdminEvent.RewindTargetResponse, payload); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.