239 lines
8 KiB
TypeScript
239 lines
8 KiB
TypeScript
import { describe, expect, it, mock } from 'bun:test';
|
|
import { Queue, removeScheduledJobsFromBullQueue } from '../src/queue';
|
|
import { QueueManager } from '../src/queue-manager';
|
|
import type { QueueWorkerConfig, RedisConfig } from '../src/types';
|
|
|
|
describe('Queue Class', () => {
|
|
const mockRedisConfig: RedisConfig = {
|
|
host: 'localhost',
|
|
port: 6379,
|
|
};
|
|
|
|
describe('basic functionality', () => {
|
|
it('should create queue with minimal config', () => {
|
|
const queue = new Queue('test-queue', mockRedisConfig);
|
|
expect(queue).toBeDefined();
|
|
expect(queue.getName()).toBe('test-queue');
|
|
});
|
|
|
|
it('should create queue with default job options', () => {
|
|
const defaultJobOptions = {
|
|
attempts: 5,
|
|
backoff: { type: 'exponential' as const, delay: 2000 },
|
|
};
|
|
|
|
const queue = new Queue('test-queue', mockRedisConfig, defaultJobOptions);
|
|
expect(queue).toBeDefined();
|
|
expect(queue.getName()).toBe('test-queue');
|
|
});
|
|
|
|
it('should create queue with custom logger', () => {
|
|
const mockLogger = {
|
|
info: mock(() => {}),
|
|
error: mock(() => {}),
|
|
warn: mock(() => {}),
|
|
debug: mock(() => {}),
|
|
trace: mock(() => {}),
|
|
};
|
|
|
|
const queue = new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger);
|
|
expect(queue).toBeDefined();
|
|
});
|
|
|
|
it('should create queue with worker config', () => {
|
|
const workerConfig: QueueWorkerConfig = {
|
|
workers: 2,
|
|
concurrency: 5,
|
|
startWorker: false, // Don't actually start workers
|
|
serviceName: 'test-service',
|
|
};
|
|
|
|
const queue = new Queue('test-queue', mockRedisConfig, {}, workerConfig);
|
|
expect(queue).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('queue naming and utilities', () => {
|
|
it('should return queue name', () => {
|
|
const queue = new Queue('my-test-queue', mockRedisConfig);
|
|
expect(queue.getName()).toBe('my-test-queue');
|
|
});
|
|
|
|
it('should get bull queue instance', () => {
|
|
const queue = new Queue('test-queue', mockRedisConfig);
|
|
const bullQueue = queue.getBullQueue();
|
|
expect(bullQueue).toBeDefined();
|
|
});
|
|
|
|
it('should create child logger with logger that supports child', () => {
|
|
const mockChildLogger = {
|
|
info: mock(() => {}),
|
|
error: mock(() => {}),
|
|
warn: mock(() => {}),
|
|
debug: mock(() => {}),
|
|
trace: mock(() => {}),
|
|
};
|
|
|
|
const mockLogger = {
|
|
info: mock(() => {}),
|
|
error: mock(() => {}),
|
|
warn: mock(() => {}),
|
|
debug: mock(() => {}),
|
|
trace: mock(() => {}),
|
|
child: mock(() => mockChildLogger),
|
|
};
|
|
|
|
const queue = new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger);
|
|
const childLogger = queue.createChildLogger('batch', { batchId: '123' });
|
|
|
|
expect(childLogger).toBe(mockChildLogger);
|
|
expect(mockLogger.child).toHaveBeenCalledWith('batch', { batchId: '123' });
|
|
});
|
|
|
|
it('should fallback to main logger if child not supported', () => {
|
|
const mockLogger = {
|
|
info: mock(() => {}),
|
|
error: mock(() => {}),
|
|
warn: mock(() => {}),
|
|
debug: mock(() => {}),
|
|
trace: mock(() => {}),
|
|
};
|
|
|
|
const queue = new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger);
|
|
const childLogger = queue.createChildLogger('batch', { batchId: '123' });
|
|
|
|
expect(childLogger).toBe(mockLogger);
|
|
});
|
|
});
|
|
|
|
describe('worker count methods', () => {
|
|
it('should get worker count when no workers', () => {
|
|
const queue = new Queue('test-queue', mockRedisConfig);
|
|
expect(queue.getWorkerCount()).toBe(0);
|
|
});
|
|
|
|
it('should handle worker count with workers config', () => {
|
|
const workerConfig: QueueWorkerConfig = {
|
|
workers: 3,
|
|
startWorker: false, // Don't actually start
|
|
};
|
|
|
|
const queue = new Queue('test-queue', mockRedisConfig, {}, workerConfig);
|
|
// Workers aren't actually started with startWorker: false
|
|
expect(queue.getWorkerCount()).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('scheduled job reconciliation', () => {
|
|
it('removes repeatable jobs, job schedulers, and pending/prioritized repeat instances while preserving manual jobs', async () => {
|
|
const removedRepeatKeys: string[] = [];
|
|
const removedSchedulerKeys: string[] = [];
|
|
const removedJobIds: string[] = [];
|
|
let requestedStates: string[] = [];
|
|
|
|
const fakeBullQueue = {
|
|
getRepeatableJobs: mock(async () => [
|
|
{ key: '{data-ingestion_te}:crawlScheduler', name: 'crawlScheduler' },
|
|
]),
|
|
removeRepeatableByKey: mock(async (key: string) => {
|
|
removedRepeatKeys.push(key);
|
|
return true;
|
|
}),
|
|
getJobSchedulers: mock(async () => [
|
|
{ key: '{data-ingestion_te}:crawlScheduler', name: 'crawlScheduler' },
|
|
{ key: '{data-ingestion_te}:dataScheduler', name: 'dataScheduler' },
|
|
]),
|
|
removeJobScheduler: mock(async (key: string) => {
|
|
removedSchedulerKeys.push(key);
|
|
return true;
|
|
}),
|
|
getJobs: mock(async (states: string[]) => {
|
|
requestedStates = states;
|
|
return [
|
|
{
|
|
id: 'repeat-job-1',
|
|
repeatJobKey: '{data-ingestion_te}:crawlScheduler',
|
|
remove: mock(async () => {
|
|
removedJobIds.push('repeat-job-1');
|
|
}),
|
|
},
|
|
{
|
|
id: 'prioritized-repeat-job-1',
|
|
opts: { priority: 5, repeatJobKey: '{data-ingestion_te}:crawlScheduler' },
|
|
remove: mock(async () => {
|
|
removedJobIds.push('prioritized-repeat-job-1');
|
|
}),
|
|
},
|
|
{
|
|
id: 'manual-eod-job-1',
|
|
opts: {},
|
|
remove: mock(async () => {
|
|
removedJobIds.push('manual-eod-job-1');
|
|
}),
|
|
},
|
|
{
|
|
id: 'prioritized-manual-job-1',
|
|
opts: { priority: 5 },
|
|
remove: mock(async () => {
|
|
removedJobIds.push('prioritized-manual-job-1');
|
|
}),
|
|
},
|
|
];
|
|
}),
|
|
};
|
|
|
|
const removedCount = await removeScheduledJobsFromBullQueue(fakeBullQueue as any);
|
|
|
|
expect(requestedStates).toEqual(['waiting', 'delayed', 'prioritized']);
|
|
expect(removedCount).toBe(2);
|
|
expect(removedRepeatKeys).toEqual(['{data-ingestion_te}:crawlScheduler']);
|
|
expect(removedSchedulerKeys).toEqual(['{data-ingestion_te}:dataScheduler']);
|
|
expect(removedJobIds).toEqual(['repeat-job-1', 'prioritized-repeat-job-1']);
|
|
});
|
|
|
|
it('keeps owned queues workerless during initialization when delayWorkerStart is enabled until startAllWorkers is called', async () => {
|
|
const mockLogger = {
|
|
info: mock(() => {}),
|
|
error: mock(() => {}),
|
|
warn: mock(() => {}),
|
|
debug: mock(() => {}),
|
|
trace: mock(() => {}),
|
|
};
|
|
|
|
const manager = new QueueManager(
|
|
{
|
|
serviceName: 'data-ingestion',
|
|
redis: { ...mockRedisConfig, maxRetriesPerRequest: 1 },
|
|
delayWorkerStart: true,
|
|
defaultQueueOptions: { workers: 1, concurrency: 2 },
|
|
},
|
|
undefined,
|
|
mockLogger
|
|
);
|
|
|
|
// Suppress background connection error logs in offline test environment
|
|
const ioRedis = (
|
|
manager as unknown as {
|
|
ioredisConnection?: { on: (e: string, cb: () => void) => void; disconnect: () => void };
|
|
}
|
|
).ioredisConnection;
|
|
ioRedis?.on('error', () => {});
|
|
|
|
try {
|
|
const teQueue = manager.getQueue('te');
|
|
expect(teQueue.getName()).toBe('{data-ingestion_te}');
|
|
// Queue must stay workerless while handlers initialize and schedules reconcile
|
|
expect(teQueue.getWorkerCount()).toBe(0);
|
|
|
|
const startSpy = mock(() => {});
|
|
teQueue.startWorkersManually = startSpy;
|
|
|
|
manager.startAllWorkers();
|
|
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
expect(startSpy).toHaveBeenCalledWith(1, 2);
|
|
} finally {
|
|
ioRedis?.disconnect();
|
|
}
|
|
});
|
|
});
|
|
});
|