|
1 | | -import { createLogger } from '../logger'; |
| 1 | +import { type Logger } from '../logger'; |
| 2 | +import * as utils from '../utils'; |
2 | 3 |
|
3 | 4 | import { runInLoop } from './index'; |
4 | 5 |
|
| 6 | +const createMockLogger = (): Logger => ({ |
| 7 | + runWithContext: jest.fn((_: Record<string, any>, fn: () => any) => fn()) as Logger['runWithContext'], |
| 8 | + debug: jest.fn(), |
| 9 | + info: jest.fn(), |
| 10 | + warn: jest.fn(), |
| 11 | + error: jest.fn() as Logger['error'], |
| 12 | + child: jest.fn() as Logger['child'], |
| 13 | +}); |
| 14 | + |
| 15 | +const createTestRunInLoopFunction = (executions: number) => { |
| 16 | + let callCount = 0; |
| 17 | + |
| 18 | + return jest.fn(async () => { |
| 19 | + callCount += 1; |
| 20 | + return { shouldContinueRunning: callCount < executions }; |
| 21 | + }); |
| 22 | +}; |
| 23 | + |
| 24 | +const getRunContexts = (mockedLogger: Logger) => { |
| 25 | + const runWithContextMock = jest.mocked(mockedLogger.runWithContext); |
| 26 | + return runWithContextMock.mock.calls.map(([context]) => context); |
| 27 | +}; |
| 28 | + |
5 | 29 | describe(runInLoop.name, () => { |
6 | | - const logger = createLogger({ |
7 | | - colorize: true, |
8 | | - enabled: true, |
9 | | - minLevel: 'info', |
10 | | - format: 'json', |
| 30 | + afterEach(() => { |
| 31 | + jest.restoreAllMocks(); |
11 | 32 | }); |
12 | 33 |
|
13 | 34 | it('stops the loop after getting the stop signal', async () => { |
14 | | - const fn = async () => ({ shouldContinueRunning: false }); |
15 | | - const fnSpy = jest |
16 | | - .spyOn({ fn }, 'fn') |
17 | | - .mockImplementationOnce(async () => ({ shouldContinueRunning: true })) |
18 | | - .mockImplementationOnce(async () => ({ shouldContinueRunning: true })) |
19 | | - .mockImplementationOnce(async () => ({ shouldContinueRunning: false })); |
20 | | - await runInLoop(fnSpy as any, { logger }); |
| 35 | + const mockedLogger = createMockLogger(); |
| 36 | + const fnSpy = createTestRunInLoopFunction(3); |
| 37 | + await runInLoop(fnSpy, { logger: mockedLogger }); |
21 | 38 | expect(fnSpy).toHaveBeenCalledTimes(3); |
22 | 39 | }); |
| 40 | + |
| 41 | + it('uses random execution IDs by default', async () => { |
| 42 | + const mockedLogger = createMockLogger(); |
| 43 | + const fnSpy = createTestRunInLoopFunction(1); |
| 44 | + jest.spyOn(utils, 'generateRandomBytes32').mockReturnValue('0xrandom'); |
| 45 | + |
| 46 | + await runInLoop(fnSpy, { logger: mockedLogger }); |
| 47 | + |
| 48 | + expect(getRunContexts(mockedLogger)).toStrictEqual([{ executionId: '0xrandom' }]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('uses incremental execution IDs', async () => { |
| 52 | + const mockedLogger = createMockLogger(); |
| 53 | + const fnSpy = createTestRunInLoopFunction(3); |
| 54 | + |
| 55 | + await runInLoop(fnSpy, { |
| 56 | + logger: mockedLogger, |
| 57 | + executionIdOptions: { type: 'incremental' }, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(getRunContexts(mockedLogger)).toStrictEqual([ |
| 61 | + { executionId: '0' }, |
| 62 | + { executionId: '1' }, |
| 63 | + { executionId: '2' }, |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('uses incremental execution IDs with prefix', async () => { |
| 68 | + const mockedLogger = createMockLogger(); |
| 69 | + const fnSpy = createTestRunInLoopFunction(3); |
| 70 | + |
| 71 | + await runInLoop(fnSpy, { |
| 72 | + logger: mockedLogger, |
| 73 | + executionIdOptions: { type: 'incremental', prefix: 'worker-' }, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(getRunContexts(mockedLogger)).toStrictEqual([ |
| 77 | + { executionId: 'worker-0' }, |
| 78 | + { executionId: 'worker-1' }, |
| 79 | + { executionId: 'worker-2' }, |
| 80 | + ]); |
| 81 | + }); |
23 | 82 | }); |
0 commit comments