|
| 1 | +import { test as JapaTest } from '@japa/runner' |
| 2 | +import { setTimeout } from 'node:timers/promises' |
| 3 | +import { Worker } from '../../src/worker.js' |
| 4 | +import { Locator } from '../../src/locator.js' |
| 5 | +import { Job } from '../../src/job.js' |
| 6 | +import type { Adapter } from '../../src/contracts/adapter.js' |
| 7 | +import type { QueueManagerConfig } from '../../src/types/main.js' |
| 8 | + |
| 9 | +interface WorkerConcurrencyTestSuiteOptions { |
| 10 | + test: typeof JapaTest |
| 11 | + createAdapter: () => Adapter | Promise<Adapter> |
| 12 | +} |
| 13 | + |
| 14 | +export function registerWorkerConcurrencyTestSuite(options: WorkerConcurrencyTestSuiteOptions) { |
| 15 | + const { test } = options |
| 16 | + |
| 17 | + test('single job should be processed exactly once with concurrency > 1', async ({ |
| 18 | + assert, |
| 19 | + cleanup, |
| 20 | + }) => { |
| 21 | + const jobExecutions: Map<string, number> = new Map() |
| 22 | + |
| 23 | + class TrackingJob extends Job<{ jobId: string }> { |
| 24 | + static jobName = 'TrackingJob' |
| 25 | + |
| 26 | + async execute() { |
| 27 | + const count = jobExecutions.get(this.payload.jobId) || 0 |
| 28 | + jobExecutions.set(this.payload.jobId, count + 1) |
| 29 | + await setTimeout(50) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const adapter = await options.createAdapter() |
| 34 | + Locator.register('TrackingJob', TrackingJob) |
| 35 | + |
| 36 | + cleanup(() => Locator.clear()) |
| 37 | + |
| 38 | + const config: QueueManagerConfig = { |
| 39 | + default: 'test', |
| 40 | + adapters: { test: () => adapter }, |
| 41 | + worker: { concurrency: 5 }, |
| 42 | + } |
| 43 | + |
| 44 | + const worker = new Worker(config) |
| 45 | + |
| 46 | + cleanup(async () => { |
| 47 | + await worker.stop() |
| 48 | + }) |
| 49 | + |
| 50 | + // Push a single job |
| 51 | + await adapter.pushOn('default', { |
| 52 | + id: 'single-job-1', |
| 53 | + name: 'TrackingJob', |
| 54 | + payload: { jobId: 'job-1' }, |
| 55 | + attempts: 0, |
| 56 | + }) |
| 57 | + |
| 58 | + // Process until idle |
| 59 | + let cycles = 0 |
| 60 | + const maxCycles = 20 |
| 61 | + while (cycles < maxCycles) { |
| 62 | + const cycle = await worker.processCycle(['default']) |
| 63 | + cycles++ |
| 64 | + if (cycle?.type === 'idle') break |
| 65 | + } |
| 66 | + |
| 67 | + assert.equal( |
| 68 | + jobExecutions.get('job-1'), |
| 69 | + 1, |
| 70 | + 'Job should be executed exactly once with concurrency 5' |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + test('multiple jobs should each be processed exactly once with high concurrency', async ({ |
| 75 | + assert, |
| 76 | + cleanup, |
| 77 | + }) => { |
| 78 | + const jobExecutions: Map<string, number> = new Map() |
| 79 | + |
| 80 | + class TrackingJob extends Job<{ jobId: string }> { |
| 81 | + static jobName = 'TrackingJob' |
| 82 | + |
| 83 | + async execute() { |
| 84 | + const count = jobExecutions.get(this.payload.jobId) || 0 |
| 85 | + jobExecutions.set(this.payload.jobId, count + 1) |
| 86 | + await setTimeout(30) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const adapter = await options.createAdapter() |
| 91 | + Locator.register('TrackingJob', TrackingJob) |
| 92 | + |
| 93 | + cleanup(() => Locator.clear()) |
| 94 | + |
| 95 | + const config: QueueManagerConfig = { |
| 96 | + default: 'test', |
| 97 | + adapters: { test: () => adapter }, |
| 98 | + worker: { concurrency: 5 }, |
| 99 | + } |
| 100 | + |
| 101 | + const worker = new Worker(config) |
| 102 | + |
| 103 | + cleanup(async () => { |
| 104 | + await worker.stop() |
| 105 | + }) |
| 106 | + |
| 107 | + // Push 3 jobs (less than concurrency) |
| 108 | + for (let i = 1; i <= 3; i++) { |
| 109 | + await adapter.pushOn('default', { |
| 110 | + id: `job-${i}`, |
| 111 | + name: 'TrackingJob', |
| 112 | + payload: { jobId: `job-${i}` }, |
| 113 | + attempts: 0, |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + // Process until idle |
| 118 | + let cycles = 0 |
| 119 | + const maxCycles = 30 |
| 120 | + while (cycles < maxCycles) { |
| 121 | + const cycle = await worker.processCycle(['default']) |
| 122 | + cycles++ |
| 123 | + if (cycle?.type === 'idle') break |
| 124 | + } |
| 125 | + |
| 126 | + assert.equal(jobExecutions.size, 3, 'All 3 jobs should have been executed') |
| 127 | + for (const [jobId, count] of jobExecutions) { |
| 128 | + assert.equal(count, 1, `${jobId} should be executed exactly once`) |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + test('jobs should not be duplicated under concurrent popFrom stress', async ({ |
| 133 | + assert, |
| 134 | + cleanup, |
| 135 | + }) => { |
| 136 | + const jobExecutions: Map<string, number> = new Map() |
| 137 | + const executionOrder: string[] = [] |
| 138 | + |
| 139 | + class TrackingJob extends Job<{ jobId: string }> { |
| 140 | + static jobName = 'TrackingJob' |
| 141 | + |
| 142 | + async execute() { |
| 143 | + executionOrder.push(this.payload.jobId) |
| 144 | + const count = jobExecutions.get(this.payload.jobId) || 0 |
| 145 | + jobExecutions.set(this.payload.jobId, count + 1) |
| 146 | + // Very short execution to maximize concurrency pressure |
| 147 | + await setTimeout(5) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + const adapter = await options.createAdapter() |
| 152 | + Locator.register('TrackingJob', TrackingJob) |
| 153 | + |
| 154 | + cleanup(() => Locator.clear()) |
| 155 | + |
| 156 | + const config: QueueManagerConfig = { |
| 157 | + default: 'test', |
| 158 | + adapters: { test: () => adapter }, |
| 159 | + worker: { concurrency: 10 }, |
| 160 | + } |
| 161 | + |
| 162 | + const worker = new Worker(config) |
| 163 | + |
| 164 | + cleanup(async () => { |
| 165 | + await worker.stop() |
| 166 | + }) |
| 167 | + |
| 168 | + // Push many jobs quickly |
| 169 | + const jobCount = 20 |
| 170 | + for (let i = 1; i <= jobCount; i++) { |
| 171 | + await adapter.pushOn('default', { |
| 172 | + id: `stress-job-${i}`, |
| 173 | + name: 'TrackingJob', |
| 174 | + payload: { jobId: `stress-job-${i}` }, |
| 175 | + attempts: 0, |
| 176 | + }) |
| 177 | + } |
| 178 | + |
| 179 | + // Process until idle |
| 180 | + let cycles = 0 |
| 181 | + const maxCycles = 100 |
| 182 | + while (cycles < maxCycles) { |
| 183 | + const cycle = await worker.processCycle(['default']) |
| 184 | + cycles++ |
| 185 | + if (cycle?.type === 'idle') break |
| 186 | + } |
| 187 | + |
| 188 | + // Verify no duplicates |
| 189 | + assert.equal(jobExecutions.size, jobCount, `All ${jobCount} jobs should have been executed`) |
| 190 | + for (const [jobId, count] of jobExecutions) { |
| 191 | + assert.equal(count, 1, `${jobId} should be executed exactly once`) |
| 192 | + } |
| 193 | + |
| 194 | + // Verify execution order has no duplicates |
| 195 | + const uniqueExecutions = new Set(executionOrder) |
| 196 | + assert.equal( |
| 197 | + executionOrder.length, |
| 198 | + uniqueExecutions.size, |
| 199 | + 'No job should appear twice in execution order' |
| 200 | + ) |
| 201 | + }) |
| 202 | +} |
0 commit comments