100 lines
3.6 KiB
TypeScript
100 lines
3.6 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { Queue as BullQueue } from 'bullmq';
|
|
import { expect, test } from 'bun:test';
|
|
import Redis from 'ioredis';
|
|
import { processBatchJob, processItems } from '../../libs/core/queue/src/batch-processor';
|
|
import { QueueManager } from '../../libs/core/queue/src/queue-manager';
|
|
import type { BatchJobData } from '../../libs/core/queue/src/types';
|
|
|
|
const redisUrl = process.env.BACKTEST_TEST_REDIS_URL;
|
|
const logger = { info() {}, debug() {}, trace() {}, warn() {}, error() {} };
|
|
|
|
test.skipIf(!redisUrl)(
|
|
'real Redis retains queued batch payloads across observer and producer shutdown',
|
|
async () => {
|
|
const url = new URL(redisUrl!);
|
|
if (url.pathname !== '/15') {
|
|
throw new Error('Dedicated Redis fixture using database 15 required');
|
|
}
|
|
const connection = {
|
|
host: url.hostname,
|
|
port: Number(url.port || 6379),
|
|
username: decodeURIComponent(url.username) || undefined,
|
|
password: decodeURIComponent(url.password) || undefined,
|
|
db: 15,
|
|
maxRetriesPerRequest: 1,
|
|
};
|
|
const queueName = `cl22_${randomUUID().replaceAll('-', '')}`;
|
|
const redis = new Redis({ ...connection, retryStrategy: () => null });
|
|
const admin = new BullQueue(queueName, { connection });
|
|
const managers: QueueManager[] = [];
|
|
const manager = () => {
|
|
// serviceName intentionally omitted: service routing forces database 0.
|
|
const value = new QueueManager(
|
|
{ redis: connection, defaultQueueOptions: { workers: 0 } },
|
|
undefined,
|
|
logger
|
|
);
|
|
managers.push(value);
|
|
return value;
|
|
};
|
|
let payloadKey: string | undefined;
|
|
try {
|
|
await redis.ping();
|
|
const producer = manager();
|
|
const observer = manager();
|
|
const items = [{ symbol: 'AAPL.US' }, { symbol: 'MSFT.US' }];
|
|
await processItems(
|
|
items,
|
|
queueName,
|
|
{
|
|
totalDelayHours: 0,
|
|
useBatching: true,
|
|
batchSize: 2,
|
|
ttl: 120,
|
|
handler: queueName,
|
|
operation: 'save',
|
|
},
|
|
producer
|
|
);
|
|
const jobs = await admin.getJobs(['waiting']);
|
|
expect(jobs).toHaveLength(1);
|
|
const batch = jobs[0]!.data.payload as BatchJobData;
|
|
payloadKey = `batch:${queueName}:${batch.payloadKey}`;
|
|
observer.getQueue(queueName);
|
|
expect(await observer.getCache(queueName).get(batch.payloadKey)).toMatchObject({ items });
|
|
await observer.shutdown();
|
|
expect(await redis.exists(payloadKey)).toBe(1);
|
|
await producer.shutdown();
|
|
expect(await redis.exists(payloadKey)).toBe(1);
|
|
expect(await redis.ttl(payloadKey)).toBeGreaterThan(0);
|
|
expect(await admin.getJob(jobs[0]!.id!)).toBeDefined();
|
|
expect(await admin.getWorkers()).toEqual([]);
|
|
|
|
const consumer = manager();
|
|
expect(await processBatchJob(batch, queueName, consumer)).toEqual({
|
|
batchIndex: 0,
|
|
itemsProcessed: 2,
|
|
jobsCreated: 2,
|
|
});
|
|
const itemJobs = (await admin.getJobs(['waiting']))
|
|
.filter(job => job.name === 'process-item')
|
|
.map(job => job.data.payload)
|
|
.sort((a, b) => a.symbol.localeCompare(b.symbol));
|
|
expect(itemJobs).toEqual(items);
|
|
expect(await redis.exists(payloadKey)).toBe(0);
|
|
} finally {
|
|
await Promise.all(managers.map(value => value.shutdown()));
|
|
// This unique queue and exact payload key belong only to this test.
|
|
try {
|
|
await admin.obliterate({ force: true });
|
|
if (payloadKey) {
|
|
await redis.del(payloadKey);
|
|
}
|
|
} finally {
|
|
await admin.close();
|
|
redis.disconnect();
|
|
}
|
|
}
|
|
}
|
|
);
|