T-16: M0.1b — Make repo-wide bun test green: fix queue, shutdown-signal and research-workflow test failures
This commit is contained in:
parent
890761e0d5
commit
9c4e87cdfd
17 changed files with 366 additions and 266 deletions
|
|
@ -99,6 +99,14 @@ export class QueueManager {
|
|||
this.ioredisConnection = deps?.createRedis
|
||||
? deps.createRedis(ioredisOptions)
|
||||
: new Redis(ioredisOptions);
|
||||
if (typeof this.ioredisConnection.on === 'function') {
|
||||
this.ioredisConnection.on('error', error => {
|
||||
this.logger.error('QueueManager Redis connection error', {
|
||||
service: this.serviceName,
|
||||
error,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Rate limiter will be initialized when rules are added dynamically
|
||||
// No longer initialized from config
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ export class Queue {
|
|||
|
||||
// Initialize queue events if workers will be used
|
||||
if (config.workers && config.workers > 0) {
|
||||
this.queueEvents = new QueueEvents(queueName, { connection });
|
||||
this.queueEvents = this.createQueueEvents(connection);
|
||||
}
|
||||
|
||||
// Start workers if requested and not explicitly disabled
|
||||
|
|
@ -262,12 +262,18 @@ export class Queue {
|
|||
return await this.bullQueue.getJob(jobId);
|
||||
}
|
||||
|
||||
private createQueueEvents(connection: ReturnType<typeof getRedisConnection>): QueueEvents {
|
||||
const queueEvents = new QueueEvents(this.queueName, { connection });
|
||||
queueEvents.on('error', error =>
|
||||
this.logger.error('QueueEvents connection error', { queueName: this.queueName, error })
|
||||
);
|
||||
return queueEvents;
|
||||
}
|
||||
|
||||
/** Wait for a result using a QueueEvents connection owned by this queue. */
|
||||
async waitForJob<T>(job: Job<unknown, T>, timeout = 30000): Promise<T> {
|
||||
if (!this.queueEvents) {
|
||||
this.queueEvents = new QueueEvents(this.queueName, {
|
||||
connection: getRedisConnection(this.redisConfig),
|
||||
});
|
||||
this.queueEvents = this.createQueueEvents(getRedisConnection(this.redisConfig));
|
||||
}
|
||||
await this.queueEvents.waitUntilReady();
|
||||
return job.waitUntilFinished(this.queueEvents, timeout);
|
||||
|
|
@ -589,7 +595,7 @@ export class Queue {
|
|||
// Initialize queue events if not already done
|
||||
if (!this.queueEvents) {
|
||||
const connection = getRedisConnection(this.redisConfig);
|
||||
this.queueEvents = new QueueEvents(this.queueName, { connection });
|
||||
this.queueEvents = this.createQueueEvents(connection);
|
||||
}
|
||||
|
||||
this.startWorkers(workerCount, concurrency);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,37 @@
|
|||
import { describe, expect, it, mock } from 'bun:test';
|
||||
import { afterEach, 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', () => {
|
||||
// Use a non-QA isolated loopback port so unit tests never touch QA Dragonfly (localhost:6379)
|
||||
const mockRedisConfig: RedisConfig = {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
host: '127.0.0.1',
|
||||
port: 16379,
|
||||
maxRetriesPerRequest: 1,
|
||||
retryDelay: 50,
|
||||
};
|
||||
const createdQueues: Queue[] = [];
|
||||
|
||||
function trackQueue(queue: Queue): Queue {
|
||||
createdQueues.push(queue);
|
||||
return queue;
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
createdQueues.splice(0).map(async queue => {
|
||||
// Stop reconnect timers on unreachable test sockets before awaiting BullMQ close()
|
||||
(queue.getBullQueue() as any).connection?._client?.disconnect(false);
|
||||
(queue as any).queueEvents?.connection?._client?.disconnect(false);
|
||||
await queue.close().catch(() => {});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
describe('basic functionality', () => {
|
||||
it('should create queue with minimal config', () => {
|
||||
const queue = new Queue('test-queue', mockRedisConfig);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig));
|
||||
expect(queue).toBeDefined();
|
||||
expect(queue.getName()).toBe('test-queue');
|
||||
});
|
||||
|
|
@ -22,7 +42,7 @@ describe('Queue Class', () => {
|
|||
backoff: { type: 'exponential' as const, delay: 2000 },
|
||||
};
|
||||
|
||||
const queue = new Queue('test-queue', mockRedisConfig, defaultJobOptions);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig, defaultJobOptions));
|
||||
expect(queue).toBeDefined();
|
||||
expect(queue.getName()).toBe('test-queue');
|
||||
});
|
||||
|
|
@ -36,7 +56,7 @@ describe('Queue Class', () => {
|
|||
trace: mock(() => {}),
|
||||
};
|
||||
|
||||
const queue = new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger));
|
||||
expect(queue).toBeDefined();
|
||||
});
|
||||
|
||||
|
|
@ -48,19 +68,19 @@ describe('Queue Class', () => {
|
|||
serviceName: 'test-service',
|
||||
};
|
||||
|
||||
const queue = new Queue('test-queue', mockRedisConfig, {}, workerConfig);
|
||||
const queue = trackQueue(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);
|
||||
const queue = trackQueue(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 queue = trackQueue(new Queue('test-queue', mockRedisConfig));
|
||||
const bullQueue = queue.getBullQueue();
|
||||
expect(bullQueue).toBeDefined();
|
||||
});
|
||||
|
|
@ -83,7 +103,7 @@ describe('Queue Class', () => {
|
|||
child: mock(() => mockChildLogger),
|
||||
};
|
||||
|
||||
const queue = new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger));
|
||||
const childLogger = queue.createChildLogger('batch', { batchId: '123' });
|
||||
|
||||
expect(childLogger).toBe(mockChildLogger);
|
||||
|
|
@ -99,7 +119,7 @@ describe('Queue Class', () => {
|
|||
trace: mock(() => {}),
|
||||
};
|
||||
|
||||
const queue = new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig, {}, {}, mockLogger));
|
||||
const childLogger = queue.createChildLogger('batch', { batchId: '123' });
|
||||
|
||||
expect(childLogger).toBe(mockLogger);
|
||||
|
|
@ -108,7 +128,7 @@ describe('Queue Class', () => {
|
|||
|
||||
describe('worker count methods', () => {
|
||||
it('should get worker count when no workers', () => {
|
||||
const queue = new Queue('test-queue', mockRedisConfig);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig));
|
||||
expect(queue.getWorkerCount()).toBe(0);
|
||||
});
|
||||
|
||||
|
|
@ -118,7 +138,7 @@ describe('Queue Class', () => {
|
|||
startWorker: false, // Don't actually start
|
||||
};
|
||||
|
||||
const queue = new Queue('test-queue', mockRedisConfig, {}, workerConfig);
|
||||
const queue = trackQueue(new Queue('test-queue', mockRedisConfig, {}, workerConfig));
|
||||
// Workers aren't actually started with startWorker: false
|
||||
expect(queue.getWorkerCount()).toBe(0);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { describe, expect, it } from 'bun:test';
|
||||
import { afterEach, describe, expect, it } from 'bun:test';
|
||||
import { createServiceCache, ServiceCache } from '../src/service-cache';
|
||||
import {
|
||||
generateCachePrefix,
|
||||
|
|
@ -78,39 +78,40 @@ describe('Service Utilities', () => {
|
|||
});
|
||||
|
||||
describe('ServiceCache', () => {
|
||||
it('should create service cache', () => {
|
||||
const mockRedisConfig = {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
};
|
||||
// Use a non-QA isolated loopback port and non-shared clients so unit tests never touch QA Dragonfly (localhost:6379)
|
||||
const mockRedisConfig = {
|
||||
host: '127.0.0.1',
|
||||
port: 16379,
|
||||
maxRetriesPerRequest: 1,
|
||||
retryDelay: 50,
|
||||
};
|
||||
const createdCaches: ServiceCache[] = [];
|
||||
|
||||
// Since ServiceCache constructor internally creates a real cache,
|
||||
// we can't easily test it without mocking the createCache function
|
||||
// For now, just test that the function exists and returns something
|
||||
const serviceCache = createServiceCache('myservice', mockRedisConfig);
|
||||
afterEach(async () => {
|
||||
await Promise.all(createdCaches.splice(0).map(cache => cache.disconnect().catch(() => {})));
|
||||
});
|
||||
|
||||
it('should create service cache', () => {
|
||||
const serviceCache = createServiceCache('myservice', mockRedisConfig, { shared: false });
|
||||
createdCaches.push(serviceCache);
|
||||
expect(serviceCache).toBeDefined();
|
||||
expect(serviceCache).toBeInstanceOf(ServiceCache);
|
||||
});
|
||||
|
||||
it('should handle cache prefix correctly', () => {
|
||||
const mockRedisConfig = {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
};
|
||||
|
||||
const serviceCache = createServiceCache('webApi', mockRedisConfig);
|
||||
const serviceCache = createServiceCache('webApi', mockRedisConfig, { shared: false });
|
||||
createdCaches.push(serviceCache);
|
||||
expect(serviceCache).toBeDefined();
|
||||
// The prefix is set internally as cache:web-api
|
||||
expect(serviceCache.getKey('test')).toBe('cache:web-api:test');
|
||||
});
|
||||
|
||||
it('should support global cache option', () => {
|
||||
const mockRedisConfig = {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
};
|
||||
|
||||
const globalCache = createServiceCache('myservice', mockRedisConfig, { global: true });
|
||||
const globalCache = createServiceCache('myservice', mockRedisConfig, {
|
||||
global: true,
|
||||
shared: false,
|
||||
});
|
||||
createdCaches.push(globalCache);
|
||||
expect(globalCache).toBeDefined();
|
||||
// Global cache uses a different prefix
|
||||
expect(globalCache.getKey('test')).toBe('stock-bot:shared:test');
|
||||
|
|
@ -120,16 +121,16 @@ describe('ServiceCache', () => {
|
|||
describe('Batch Processing', () => {
|
||||
it('should handle batch job data types', () => {
|
||||
const batchJob: BatchJobData = {
|
||||
items: [1, 2, 3],
|
||||
options: {
|
||||
batchSize: 10,
|
||||
concurrency: 2,
|
||||
},
|
||||
payloadKey: 'batch:payload:1',
|
||||
batchIndex: 0,
|
||||
totalBatches: 3,
|
||||
itemCount: 10,
|
||||
totalDelayHours: 1,
|
||||
};
|
||||
|
||||
expect(batchJob.items).toHaveLength(3);
|
||||
expect(batchJob.options.batchSize).toBe(10);
|
||||
expect(batchJob.options.concurrency).toBe(2);
|
||||
expect(batchJob.payloadKey).toBe('batch:payload:1');
|
||||
expect(batchJob.totalBatches).toBe(3);
|
||||
expect(batchJob.itemCount).toBe(10);
|
||||
});
|
||||
|
||||
it('should process batch results', () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue