ioredis-streams 1.3.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +8 -0
- package/README.md +75 -3
- package/coverage.js +11 -0
- package/dist/consumer-buffer.d.ts +16 -0
- package/dist/consumer-buffer.js +53 -0
- package/dist/consumer-buffer.js.map +1 -0
- package/dist/events.d.ts +15 -0
- package/dist/events.js +30 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +5 -134
- package/dist/index.js +9 -452
- package/dist/index.js.map +1 -1
- package/dist/redis-client.d.ts +32 -0
- package/dist/redis-client.js +46 -0
- package/dist/redis-client.js.map +1 -0
- package/dist/redis-streams.d.ts +19 -0
- package/dist/redis-streams.js +198 -0
- package/dist/redis-streams.js.map +1 -0
- package/dist/stream-consumer.d.ts +28 -0
- package/dist/stream-consumer.js +177 -0
- package/dist/stream-consumer.js.map +1 -0
- package/dist/types.d.ts +112 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/docs/api.md +393 -0
- package/docs/configuration.md +129 -0
- package/docs/dead-letters.md +132 -0
- package/docs/examples.md +334 -0
- package/docs/superpowers/specs/2026-04-04-refactor-design.md +60 -0
- package/package.json +27 -5
- package/src/consumer-buffer.ts +60 -0
- package/src/events.ts +35 -0
- package/src/index.ts +18 -609
- package/src/redis-client.ts +64 -0
- package/src/redis-streams.ts +249 -0
- package/src/stream-consumer.ts +241 -0
- package/src/types.ts +115 -0
- package/test/consumer-buffer.spec.ts +154 -0
- package/test/events.spec.ts +104 -0
- package/test/helpers/fake-redis.ts +60 -0
- package/test/mocha.opts +2 -1
- package/test/redis-client.spec.ts +122 -0
- package/test/redis-streams.spec.ts +494 -0
- package/test/setup.ts +3 -0
- package/test/stream-consumer.spec.ts +493 -0
- package/.nycrc +0 -20
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
import 'mocha';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { RedisStreams } from '../src/redis-streams';
|
|
4
|
+
import { StreamConsumer } from '../src/stream-consumer';
|
|
5
|
+
import { IRedisClient } from '../src/redis-client';
|
|
6
|
+
import { ConsumerConfigs, NamedEventHandler, RedisStreamsInputConfig, StreamConfigs } from '../src/types';
|
|
7
|
+
import { createFakeRedis, FakeRedis } from './helpers/fake-redis';
|
|
8
|
+
|
|
9
|
+
// ── Test helpers ──────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
class TestRedisStreams extends RedisStreams {
|
|
12
|
+
private _producer: IRedisClient;
|
|
13
|
+
private _consumers: IRedisClient[];
|
|
14
|
+
public capturedConsumer?: StreamConsumer;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
producer: IRedisClient,
|
|
18
|
+
consumers: IRedisClient[] = [],
|
|
19
|
+
config?: Partial<RedisStreamsInputConfig>,
|
|
20
|
+
) {
|
|
21
|
+
super('test-peer', config ?? { redis: { host: 'fake', port: 1 } });
|
|
22
|
+
this._producer = producer;
|
|
23
|
+
this._consumers = [...consumers];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected getProducerRedis(): IRedisClient { return this._producer; }
|
|
27
|
+
protected getConsumerRedis(): IRedisClient { return this._consumers.shift() ?? this._producer; }
|
|
28
|
+
|
|
29
|
+
protected registerConsumer(
|
|
30
|
+
streamName: string,
|
|
31
|
+
groupName: string,
|
|
32
|
+
handlers: Map<string, NamedEventHandler>,
|
|
33
|
+
config: ConsumerConfigs,
|
|
34
|
+
): StreamConsumer {
|
|
35
|
+
const consumer = super.registerConsumer(streamName, groupName, handlers, config);
|
|
36
|
+
this.capturedConsumer = consumer;
|
|
37
|
+
return consumer;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// expose buildConsumerConfigs for testing deadLetters handler
|
|
41
|
+
public buildConsumerConfigsPublic(streamName: string, groupName: string, config?: Partial<StreamConfigs>) {
|
|
42
|
+
return (this as any).buildConsumerConfigs(streamName, groupName, config);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function makeEvent(name = 'click', data: any = {}) {
|
|
47
|
+
return { name, v: '1.0.0', data, time: Date.now() };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ── buildConfig ───────────────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
describe('RedisStreams - buildConfig', () => {
|
|
53
|
+
it('uses supplied host/port config', () => {
|
|
54
|
+
const rs = new TestRedisStreams(createFakeRedis(), [], { redis: { host: 'h', port: 1234 } });
|
|
55
|
+
expect((rs as any).config.redis).to.deep.equal({ host: 'h', port: 1234 });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('parses cluster string from config', () => {
|
|
59
|
+
const rs = new TestRedisStreams(createFakeRedis(), [], { redis: { cluster: 'n1:7001,n2:7002' } });
|
|
60
|
+
expect((rs as any).config.redis).to.deep.equal([
|
|
61
|
+
{ host: 'n1', port: 7001 },
|
|
62
|
+
{ host: 'n2', port: 7002 },
|
|
63
|
+
]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('falls back to env vars when no redis config is supplied', () => {
|
|
67
|
+
const orig = { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT };
|
|
68
|
+
process.env.REDIS_HOST = 'env-host';
|
|
69
|
+
process.env.REDIS_PORT = '6379';
|
|
70
|
+
try {
|
|
71
|
+
const rs = new RedisStreams('test-peer'); // no config → reads from env vars
|
|
72
|
+
expect((rs as any).config.redis.host).to.equal('env-host');
|
|
73
|
+
} finally {
|
|
74
|
+
if (orig.host !== undefined) process.env.REDIS_HOST = orig.host; else delete process.env.REDIS_HOST;
|
|
75
|
+
if (orig.port !== undefined) process.env.REDIS_PORT = orig.port; else delete process.env.REDIS_PORT;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('stores deadLetters config when provided', () => {
|
|
80
|
+
const rs = new TestRedisStreams(createFakeRedis(), [], {
|
|
81
|
+
redis: { host: 'h', port: 1 },
|
|
82
|
+
deadLetters: { stream: 'dl', maxRetries: 3, maxSize: 1000 },
|
|
83
|
+
});
|
|
84
|
+
expect((rs as any).config.deadLetters).to.exist;
|
|
85
|
+
expect((rs as any).config.deadLetters.maxRetries).to.equal(3);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('stores clusterOptions when provided', () => {
|
|
89
|
+
const rs = new TestRedisStreams(createFakeRedis(), [], {
|
|
90
|
+
redis: { cluster: 'n1:7001' },
|
|
91
|
+
clusterOptions: { scaleReads: 'slave' },
|
|
92
|
+
});
|
|
93
|
+
expect((rs as any).config.clusterOptions).to.deep.equal({ scaleReads: 'slave' });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('uses the provided logger instead of creating a default one', () => {
|
|
97
|
+
const myLogger = { trace: () => {}, info: () => {}, error: () => {}, warn: () => {} };
|
|
98
|
+
const rs = new TestRedisStreams(createFakeRedis(), [], {
|
|
99
|
+
redis: { host: 'h', port: 1 },
|
|
100
|
+
logger: myLogger as any,
|
|
101
|
+
});
|
|
102
|
+
expect((rs as any).config.logger).to.equal(myLogger);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// ── group() / stream caching ──────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
describe('RedisStreams - group() and stream caching', () => {
|
|
109
|
+
it('returns a ConsumerGroup with a stream() factory', () => {
|
|
110
|
+
const rs = new TestRedisStreams(createFakeRedis());
|
|
111
|
+
const group = rs.group('g');
|
|
112
|
+
expect(group.stream).to.be.a('function');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('returns the same StreamGroupConsumer on repeated stream() calls', () => {
|
|
116
|
+
const rs = new TestRedisStreams(createFakeRedis());
|
|
117
|
+
const group = rs.group('g');
|
|
118
|
+
const s1 = group.stream('s');
|
|
119
|
+
const s2 = group.stream('s');
|
|
120
|
+
expect(s1).to.equal(s2);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('returns different consumers for different stream names', () => {
|
|
124
|
+
const rs = new TestRedisStreams(createFakeRedis());
|
|
125
|
+
const group = rs.group('g');
|
|
126
|
+
const s1 = group.stream('stream-a');
|
|
127
|
+
const s2 = group.stream('stream-b');
|
|
128
|
+
expect(s1).to.not.equal(s2);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ── getProducerRedis lazy caching ─────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
describe('RedisStreams - producer client lazy caching', () => {
|
|
135
|
+
it('returns the same producer instance on repeated calls', () => {
|
|
136
|
+
const fakeProducer = createFakeRedis();
|
|
137
|
+
// After overriding _producer via subclass we can verify caching:
|
|
138
|
+
const rs2 = new TestRedisStreams(fakeProducer);
|
|
139
|
+
const p1 = rs2['getProducerRedis']();
|
|
140
|
+
const p2 = rs2['getProducerRedis']();
|
|
141
|
+
expect(p1).to.equal(p2);
|
|
142
|
+
expect(p1).to.equal(fakeProducer);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('real getProducerRedis() creates and caches a new client on first call', () => {
|
|
146
|
+
const rs = new RedisStreams('peer', { redis: { host: 'localhost', port: 6399 } });
|
|
147
|
+
expect((rs as any)._producerRedis).to.be.undefined;
|
|
148
|
+
const c1 = (rs as any).getProducerRedis();
|
|
149
|
+
const c2 = (rs as any).getProducerRedis();
|
|
150
|
+
expect(c1).to.exist;
|
|
151
|
+
expect(c1).to.equal(c2);
|
|
152
|
+
c1.disconnect();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('real getConsumerRedis() creates a fresh client on each call', () => {
|
|
156
|
+
const rs = new RedisStreams('peer', { redis: { host: 'localhost', port: 6399 } });
|
|
157
|
+
const c1 = (rs as any).getConsumerRedis();
|
|
158
|
+
const c2 = (rs as any).getConsumerRedis();
|
|
159
|
+
expect(c1).to.exist;
|
|
160
|
+
c1.disconnect();
|
|
161
|
+
c2.disconnect();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// ── produce() ────────────────────────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
describe('RedisStreams - produce()', () => {
|
|
168
|
+
it('executes a pipeline for a plain event', async () => {
|
|
169
|
+
const fakeProducer = createFakeRedis();
|
|
170
|
+
const fakePipe = fakeProducer.pipeline();
|
|
171
|
+
fakeProducer.pipeline.resetHistory();
|
|
172
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
173
|
+
|
|
174
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
175
|
+
const stream = rs.group('g').stream('s');
|
|
176
|
+
await stream.produce(makeEvent());
|
|
177
|
+
expect((fakePipe as any).exec.calledOnce).to.be.true;
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('returns a wait handle for an IWaitEvent and resolves with the reply', async () => {
|
|
181
|
+
const fakeProducer = createFakeRedis();
|
|
182
|
+
const fakeConsumer = createFakeRedis();
|
|
183
|
+
|
|
184
|
+
let capturedMsgHandler: ((...a: any[]) => void) | undefined;
|
|
185
|
+
// Use withArgs('message') because sinon 9's returnsThis() takes precedence over plain callsFake()
|
|
186
|
+
fakeConsumer.on.withArgs('message').callsFake((_event: string, handler: any) => {
|
|
187
|
+
capturedMsgHandler = handler;
|
|
188
|
+
return fakeConsumer;
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// After pipeline.exec on the producer, emit the reply on the consumer
|
|
192
|
+
const fakePipe = fakeProducer.pipeline();
|
|
193
|
+
fakeProducer.pipeline.resetHistory();
|
|
194
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
195
|
+
(fakePipe as any).exec.callsFake(() => {
|
|
196
|
+
// capturedMsgHandler is set synchronously by redis.on('message', ...) before exec is called
|
|
197
|
+
if (capturedMsgHandler) capturedMsgHandler('ch', JSON.stringify({ reply: 'pong', wait: {} }));
|
|
198
|
+
return Promise.resolve([]);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// First consumer goes to registerConsumer (StreamConsumer internal), second goes to wait()
|
|
202
|
+
const rs = new TestRedisStreams(fakeProducer, [createFakeRedis(), fakeConsumer]);
|
|
203
|
+
const stream = rs.group('g').stream('s');
|
|
204
|
+
|
|
205
|
+
const waitEvent = { name: 'ping', v: '1', data: {}, time: 1, wait: { source: 'svc' } };
|
|
206
|
+
const result = await (stream.produce(waitEvent) as any).wait(5000);
|
|
207
|
+
expect(result).to.equal('pong');
|
|
208
|
+
expect(fakeConsumer.disconnect.calledOnce).to.be.true;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('cleanup skips redis.off when subscribe rejects before the message handler is attached', async () => {
|
|
212
|
+
const fakeProducer = createFakeRedis();
|
|
213
|
+
const fakeConsumer = createFakeRedis();
|
|
214
|
+
// subscribe rejects inside the async promise executor — the error is swallowed by the async
|
|
215
|
+
// function, so the outer promise times out. msgHandler is never set, so cleanup runs without it.
|
|
216
|
+
fakeConsumer.subscribe.rejects(new Error('subscribe failed'));
|
|
217
|
+
|
|
218
|
+
const fakePipe = fakeProducer.pipeline();
|
|
219
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
220
|
+
|
|
221
|
+
const rs = new TestRedisStreams(fakeProducer, [createFakeRedis(), fakeConsumer]);
|
|
222
|
+
const stream = rs.group('g').stream('s');
|
|
223
|
+
const waitEvent = { name: 'ping', v: '1', data: {}, time: 1, wait: { source: 'svc' } };
|
|
224
|
+
// use a 1ms timeout — subscribe error is swallowed; timer fires instead
|
|
225
|
+
await expect((stream.produce(waitEvent) as any).wait(1)).to.be.rejectedWith('Timeout');
|
|
226
|
+
// cleanup ran: msgHandler was undefined, so off() was NOT called
|
|
227
|
+
expect(fakeConsumer.off.called).to.be.false;
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('rejects with Timeout error when the reply does not arrive in time', async () => {
|
|
231
|
+
const fakeProducer = createFakeRedis();
|
|
232
|
+
const fakeConsumer = createFakeRedis();
|
|
233
|
+
// pipeline.exec never emits a message
|
|
234
|
+
const fakePipe = fakeProducer.pipeline();
|
|
235
|
+
fakeProducer.pipeline.resetHistory();
|
|
236
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
237
|
+
(fakePipe as any).exec.callsFake(async () => new Promise(() => {})); // never resolves
|
|
238
|
+
|
|
239
|
+
// First consumer goes to registerConsumer, second goes to wait()
|
|
240
|
+
const rs = new TestRedisStreams(fakeProducer, [createFakeRedis(), fakeConsumer]);
|
|
241
|
+
const stream = rs.group('g').stream('s');
|
|
242
|
+
const waitEvent = { name: 'ping', v: '1', data: {}, time: 1, wait: { source: 'svc' } };
|
|
243
|
+
await expect((stream.produce(waitEvent) as any).wait(1)).to.be.rejectedWith('Timeout');
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// ── produceMany() / flush() ───────────────────────────────────────────────────
|
|
248
|
+
|
|
249
|
+
describe('RedisStreams - produceMany() + flush()', () => {
|
|
250
|
+
it('batches multiple events into one pipeline and executes on flush', async () => {
|
|
251
|
+
const fakeProducer = createFakeRedis();
|
|
252
|
+
const fakeMulti = fakeProducer.multi();
|
|
253
|
+
fakeProducer.multi.resetHistory();
|
|
254
|
+
fakeProducer.multi.callsFake(() => fakeMulti);
|
|
255
|
+
|
|
256
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
257
|
+
const stream = rs.group('g').stream('s');
|
|
258
|
+
|
|
259
|
+
await stream.produceMany(makeEvent('a'), makeEvent('b')).flush();
|
|
260
|
+
expect((fakeMulti as any).exec.calledOnce).to.be.true;
|
|
261
|
+
expect((fakeMulti as any).xadd.callCount).to.equal(2);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('creates a fresh pipeline after each flush', async () => {
|
|
265
|
+
const fakeProducer = createFakeRedis();
|
|
266
|
+
const pipes: any[] = [];
|
|
267
|
+
fakeProducer.multi.callsFake(() => {
|
|
268
|
+
const p = fakeProducer.pipeline();
|
|
269
|
+
pipes.push(p);
|
|
270
|
+
return p;
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
274
|
+
const stream = rs.group('g').stream('s');
|
|
275
|
+
|
|
276
|
+
await stream.produceMany(makeEvent()).flush();
|
|
277
|
+
await stream.produceMany(makeEvent()).flush();
|
|
278
|
+
expect(pipes.length).to.be.at.least(2);
|
|
279
|
+
expect(pipes[0]).to.not.equal(pipes[1]);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('reuses the existing pipeline when produceMany is called again before flush', () => {
|
|
283
|
+
const fakeProducer = createFakeRedis();
|
|
284
|
+
const fakeMulti = fakeProducer.multi();
|
|
285
|
+
fakeProducer.multi.resetHistory();
|
|
286
|
+
fakeProducer.multi.callsFake(() => fakeMulti);
|
|
287
|
+
|
|
288
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
289
|
+
const stream = rs.group('g').stream('s');
|
|
290
|
+
|
|
291
|
+
// Call produceMany twice without flushing — _currentPipeline already set on second call
|
|
292
|
+
stream.produceMany(makeEvent('a'));
|
|
293
|
+
stream.produceMany(makeEvent('b'));
|
|
294
|
+
|
|
295
|
+
// multi() should only have been created once
|
|
296
|
+
expect(fakeProducer.multi.callCount).to.equal(1);
|
|
297
|
+
expect((fakeMulti as any).xadd.callCount).to.equal(2);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('each stream uses its own isolated pipeline', async () => {
|
|
301
|
+
const fakeProducer = createFakeRedis();
|
|
302
|
+
const pipes: any[] = [];
|
|
303
|
+
fakeProducer.multi.callsFake(() => {
|
|
304
|
+
const p = fakeProducer.pipeline();
|
|
305
|
+
pipes.push(p);
|
|
306
|
+
return p;
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
310
|
+
const streamA = rs.group('g').stream('stream-a');
|
|
311
|
+
const streamB = rs.group('g').stream('stream-b');
|
|
312
|
+
|
|
313
|
+
streamA.produceMany(makeEvent('a'));
|
|
314
|
+
streamB.produceMany(makeEvent('b'));
|
|
315
|
+
|
|
316
|
+
expect(pipes.length).to.equal(2);
|
|
317
|
+
expect(pipes[0]).to.not.equal(pipes[1]);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// ── handle() / event dispatch ─────────────────────────────────────────────────
|
|
322
|
+
|
|
323
|
+
describe('RedisStreams - handle() and event dispatch', () => {
|
|
324
|
+
it('dispatches to the specific named handler', async () => {
|
|
325
|
+
const rs = new TestRedisStreams(createFakeRedis());
|
|
326
|
+
const stream = rs.group('g').stream('s');
|
|
327
|
+
|
|
328
|
+
let called = false;
|
|
329
|
+
stream.handle('myEvent', async () => { called = true; });
|
|
330
|
+
|
|
331
|
+
const ev = { name: 'myEvent', v: '1', data: {}, time: 1 };
|
|
332
|
+
await (rs.capturedConsumer as any).processMessage('id-1', ['myEvent', JSON.stringify(ev)]);
|
|
333
|
+
expect(called).to.be.true;
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('falls back to the "*" wildcard handler when no specific handler matches', async () => {
|
|
337
|
+
const rs = new TestRedisStreams(createFakeRedis());
|
|
338
|
+
const stream = rs.group('g').stream('s');
|
|
339
|
+
|
|
340
|
+
let wildcardCalled = false;
|
|
341
|
+
stream.handle('*', async () => { wildcardCalled = true; });
|
|
342
|
+
|
|
343
|
+
const ev = { name: 'unknownEvent', v: '1', data: {}, time: 1 };
|
|
344
|
+
await (rs.capturedConsumer as any).processMessage('id-1', ['unknownEvent', JSON.stringify(ev)]);
|
|
345
|
+
expect(wildcardCalled).to.be.true;
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('returns undefined silently when no handler matches', async () => {
|
|
349
|
+
const rs = new TestRedisStreams(createFakeRedis());
|
|
350
|
+
rs.group('g').stream('s'); // register no handlers
|
|
351
|
+
|
|
352
|
+
const ev = { name: 'unhandled', v: '1', data: {}, time: 1 };
|
|
353
|
+
const result = await (rs.capturedConsumer as any).processMessage('id-1', ['unhandled', JSON.stringify(ev)]);
|
|
354
|
+
expect(result).to.be.undefined;
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// ── consume() / continue() ────────────────────────────────────────────────────
|
|
359
|
+
|
|
360
|
+
describe('RedisStreams - consume() and continue()', () => {
|
|
361
|
+
it('initializes the consumer group and returns stop/continue handles', async () => {
|
|
362
|
+
const fakeConsumer = createFakeRedis();
|
|
363
|
+
const rs = new TestRedisStreams(createFakeRedis(), [fakeConsumer]);
|
|
364
|
+
const stream = rs.group('g').stream('s');
|
|
365
|
+
|
|
366
|
+
let unblock!: () => void;
|
|
367
|
+
fakeConsumer.xpending.resolves([]);
|
|
368
|
+
fakeConsumer.xreadgroup.callsFake(async () => {
|
|
369
|
+
await new Promise<void>(r => { unblock = r; });
|
|
370
|
+
return null;
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const { stop, continue: cont } = await stream.consume();
|
|
374
|
+
expect(fakeConsumer.xgroup.calledOnce).to.be.true;
|
|
375
|
+
expect(stop).to.be.a('function');
|
|
376
|
+
expect(cont).to.be.a('function');
|
|
377
|
+
|
|
378
|
+
// let the (fire-and-forget) start() reach the blocked xreadgroup call and assign `unblock`
|
|
379
|
+
await new Promise(r => setTimeout(r, 0));
|
|
380
|
+
|
|
381
|
+
// stop() now waits for the in-flight (blocked) read to finish before resolving
|
|
382
|
+
const stopping = stop();
|
|
383
|
+
unblock();
|
|
384
|
+
await stopping;
|
|
385
|
+
|
|
386
|
+
expect(fakeConsumer.xgroup.calledWith('DELCONSUMER')).to.be.true;
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it('continue() calls start() again on the consumer', async () => {
|
|
390
|
+
const fakeConsumer = createFakeRedis();
|
|
391
|
+
const rs = new TestRedisStreams(createFakeRedis(), [fakeConsumer]);
|
|
392
|
+
const stream = rs.group('g').stream('s');
|
|
393
|
+
|
|
394
|
+
let unblock!: () => void;
|
|
395
|
+
fakeConsumer.xpending.resolves([]);
|
|
396
|
+
fakeConsumer.xreadgroup.callsFake(async () => {
|
|
397
|
+
await new Promise<void>(r => { unblock = r; });
|
|
398
|
+
return null;
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
const { stop, continue: cont } = await stream.consume();
|
|
402
|
+
// let the (fire-and-forget) start() reach the blocked xreadgroup call and assign `unblock`
|
|
403
|
+
await new Promise(r => setTimeout(r, 0));
|
|
404
|
+
const stopping = stop();
|
|
405
|
+
unblock();
|
|
406
|
+
await stopping;
|
|
407
|
+
// after stop, calling continue re-invokes start (exits immediately since disposing=true)
|
|
408
|
+
cont();
|
|
409
|
+
const createCalls = fakeConsumer.xgroup.getCalls().filter((c: any) => c.args[0] === 'CREATE');
|
|
410
|
+
expect(createCalls.length).to.equal(1); // only one init
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// ── with() ────────────────────────────────────────────────────────────────────
|
|
415
|
+
|
|
416
|
+
describe('RedisStreams - with()', () => {
|
|
417
|
+
it('wraps event factories to call stream.produce', async () => {
|
|
418
|
+
const fakeProducer = createFakeRedis();
|
|
419
|
+
const fakePipe = fakeProducer.pipeline();
|
|
420
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
421
|
+
|
|
422
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
423
|
+
const stream = rs.group('g').stream('s').with({
|
|
424
|
+
ping: (msg: string) => makeEvent('ping', { msg }),
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
await (stream as any).ping('hello');
|
|
428
|
+
expect((fakePipe as any).exec.calledOnce).to.be.true;
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// ── doProduce() edge cases ────────────────────────────────────────────────────
|
|
433
|
+
|
|
434
|
+
describe('RedisStreams - doProduce()', () => {
|
|
435
|
+
it('returns the pipeline unchanged when events array is empty', async () => {
|
|
436
|
+
const fakeProducer = createFakeRedis();
|
|
437
|
+
const fakePipe = fakeProducer.pipeline();
|
|
438
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
439
|
+
|
|
440
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
441
|
+
const stream = rs.group('g').stream('s');
|
|
442
|
+
// produceMany with no events — calls doProduce with empty list
|
|
443
|
+
await stream.produceMany().flush();
|
|
444
|
+
expect((fakePipe as any).xadd.called).to.be.false;
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it('calls xadd without MAXLEN when maxLen is undefined', () => {
|
|
448
|
+
const fakeProducer = createFakeRedis();
|
|
449
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
450
|
+
const stream = rs.group('g').stream('s', { maxLen: undefined as any });
|
|
451
|
+
const fakePipe = fakeProducer.pipeline();
|
|
452
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
453
|
+
(rs as any).doProduce(fakePipe, 'my-stream', undefined, makeEvent('e'));
|
|
454
|
+
const xaddArgs = (fakePipe as any).xadd.firstCall.args;
|
|
455
|
+
expect(xaddArgs[0]).to.equal('my-stream');
|
|
456
|
+
expect(xaddArgs[1]).to.equal('*');
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it('calls xadd with MAXLEN ~ when maxLen is a number', () => {
|
|
460
|
+
const fakeProducer = createFakeRedis();
|
|
461
|
+
const rs = new TestRedisStreams(fakeProducer);
|
|
462
|
+
const fakePipe = fakeProducer.pipeline();
|
|
463
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
464
|
+
(rs as any).doProduce(fakePipe, 'my-stream', 5000, makeEvent('e'));
|
|
465
|
+
const xaddArgs = (fakePipe as any).xadd.firstCall.args;
|
|
466
|
+
expect(xaddArgs[1]).to.equal('MAXLEN');
|
|
467
|
+
expect(xaddArgs[2]).to.equal('~');
|
|
468
|
+
expect(xaddArgs[3]).to.equal('5000');
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
// ── deadLetters handler ───────────────────────────────────────────────────────
|
|
473
|
+
|
|
474
|
+
describe('RedisStreams - buildConsumerConfigs deadLetters.handler', () => {
|
|
475
|
+
it('publishes dead-letter events to the configured stream via the producer pipeline', async () => {
|
|
476
|
+
const fakeProducer = createFakeRedis();
|
|
477
|
+
const fakePipe = fakeProducer.pipeline();
|
|
478
|
+
fakeProducer.pipeline.callsFake(() => fakePipe);
|
|
479
|
+
|
|
480
|
+
const rs = new TestRedisStreams(fakeProducer, [], {
|
|
481
|
+
redis: { host: 'h', port: 1 },
|
|
482
|
+
deadLetters: { stream: 'dead-letters', maxRetries: 3, maxSize: 1000 },
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
const configs = rs.buildConsumerConfigsPublic('s', 'g');
|
|
486
|
+
await configs.deadLetters!.handler({ name: 'deadMessage', v: '1', data: {} as any, time: 1 });
|
|
487
|
+
|
|
488
|
+
expect((fakePipe as any).exec.calledOnce).to.be.true;
|
|
489
|
+
expect((fakePipe as any).xadd.calledOnce).to.be.true;
|
|
490
|
+
// xadd was called for the dead-letters stream
|
|
491
|
+
const xaddArgs = (fakePipe as any).xadd.firstCall.args;
|
|
492
|
+
expect(xaddArgs[0]).to.equal('dead-letters');
|
|
493
|
+
});
|
|
494
|
+
});
|
package/test/setup.ts
ADDED