|
| 1 | +import { setupRunnerConfigReloadRuntime } from '../../../src/runners/config-reload-runtime'; |
| 2 | + |
| 3 | +const reloadMock = jest.fn().mockResolvedValue(true); |
| 4 | +const initializeMock = jest.fn().mockResolvedValue(undefined); |
| 5 | +const shutdownMock = jest.fn().mockResolvedValue(undefined); |
| 6 | +const watcherStartMock = jest.fn(); |
| 7 | +const watcherStopMock = jest.fn(); |
| 8 | + |
| 9 | +jest.mock('../../../src/logger', () => ({ |
| 10 | + logger: { |
| 11 | + info: jest.fn(), |
| 12 | + warn: jest.fn(), |
| 13 | + debug: jest.fn(), |
| 14 | + error: jest.fn(), |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('../../../src/config/config-snapshot-store', () => ({ |
| 19 | + ConfigSnapshotStore: jest.fn().mockImplementation(() => ({ |
| 20 | + initialize: initializeMock, |
| 21 | + shutdown: shutdownMock, |
| 22 | + })), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('../../../src/config/config-reloader', () => ({ |
| 26 | + ConfigReloader: jest.fn().mockImplementation(() => ({ |
| 27 | + reload: reloadMock, |
| 28 | + })), |
| 29 | +})); |
| 30 | + |
| 31 | +jest.mock('../../../src/config/config-watcher', () => ({ |
| 32 | + ConfigWatcher: jest.fn().mockImplementation(() => ({ |
| 33 | + start: watcherStartMock, |
| 34 | + stop: watcherStopMock, |
| 35 | + })), |
| 36 | +})); |
| 37 | + |
| 38 | +describe('setupRunnerConfigReloadRuntime', () => { |
| 39 | + const mockLogger = jest.requireMock('../../../src/logger').logger as { |
| 40 | + info: jest.Mock; |
| 41 | + warn: jest.Mock; |
| 42 | + debug: jest.Mock; |
| 43 | + error: jest.Mock; |
| 44 | + }; |
| 45 | + let processOnSpy: jest.SpyInstance; |
| 46 | + let processRemoveListenerSpy: jest.SpyInstance; |
| 47 | + let registeredUsr2Handler: (() => void) | undefined; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + jest.clearAllMocks(); |
| 51 | + registeredUsr2Handler = undefined; |
| 52 | + processOnSpy = jest.spyOn(process, 'on').mockImplementation((( |
| 53 | + event: string, |
| 54 | + handler: () => void |
| 55 | + ) => { |
| 56 | + if (event === 'SIGUSR2') registeredUsr2Handler = handler; |
| 57 | + return process; |
| 58 | + }) as any); |
| 59 | + processRemoveListenerSpy = jest.spyOn(process, 'removeListener').mockImplementation((( |
| 60 | + event: string, |
| 61 | + handler: () => void |
| 62 | + ) => { |
| 63 | + if (event === 'SIGUSR2' && registeredUsr2Handler === handler) { |
| 64 | + registeredUsr2Handler = undefined; |
| 65 | + } |
| 66 | + return process; |
| 67 | + }) as any); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + processOnSpy.mockRestore(); |
| 72 | + processRemoveListenerSpy.mockRestore(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('registers SIGUSR2 reload handling without file watch mode', async () => { |
| 76 | + const runtime = await setupRunnerConfigReloadRuntime({ |
| 77 | + configPath: '/tmp/test.visor.yaml', |
| 78 | + watch: false, |
| 79 | + configManager: {} as any, |
| 80 | + onSwap: jest.fn(), |
| 81 | + }); |
| 82 | + |
| 83 | + expect(initializeMock).toHaveBeenCalledTimes(1); |
| 84 | + expect(watcherStartMock).not.toHaveBeenCalled(); |
| 85 | + expect(registeredUsr2Handler).toEqual(expect.any(Function)); |
| 86 | + |
| 87 | + registeredUsr2Handler?.(); |
| 88 | + await Promise.resolve(); |
| 89 | + |
| 90 | + expect(reloadMock).toHaveBeenCalledTimes(1); |
| 91 | + |
| 92 | + await runtime.cleanup(); |
| 93 | + expect(shutdownMock).toHaveBeenCalledTimes(1); |
| 94 | + expect(registeredUsr2Handler).toBeUndefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('starts file watching without registering a duplicate SIGUSR2 handler in watch mode', async () => { |
| 98 | + const runtime = await setupRunnerConfigReloadRuntime({ |
| 99 | + configPath: '/tmp/test.visor.yaml', |
| 100 | + watch: true, |
| 101 | + configManager: {} as any, |
| 102 | + onSwap: jest.fn(), |
| 103 | + }); |
| 104 | + |
| 105 | + expect(watcherStartMock).toHaveBeenCalledWith({ listenForSignals: false }); |
| 106 | + expect(registeredUsr2Handler).toEqual(expect.any(Function)); |
| 107 | + |
| 108 | + await runtime.cleanup(); |
| 109 | + expect(watcherStopMock).toHaveBeenCalledTimes(1); |
| 110 | + }); |
| 111 | + |
| 112 | + it('installs a non-fatal SIGUSR2 handler even without a config path', async () => { |
| 113 | + const runtime = await setupRunnerConfigReloadRuntime({ |
| 114 | + configPath: undefined, |
| 115 | + watch: false, |
| 116 | + configManager: {} as any, |
| 117 | + onSwap: jest.fn(), |
| 118 | + }); |
| 119 | + |
| 120 | + expect(initializeMock).not.toHaveBeenCalled(); |
| 121 | + expect(registeredUsr2Handler).toEqual(expect.any(Function)); |
| 122 | + |
| 123 | + expect(() => registeredUsr2Handler?.()).not.toThrow(); |
| 124 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 125 | + '[ConfigReload] Ignoring SIGUSR2: no --config path configured' |
| 126 | + ); |
| 127 | + |
| 128 | + await runtime.cleanup(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments