|
| 1 | +import { assertEquals } from '@std/assert'; |
| 2 | +import { Worker } from '../../src/core/Worker.ts'; |
| 3 | +import type { |
| 4 | + IBatchProcessor, |
| 5 | + ILifecycle, |
| 6 | + WorkerBootstrap, |
| 7 | +} from '../../src/core/types.ts'; |
| 8 | +import { createLoggingFactory } from '../../src/platform/logging.ts'; |
| 9 | + |
| 10 | +const loggingFactory = createLoggingFactory(); |
| 11 | +loggingFactory.setLogLevel('error'); // Suppress debug output during tests |
| 12 | +const logger = loggingFactory.createLogger('Worker.startOnlyOnce.test'); |
| 13 | + |
| 14 | +// Mock ILifecycle that tracks calls and allows controlling state |
| 15 | +function createMockLifecycle( |
| 16 | + state: 'created' | 'starting' | 'running' | 'stopping' | 'stopped' |
| 17 | +): ILifecycle & { acknowledgeStartCalled: boolean } { |
| 18 | + return { |
| 19 | + acknowledgeStartCalled: false, |
| 20 | + acknowledgeStart: function () { |
| 21 | + this.acknowledgeStartCalled = true; |
| 22 | + return Promise.resolve(); |
| 23 | + }, |
| 24 | + acknowledgeStop: () => {}, |
| 25 | + sendHeartbeat: async () => {}, |
| 26 | + edgeFunctionName: 'test-function', |
| 27 | + queueName: 'test-queue', |
| 28 | + isCreated: state === 'created', |
| 29 | + isRunning: state === 'running', |
| 30 | + isStopping: state === 'stopping', |
| 31 | + isStopped: state === 'stopped', |
| 32 | + transitionToStopping: () => {}, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +// Mock IBatchProcessor |
| 37 | +function createMockBatchProcessor(): IBatchProcessor { |
| 38 | + return { |
| 39 | + processBatch: async () => {}, |
| 40 | + awaitCompletion: async () => {}, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +// Mock SQL connection |
| 45 | +const mockSql = { |
| 46 | + end: async () => {}, |
| 47 | +} as never; |
| 48 | + |
| 49 | +const workerBootstrap: WorkerBootstrap = { |
| 50 | + edgeFunctionName: 'test-function', |
| 51 | + workerId: 'test-worker-id', |
| 52 | +}; |
| 53 | + |
| 54 | +Deno.test('Worker.startOnlyOnce - starts worker when in Created state', async () => { |
| 55 | + const lifecycle = createMockLifecycle('created'); |
| 56 | + const batchProcessor = createMockBatchProcessor(); |
| 57 | + const worker = new Worker(batchProcessor, lifecycle, mockSql as never, logger); |
| 58 | + |
| 59 | + worker.startOnlyOnce(workerBootstrap); |
| 60 | + |
| 61 | + // Give the async start() a moment to call acknowledgeStart |
| 62 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 63 | + |
| 64 | + assertEquals( |
| 65 | + lifecycle.acknowledgeStartCalled, |
| 66 | + true, |
| 67 | + 'Worker should start when in Created state' |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +Deno.test('Worker.startOnlyOnce - ignores request when in Starting state', async () => { |
| 72 | + const lifecycle = createMockLifecycle('starting'); |
| 73 | + const batchProcessor = createMockBatchProcessor(); |
| 74 | + const worker = new Worker(batchProcessor, lifecycle, mockSql as never, logger); |
| 75 | + |
| 76 | + worker.startOnlyOnce(workerBootstrap); |
| 77 | + |
| 78 | + // Give any async operations a moment |
| 79 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 80 | + |
| 81 | + assertEquals( |
| 82 | + lifecycle.acknowledgeStartCalled, |
| 83 | + false, |
| 84 | + 'Worker should NOT start when in Starting state' |
| 85 | + ); |
| 86 | +}); |
| 87 | + |
| 88 | +Deno.test('Worker.startOnlyOnce - ignores request when in Running state', async () => { |
| 89 | + const lifecycle = createMockLifecycle('running'); |
| 90 | + const batchProcessor = createMockBatchProcessor(); |
| 91 | + const worker = new Worker(batchProcessor, lifecycle, mockSql as never, logger); |
| 92 | + |
| 93 | + worker.startOnlyOnce(workerBootstrap); |
| 94 | + |
| 95 | + // Give any async operations a moment |
| 96 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 97 | + |
| 98 | + assertEquals( |
| 99 | + lifecycle.acknowledgeStartCalled, |
| 100 | + false, |
| 101 | + 'Worker should NOT start when in Running state' |
| 102 | + ); |
| 103 | +}); |
| 104 | + |
| 105 | +Deno.test('Worker.startOnlyOnce - ignores request when in Stopping state', async () => { |
| 106 | + const lifecycle = createMockLifecycle('stopping'); |
| 107 | + const batchProcessor = createMockBatchProcessor(); |
| 108 | + const worker = new Worker(batchProcessor, lifecycle, mockSql as never, logger); |
| 109 | + |
| 110 | + worker.startOnlyOnce(workerBootstrap); |
| 111 | + |
| 112 | + // Give any async operations a moment |
| 113 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 114 | + |
| 115 | + assertEquals( |
| 116 | + lifecycle.acknowledgeStartCalled, |
| 117 | + false, |
| 118 | + 'Worker should NOT start when in Stopping state' |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +Deno.test('Worker.startOnlyOnce - ignores request when in Stopped state', async () => { |
| 123 | + const lifecycle = createMockLifecycle('stopped'); |
| 124 | + const batchProcessor = createMockBatchProcessor(); |
| 125 | + const worker = new Worker(batchProcessor, lifecycle, mockSql as never, logger); |
| 126 | + |
| 127 | + worker.startOnlyOnce(workerBootstrap); |
| 128 | + |
| 129 | + // Give any async operations a moment |
| 130 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 131 | + |
| 132 | + assertEquals( |
| 133 | + lifecycle.acknowledgeStartCalled, |
| 134 | + false, |
| 135 | + 'Worker should NOT start when in Stopped state' |
| 136 | + ); |
| 137 | +}); |
0 commit comments