-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqueue-service.test.ts
More file actions
121 lines (103 loc) · 3.88 KB
/
Copy pathqueue-service.test.ts
File metadata and controls
121 lines (103 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { describe, it, expect } from 'vitest';
import type { IQueueService, QueueMessage, QueueHandler } from './queue-service';
describe('Queue Service Contract', () => {
it('should allow a minimal IQueueService implementation with required methods', () => {
const queue: IQueueService = {
publish: async (_queue, _data, _options?) => 'msg-1',
subscribe: async (_queue, _handler) => {},
unsubscribe: async (_queue) => {},
};
expect(typeof queue.publish).toBe('function');
expect(typeof queue.subscribe).toBe('function');
expect(typeof queue.unsubscribe).toBe('function');
});
it('should allow a full implementation with optional methods', () => {
const queue: IQueueService = {
publish: async () => 'msg-1',
subscribe: async () => {},
unsubscribe: async () => {},
getQueueSize: async (_queue) => 0,
purge: async (_queue) => {},
};
expect(queue.getQueueSize).toBeDefined();
expect(queue.purge).toBeDefined();
});
it('should publish and subscribe to messages', async () => {
const queues = new Map<string, QueueMessage[]>();
const handlers = new Map<string, QueueHandler>();
let msgCounter = 0;
const queue: IQueueService = {
publish: async (name, data, _options?) => {
const id = `msg-${++msgCounter}`;
const msg: QueueMessage = {
id,
data,
attempts: 0,
timestamp: Date.now(),
};
if (!queues.has(name)) queues.set(name, []);
queues.get(name)!.push(msg);
// Process immediately if handler registered
const handler = handlers.get(name);
if (handler) await handler(msg);
return id;
},
subscribe: async (name, handler) => {
handlers.set(name, handler as QueueHandler);
},
unsubscribe: async (name) => {
handlers.delete(name);
},
};
const received: QueueMessage[] = [];
await queue.subscribe('orders', async (msg) => {
received.push(msg);
});
const id = await queue.publish('orders', { orderId: 123, total: 99.99 });
expect(id).toBe('msg-1');
expect(received).toHaveLength(1);
expect(received[0].data).toEqual({ orderId: 123, total: 99.99 });
});
it('should unsubscribe from a queue', async () => {
const handlers = new Map<string, QueueHandler>();
const queue: IQueueService = {
publish: async (name, data) => {
const handler = handlers.get(name);
if (handler) await handler({ id: '1', data, attempts: 0, timestamp: Date.now() });
return '1';
},
subscribe: async (name, handler) => {
handlers.set(name, handler as QueueHandler);
},
unsubscribe: async (name) => {
handlers.delete(name);
},
};
const received: unknown[] = [];
await queue.subscribe('events', async (msg) => { received.push(msg.data); });
await queue.publish('events', 'hello');
expect(received).toHaveLength(1);
await queue.unsubscribe('events');
await queue.publish('events', 'world');
expect(received).toHaveLength(1); // No new messages after unsubscribe
});
it('should support getQueueSize and purge', async () => {
const messages = new Map<string, unknown[]>();
const queue: IQueueService = {
publish: async (name, data) => {
if (!messages.has(name)) messages.set(name, []);
messages.get(name)!.push(data);
return `msg-${messages.get(name)!.length}`;
},
subscribe: async () => {},
unsubscribe: async () => {},
getQueueSize: async (name) => messages.get(name)?.length ?? 0,
purge: async (name) => { messages.set(name, []); },
};
await queue.publish('jobs', { type: 'email' });
await queue.publish('jobs', { type: 'sms' });
expect(await queue.getQueueSize!('jobs')).toBe(2);
await queue.purge!('jobs');
expect(await queue.getQueueSize!('jobs')).toBe(0);
});
});