bunsane 0.2.8 → 0.2.10
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.md +26 -0
- package/core/App.ts +97 -0
- package/core/remote/CircuitBreaker.ts +115 -0
- package/core/remote/OutboxWorker.ts +176 -0
- package/core/remote/RemoteManager.ts +400 -0
- package/core/remote/RpcCaller.ts +310 -0
- package/core/remote/StreamConsumer.ts +535 -0
- package/core/remote/decorators.ts +121 -0
- package/core/remote/health.ts +139 -0
- package/core/remote/index.ts +37 -0
- package/core/remote/metrics.ts +99 -0
- package/core/remote/outboxSchema.ts +41 -0
- package/core/remote/types.ts +151 -0
- package/core/scheduler/DistributedLock.ts +309 -266
- package/docs/SCALABILITY_PLAN.md +3 -3
- package/package.json +1 -1
- package/query/FilterBuilder.ts +25 -0
- package/query/Query.ts +5 -1
- package/query/builders/JsonbArrayBuilder.ts +116 -0
- package/query/index.ts +28 -2
- package/tests/helpers/MockRedisClient.ts +113 -0
- package/tests/helpers/MockRedisStreamServer.ts +448 -0
- package/tests/integration/query/Query.exec.test.ts +67 -14
- package/tests/integration/query/Query.jsonbArray.test.ts +214 -0
- package/tests/integration/remote/dlq.test.ts +175 -0
- package/tests/integration/remote/event-dispatch.test.ts +114 -0
- package/tests/integration/remote/outbox.test.ts +130 -0
- package/tests/integration/remote/rpc.test.ts +177 -0
- package/tests/pglite-setup.ts +1 -0
- package/tests/unit/query/JsonbArrayBuilder.test.ts +178 -0
- package/tests/unit/remote/CircuitBreaker.test.ts +159 -0
- package/tests/unit/remote/RemoteError.test.ts +55 -0
- package/tests/unit/remote/decorators.test.ts +195 -0
- package/tests/unit/remote/metrics.test.ts +115 -0
- package/tests/unit/remote/mockRedisStreamServer.test.ts +104 -0
|
@@ -16,16 +16,28 @@ describe('Query Execution', () => {
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
describe('basic query execution', () => {
|
|
19
|
-
test('exec() returns entities with specified component', async () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
test('exec() returns only entities with specified component', async () => {
|
|
20
|
+
// Positive case: entity WITH TestUser component
|
|
21
|
+
const withUser = ctx.tracker.create();
|
|
22
|
+
withUser.add(TestUser, { name: 'QueryTest', email: 'query@example.com', age: 30 });
|
|
23
|
+
await withUser.save();
|
|
24
|
+
|
|
25
|
+
// Negative case: entity WITHOUT TestUser component (only has TestProduct)
|
|
26
|
+
const withoutUser = ctx.tracker.create();
|
|
27
|
+
withoutUser.add(TestProduct, { sku: 'NO_USER', name: 'No User Product', price: 10, inStock: true });
|
|
28
|
+
await withoutUser.save();
|
|
23
29
|
|
|
24
30
|
const results = await new Query()
|
|
25
31
|
.with(TestUser)
|
|
26
32
|
.exec();
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
// Positive: entity with TestUser should be found
|
|
35
|
+
const foundWithUser = results.some(e => e.id === withUser.id);
|
|
36
|
+
expect(foundWithUser).toBe(true);
|
|
37
|
+
|
|
38
|
+
// Negative: entity without TestUser should NOT be found
|
|
39
|
+
const foundWithoutUser = results.some(e => e.id === withoutUser.id);
|
|
40
|
+
expect(foundWithoutUser).toBe(false);
|
|
29
41
|
});
|
|
30
42
|
|
|
31
43
|
test('populate() loads all component data', async () => {
|
|
@@ -282,11 +294,17 @@ describe('Query Execution', () => {
|
|
|
282
294
|
});
|
|
283
295
|
|
|
284
296
|
describe('multiple components', () => {
|
|
285
|
-
test('with() multiple components finds entities with
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
297
|
+
test('with() multiple components finds only entities with ALL components', async () => {
|
|
298
|
+
// Positive case: entity with BOTH TestUser AND TestProduct
|
|
299
|
+
const withBoth = ctx.tracker.create();
|
|
300
|
+
withBoth.add(TestUser, { name: 'MultiComp', email: 'multi@example.com', age: 30 });
|
|
301
|
+
withBoth.add(TestProduct, { sku: 'MULTI', name: 'Multi Product', price: 50, inStock: true });
|
|
302
|
+
await withBoth.save();
|
|
303
|
+
|
|
304
|
+
// Negative case: entity with ONLY TestUser (missing TestProduct)
|
|
305
|
+
const withOnlyUser = ctx.tracker.create();
|
|
306
|
+
withOnlyUser.add(TestUser, { name: 'OnlyUser', email: 'onlyuser@example.com', age: 25 });
|
|
307
|
+
await withOnlyUser.save();
|
|
290
308
|
|
|
291
309
|
const results = await new Query()
|
|
292
310
|
.with(TestUser)
|
|
@@ -294,10 +312,15 @@ describe('Query Execution', () => {
|
|
|
294
312
|
.populate()
|
|
295
313
|
.exec();
|
|
296
314
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
expect(
|
|
300
|
-
expect(
|
|
315
|
+
// Positive: entity with both components should be found
|
|
316
|
+
const foundWithBoth = results.find(e => e.id === withBoth.id);
|
|
317
|
+
expect(foundWithBoth).toBeDefined();
|
|
318
|
+
expect(foundWithBoth?.getInMemory(TestUser)).toBeDefined();
|
|
319
|
+
expect(foundWithBoth?.getInMemory(TestProduct)).toBeDefined();
|
|
320
|
+
|
|
321
|
+
// Negative: entity with only one component should NOT be found
|
|
322
|
+
const foundWithOnlyUser = results.some(e => e.id === withOnlyUser.id);
|
|
323
|
+
expect(foundWithOnlyUser).toBe(false);
|
|
301
324
|
});
|
|
302
325
|
|
|
303
326
|
test('without() excludes entities with component', async () => {
|
|
@@ -318,6 +341,36 @@ describe('Query Execution', () => {
|
|
|
318
341
|
const hasWithProduct = results.some(e => e.id === withProduct.id);
|
|
319
342
|
expect(hasWithProduct).toBe(false);
|
|
320
343
|
});
|
|
344
|
+
|
|
345
|
+
test('with() 3+ components without filters finds entities with ALL components', async () => {
|
|
346
|
+
// Positive case: entity with ALL 3 components
|
|
347
|
+
const withAll = ctx.tracker.create();
|
|
348
|
+
withAll.add(TestUser, { name: 'AllThree', email: 'allthree@example.com', age: 30 });
|
|
349
|
+
withAll.add(TestProduct, { sku: 'ALL3', name: 'All Three Product', price: 100, inStock: true });
|
|
350
|
+
withAll.add(TestOrder, { orderNumber: 'ORD-ALL3', total: 100, status: 'pending' });
|
|
351
|
+
await withAll.save();
|
|
352
|
+
|
|
353
|
+
// Negative case: entity with only 2 of 3 components (missing TestOrder)
|
|
354
|
+
const withTwo = ctx.tracker.create();
|
|
355
|
+
withTwo.add(TestUser, { name: 'TwoOnly', email: 'twoonly@example.com', age: 25 });
|
|
356
|
+
withTwo.add(TestProduct, { sku: 'TWO', name: 'Two Only Product', price: 50, inStock: true });
|
|
357
|
+
await withTwo.save();
|
|
358
|
+
|
|
359
|
+
// Query for entities with all 3 components - NO FILTERS
|
|
360
|
+
const results = await new Query()
|
|
361
|
+
.with(TestUser)
|
|
362
|
+
.with(TestProduct)
|
|
363
|
+
.with(TestOrder)
|
|
364
|
+
.exec();
|
|
365
|
+
|
|
366
|
+
// Positive: entity with all 3 components should be found
|
|
367
|
+
const foundWithAll = results.some(e => e.id === withAll.id);
|
|
368
|
+
expect(foundWithAll).toBe(true);
|
|
369
|
+
|
|
370
|
+
// Negative: entity with only 2 components should NOT be found
|
|
371
|
+
const foundWithTwo = results.some(e => e.id === withTwo.id);
|
|
372
|
+
expect(foundWithTwo).toBe(false);
|
|
373
|
+
});
|
|
321
374
|
});
|
|
322
375
|
|
|
323
376
|
describe('excludeEntityId()', () => {
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for JSONB Array Query Operators
|
|
3
|
+
* Tests CONTAINS (@>), CONTAINED_BY (<@), HAS_ANY (?|), HAS_ALL (?&)
|
|
4
|
+
*/
|
|
5
|
+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test';
|
|
6
|
+
import { Query, FilterOp } from '../../../query/Query';
|
|
7
|
+
import { BaseComponent } from '../../../core/components/BaseComponent';
|
|
8
|
+
import { Component, CompData } from '../../../core/components/Decorators';
|
|
9
|
+
import { createTestContext, ensureComponentsRegistered } from '../../utils';
|
|
10
|
+
|
|
11
|
+
@Component
|
|
12
|
+
class TaggedItem extends BaseComponent {
|
|
13
|
+
@CompData({ indexed: true, arrayOf: String })
|
|
14
|
+
tags: string[] = [];
|
|
15
|
+
|
|
16
|
+
@CompData({ indexed: true })
|
|
17
|
+
name: string = '';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Component
|
|
21
|
+
class CategoryItem extends BaseComponent {
|
|
22
|
+
@CompData({ indexed: true })
|
|
23
|
+
title: string = '';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const isPGlite = process.env.USE_PGLITE === 'true';
|
|
27
|
+
|
|
28
|
+
describe('JSONB Array Query Operators', () => {
|
|
29
|
+
const ctx = createTestContext();
|
|
30
|
+
|
|
31
|
+
beforeAll(async () => {
|
|
32
|
+
await ensureComponentsRegistered(TaggedItem, CategoryItem);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
beforeEach(async () => {
|
|
36
|
+
// entity1: tags = ["red", "blue"]
|
|
37
|
+
const e1 = ctx.tracker.create();
|
|
38
|
+
e1.add(TaggedItem, { tags: ['red', 'blue'], name: 'item1' });
|
|
39
|
+
await e1.save();
|
|
40
|
+
|
|
41
|
+
// entity2: tags = ["blue", "green"]
|
|
42
|
+
const e2 = ctx.tracker.create();
|
|
43
|
+
e2.add(TaggedItem, { tags: ['blue', 'green'], name: 'item2' });
|
|
44
|
+
await e2.save();
|
|
45
|
+
|
|
46
|
+
// entity3: tags = ["red", "green", "blue"]
|
|
47
|
+
const e3 = ctx.tracker.create();
|
|
48
|
+
e3.add(TaggedItem, { tags: ['red', 'green', 'blue'], name: 'item3' });
|
|
49
|
+
await e3.save();
|
|
50
|
+
|
|
51
|
+
// entity4: tags = ["yellow"]
|
|
52
|
+
const e4 = ctx.tracker.create();
|
|
53
|
+
e4.add(TaggedItem, { tags: ['yellow'], name: 'item4' });
|
|
54
|
+
await e4.save();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('CONTAINS (@>)', () => {
|
|
58
|
+
test('finds entities where array contains a single value', async () => {
|
|
59
|
+
const results = await new Query()
|
|
60
|
+
.with(TaggedItem, {
|
|
61
|
+
filters: [Query.filter('tags', FilterOp.CONTAINS, 'red')]
|
|
62
|
+
})
|
|
63
|
+
.exec();
|
|
64
|
+
|
|
65
|
+
// item1 (red,blue) and item3 (red,green,blue)
|
|
66
|
+
expect(results.length).toBe(2);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('finds entities where array contains multiple values (AND)', async () => {
|
|
70
|
+
const results = await new Query()
|
|
71
|
+
.with(TaggedItem, {
|
|
72
|
+
filters: [Query.filter('tags', FilterOp.CONTAINS, ['red', 'blue'])]
|
|
73
|
+
})
|
|
74
|
+
.exec();
|
|
75
|
+
|
|
76
|
+
// item1 (red,blue) and item3 (red,green,blue) both have red AND blue
|
|
77
|
+
expect(results.length).toBe(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('returns empty when no match', async () => {
|
|
81
|
+
const results = await new Query()
|
|
82
|
+
.with(TaggedItem, {
|
|
83
|
+
filters: [Query.filter('tags', FilterOp.CONTAINS, 'purple')]
|
|
84
|
+
})
|
|
85
|
+
.exec();
|
|
86
|
+
|
|
87
|
+
expect(results.length).toBe(0);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('single element array matches', async () => {
|
|
91
|
+
const results = await new Query()
|
|
92
|
+
.with(TaggedItem, {
|
|
93
|
+
filters: [Query.filter('tags', FilterOp.CONTAINS, 'yellow')]
|
|
94
|
+
})
|
|
95
|
+
.exec();
|
|
96
|
+
|
|
97
|
+
expect(results.length).toBe(1);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('combined with other filters', async () => {
|
|
101
|
+
const results = await new Query()
|
|
102
|
+
.with(TaggedItem, {
|
|
103
|
+
filters: [
|
|
104
|
+
Query.filter('tags', FilterOp.CONTAINS, 'red'),
|
|
105
|
+
Query.filter('name', FilterOp.EQ, 'item1'),
|
|
106
|
+
]
|
|
107
|
+
})
|
|
108
|
+
.exec();
|
|
109
|
+
|
|
110
|
+
expect(results.length).toBe(1);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('CONTAINED_BY (<@)', () => {
|
|
115
|
+
test('finds entities whose array is a subset of given values', async () => {
|
|
116
|
+
const results = await new Query()
|
|
117
|
+
.with(TaggedItem, {
|
|
118
|
+
filters: [Query.filter('tags', FilterOp.CONTAINED_BY, ['red', 'blue'])]
|
|
119
|
+
})
|
|
120
|
+
.exec();
|
|
121
|
+
|
|
122
|
+
// Only item1 (red,blue) is a subset of [red,blue]
|
|
123
|
+
expect(results.length).toBe(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('superset input matches all subsets', async () => {
|
|
127
|
+
const results = await new Query()
|
|
128
|
+
.with(TaggedItem, {
|
|
129
|
+
filters: [Query.filter('tags', FilterOp.CONTAINED_BY, ['red', 'blue', 'green', 'yellow'])]
|
|
130
|
+
})
|
|
131
|
+
.exec();
|
|
132
|
+
|
|
133
|
+
// All 4 entities are subsets
|
|
134
|
+
expect(results.length).toBe(4);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe.skipIf(isPGlite)('HAS_ANY (?|)', () => {
|
|
139
|
+
test('finds entities with any of the given values', async () => {
|
|
140
|
+
const results = await new Query()
|
|
141
|
+
.with(TaggedItem, {
|
|
142
|
+
filters: [Query.filter('tags', FilterOp.HAS_ANY, ['red', 'yellow'])]
|
|
143
|
+
})
|
|
144
|
+
.exec();
|
|
145
|
+
|
|
146
|
+
// item1 (red), item3 (red), item4 (yellow)
|
|
147
|
+
expect(results.length).toBe(3);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('single value works', async () => {
|
|
151
|
+
const results = await new Query()
|
|
152
|
+
.with(TaggedItem, {
|
|
153
|
+
filters: [Query.filter('tags', FilterOp.HAS_ANY, ['yellow'])]
|
|
154
|
+
})
|
|
155
|
+
.exec();
|
|
156
|
+
|
|
157
|
+
expect(results.length).toBe(1);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe.skipIf(isPGlite)('HAS_ALL (?&)', () => {
|
|
162
|
+
test('finds entities with all of the given values', async () => {
|
|
163
|
+
const results = await new Query()
|
|
164
|
+
.with(TaggedItem, {
|
|
165
|
+
filters: [Query.filter('tags', FilterOp.HAS_ALL, ['red', 'green'])]
|
|
166
|
+
})
|
|
167
|
+
.exec();
|
|
168
|
+
|
|
169
|
+
// Only item3 (red,green,blue) has both red AND green
|
|
170
|
+
expect(results.length).toBe(1);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('single value matches all entities containing it', async () => {
|
|
174
|
+
const results = await new Query()
|
|
175
|
+
.with(TaggedItem, {
|
|
176
|
+
filters: [Query.filter('tags', FilterOp.HAS_ALL, ['blue'])]
|
|
177
|
+
})
|
|
178
|
+
.exec();
|
|
179
|
+
|
|
180
|
+
// item1, item2, item3 all have blue
|
|
181
|
+
expect(results.length).toBe(3);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe('multi-component INTERSECT compatibility', () => {
|
|
186
|
+
test('CONTAINS works with multiple .with() components', async () => {
|
|
187
|
+
// Add a second component to one entity
|
|
188
|
+
const e = ctx.tracker.create();
|
|
189
|
+
e.add(TaggedItem, { tags: ['special'], name: 'multi' });
|
|
190
|
+
e.add(CategoryItem, { title: 'test-category' });
|
|
191
|
+
await e.save();
|
|
192
|
+
|
|
193
|
+
const results = await new Query()
|
|
194
|
+
.with(TaggedItem, {
|
|
195
|
+
filters: [Query.filter('tags', FilterOp.CONTAINS, 'special')]
|
|
196
|
+
})
|
|
197
|
+
.with(CategoryItem)
|
|
198
|
+
.exec();
|
|
199
|
+
|
|
200
|
+
expect(results.length).toBe(1);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe('validation', () => {
|
|
205
|
+
test('rejects null value via validator', () => {
|
|
206
|
+
expect(() => {
|
|
207
|
+
new Query()
|
|
208
|
+
.with(TaggedItem, {
|
|
209
|
+
filters: [Query.filter('tags', FilterOp.CONTAINS, null)]
|
|
210
|
+
});
|
|
211
|
+
}).not.toThrow(); // Filter creation succeeds, validation happens at exec time
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { RemoteManager } from "../../../core/remote/RemoteManager";
|
|
3
|
+
import { MockRedisStreamServer } from "../../helpers/MockRedisStreamServer";
|
|
4
|
+
import { createMockRedisFactory } from "../../helpers/MockRedisClient";
|
|
5
|
+
|
|
6
|
+
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
describe("Dead Letter Queue", () => {
|
|
9
|
+
let server: MockRedisStreamServer;
|
|
10
|
+
let producer: RemoteManager;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
server = new MockRedisStreamServer();
|
|
14
|
+
producer = new RemoteManager({
|
|
15
|
+
appName: "prod",
|
|
16
|
+
redisFactory: createMockRedisFactory(server),
|
|
17
|
+
blockMs: 30,
|
|
18
|
+
autoClaimIdleMs: 0,
|
|
19
|
+
shutdownDrainMs: 100,
|
|
20
|
+
});
|
|
21
|
+
await producer.start();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(async () => {
|
|
25
|
+
await producer.shutdown();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("poison message routed to DLQ after second delivery", async () => {
|
|
29
|
+
// Consumer 1: handler fails → message stays in PEL with deliveryCount=1
|
|
30
|
+
const c1 = new RemoteManager({
|
|
31
|
+
appName: "cons",
|
|
32
|
+
redisFactory: createMockRedisFactory(server),
|
|
33
|
+
blockMs: 30,
|
|
34
|
+
autoClaimIdleMs: 0,
|
|
35
|
+
dlqMaxDeliveries: 2,
|
|
36
|
+
shutdownDrainMs: 100,
|
|
37
|
+
});
|
|
38
|
+
await c1.start();
|
|
39
|
+
c1.on(
|
|
40
|
+
"poison",
|
|
41
|
+
async () => {
|
|
42
|
+
throw new Error("always fails");
|
|
43
|
+
},
|
|
44
|
+
"h1"
|
|
45
|
+
);
|
|
46
|
+
await producer.emit("cons", "poison", { bad: true });
|
|
47
|
+
await wait(200);
|
|
48
|
+
expect(server.getPelSize("remote:cons", "cons")).toBe(1);
|
|
49
|
+
await c1.shutdown();
|
|
50
|
+
|
|
51
|
+
// Consumer 2: autoClaimIdleMs > 0 triggers XAUTOCLAIM on startup →
|
|
52
|
+
// claims the orphan, deliveryCount becomes 2 → DLQ check fires.
|
|
53
|
+
const c2 = new RemoteManager({
|
|
54
|
+
appName: "cons",
|
|
55
|
+
redisFactory: createMockRedisFactory(server),
|
|
56
|
+
blockMs: 30,
|
|
57
|
+
autoClaimIdleMs: 1,
|
|
58
|
+
dlqMaxDeliveries: 2,
|
|
59
|
+
shutdownDrainMs: 100,
|
|
60
|
+
enableLogging: true,
|
|
61
|
+
});
|
|
62
|
+
await c2.start();
|
|
63
|
+
c2.on(
|
|
64
|
+
"poison",
|
|
65
|
+
async () => {
|
|
66
|
+
throw new Error("still fails");
|
|
67
|
+
},
|
|
68
|
+
"h1"
|
|
69
|
+
);
|
|
70
|
+
await wait(300);
|
|
71
|
+
|
|
72
|
+
expect(server.getStreamLength("remote:cons:dlq")).toBe(1);
|
|
73
|
+
const snap = c2.getMetrics();
|
|
74
|
+
expect(snap.events.dlq).toBe(1);
|
|
75
|
+
|
|
76
|
+
await c2.shutdown();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("dlqMaxDeliveries=0 disables DLQ routing", async () => {
|
|
80
|
+
const c1 = new RemoteManager({
|
|
81
|
+
appName: "cons2",
|
|
82
|
+
redisFactory: createMockRedisFactory(server),
|
|
83
|
+
blockMs: 30,
|
|
84
|
+
autoClaimIdleMs: 0,
|
|
85
|
+
dlqMaxDeliveries: 0,
|
|
86
|
+
shutdownDrainMs: 100,
|
|
87
|
+
});
|
|
88
|
+
await c1.start();
|
|
89
|
+
c1.on(
|
|
90
|
+
"fail",
|
|
91
|
+
async () => {
|
|
92
|
+
throw new Error("x");
|
|
93
|
+
},
|
|
94
|
+
"h1"
|
|
95
|
+
);
|
|
96
|
+
await producer.emit("cons2", "fail", {});
|
|
97
|
+
await wait(200);
|
|
98
|
+
await c1.shutdown();
|
|
99
|
+
|
|
100
|
+
const c2 = new RemoteManager({
|
|
101
|
+
appName: "cons2",
|
|
102
|
+
redisFactory: createMockRedisFactory(server),
|
|
103
|
+
blockMs: 30,
|
|
104
|
+
autoClaimIdleMs: 1,
|
|
105
|
+
dlqMaxDeliveries: 0,
|
|
106
|
+
shutdownDrainMs: 100,
|
|
107
|
+
});
|
|
108
|
+
await c2.start();
|
|
109
|
+
c2.on(
|
|
110
|
+
"fail",
|
|
111
|
+
async () => {
|
|
112
|
+
throw new Error("x");
|
|
113
|
+
},
|
|
114
|
+
"h1"
|
|
115
|
+
);
|
|
116
|
+
await wait(300);
|
|
117
|
+
|
|
118
|
+
expect(server.getStreamLength("remote:cons2:dlq")).toBe(0);
|
|
119
|
+
|
|
120
|
+
await c2.shutdown();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("DLQ entry carries original_id + delivery_count metadata", async () => {
|
|
124
|
+
const c1 = new RemoteManager({
|
|
125
|
+
appName: "cons3",
|
|
126
|
+
redisFactory: createMockRedisFactory(server),
|
|
127
|
+
blockMs: 30,
|
|
128
|
+
autoClaimIdleMs: 0,
|
|
129
|
+
dlqMaxDeliveries: 2,
|
|
130
|
+
shutdownDrainMs: 100,
|
|
131
|
+
});
|
|
132
|
+
await c1.start();
|
|
133
|
+
c1.on(
|
|
134
|
+
"p",
|
|
135
|
+
async () => {
|
|
136
|
+
throw new Error("x");
|
|
137
|
+
},
|
|
138
|
+
"h1"
|
|
139
|
+
);
|
|
140
|
+
await producer.emit("cons3", "p", {});
|
|
141
|
+
await wait(200);
|
|
142
|
+
await c1.shutdown();
|
|
143
|
+
|
|
144
|
+
const c2 = new RemoteManager({
|
|
145
|
+
appName: "cons3",
|
|
146
|
+
redisFactory: createMockRedisFactory(server),
|
|
147
|
+
blockMs: 30,
|
|
148
|
+
autoClaimIdleMs: 1,
|
|
149
|
+
dlqMaxDeliveries: 2,
|
|
150
|
+
shutdownDrainMs: 100,
|
|
151
|
+
});
|
|
152
|
+
await c2.start();
|
|
153
|
+
c2.on(
|
|
154
|
+
"p",
|
|
155
|
+
async () => {
|
|
156
|
+
throw new Error("x");
|
|
157
|
+
},
|
|
158
|
+
"h1"
|
|
159
|
+
);
|
|
160
|
+
await wait(300);
|
|
161
|
+
|
|
162
|
+
const dlqEntries = server.xrange("remote:cons3:dlq", "-", "+");
|
|
163
|
+
expect(dlqEntries.length).toBe(1);
|
|
164
|
+
const [, fields] = dlqEntries[0]!;
|
|
165
|
+
// fields = [k1, v1, k2, v2, ...]
|
|
166
|
+
const flat = fields as string[];
|
|
167
|
+
const idx = (k: string) => flat.indexOf(k);
|
|
168
|
+
expect(idx("original_id")).toBeGreaterThanOrEqual(0);
|
|
169
|
+
expect(idx("delivery_count")).toBeGreaterThanOrEqual(0);
|
|
170
|
+
expect(idx("moved_at")).toBeGreaterThanOrEqual(0);
|
|
171
|
+
expect(idx("data")).toBeGreaterThanOrEqual(0);
|
|
172
|
+
|
|
173
|
+
await c2.shutdown();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { RemoteManager } from "../../../core/remote/RemoteManager";
|
|
3
|
+
import { MockRedisStreamServer } from "../../helpers/MockRedisStreamServer";
|
|
4
|
+
import { createMockRedisFactory } from "../../helpers/MockRedisClient";
|
|
5
|
+
|
|
6
|
+
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
describe("Event round-trip over mock Redis", () => {
|
|
9
|
+
let server: MockRedisStreamServer;
|
|
10
|
+
let appA: RemoteManager;
|
|
11
|
+
let appB: RemoteManager;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
server = new MockRedisStreamServer();
|
|
15
|
+
appA = new RemoteManager({
|
|
16
|
+
appName: "app-a",
|
|
17
|
+
redisFactory: createMockRedisFactory(server),
|
|
18
|
+
blockMs: 50,
|
|
19
|
+
autoClaimIdleMs: 0, // skip orphan reclaim
|
|
20
|
+
dlqMaxDeliveries: 0, // no DLQ for basic tests
|
|
21
|
+
});
|
|
22
|
+
appB = new RemoteManager({
|
|
23
|
+
appName: "app-b",
|
|
24
|
+
redisFactory: createMockRedisFactory(server),
|
|
25
|
+
blockMs: 50,
|
|
26
|
+
autoClaimIdleMs: 0,
|
|
27
|
+
dlqMaxDeliveries: 0,
|
|
28
|
+
});
|
|
29
|
+
await appA.start();
|
|
30
|
+
await appB.start();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(async () => {
|
|
34
|
+
await appA.shutdown();
|
|
35
|
+
await appB.shutdown();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("emit from A is received by B's handler", async () => {
|
|
39
|
+
const received: any[] = [];
|
|
40
|
+
appB.on(
|
|
41
|
+
"order.created",
|
|
42
|
+
async (data, ctx) => {
|
|
43
|
+
received.push({ data, sourceApp: ctx.sourceApp });
|
|
44
|
+
},
|
|
45
|
+
"h1"
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
await appA.emit("app-b", "order.created", { orderId: "abc" });
|
|
49
|
+
await wait(150);
|
|
50
|
+
|
|
51
|
+
expect(received).toHaveLength(1);
|
|
52
|
+
expect(received[0].data).toEqual({ orderId: "abc" });
|
|
53
|
+
expect(received[0].sourceApp).toBe("app-a");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("handler receives ctx.attempt=1 on first delivery", async () => {
|
|
57
|
+
const attempts: number[] = [];
|
|
58
|
+
appB.on(
|
|
59
|
+
"x",
|
|
60
|
+
async (_data, ctx) => {
|
|
61
|
+
attempts.push(ctx.attempt);
|
|
62
|
+
},
|
|
63
|
+
"h1"
|
|
64
|
+
);
|
|
65
|
+
await appA.emit("app-b", "x", {});
|
|
66
|
+
await wait(150);
|
|
67
|
+
expect(attempts).toEqual([1]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("no-handler event is ACKed silently", async () => {
|
|
71
|
+
await appA.emit("app-b", "unhandled.event", {});
|
|
72
|
+
await wait(150);
|
|
73
|
+
// PEL should be empty after ACK
|
|
74
|
+
expect(server.getPelSize("remote:app-b", "app-b")).toBe(0);
|
|
75
|
+
const snap = appB.getMetrics();
|
|
76
|
+
expect(snap.events.noHandler).toBeGreaterThan(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("multiple handlers all fire for one event", async () => {
|
|
80
|
+
const log: string[] = [];
|
|
81
|
+
appB.on("e", async () => { log.push("h1"); }, "h1");
|
|
82
|
+
appB.on("e", async () => { log.push("h2"); }, "h2");
|
|
83
|
+
await appA.emit("app-b", "e", {});
|
|
84
|
+
await wait(150);
|
|
85
|
+
expect(log.sort()).toEqual(["h1", "h2"]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("handler failure leaves message in PEL (no ACK)", async () => {
|
|
89
|
+
appB.on(
|
|
90
|
+
"fail",
|
|
91
|
+
async () => {
|
|
92
|
+
throw new Error("handler boom");
|
|
93
|
+
},
|
|
94
|
+
"h1"
|
|
95
|
+
);
|
|
96
|
+
await appA.emit("app-b", "fail", {});
|
|
97
|
+
await wait(150);
|
|
98
|
+
expect(server.getPelSize("remote:app-b", "app-b")).toBe(1);
|
|
99
|
+
const snap = appB.getMetrics();
|
|
100
|
+
expect(snap.events.handlerFailed).toBe(1);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("metrics reflect emit + receive counters", async () => {
|
|
104
|
+
appB.on("m", async () => {}, "h1");
|
|
105
|
+
await appA.emit("app-b", "m", {});
|
|
106
|
+
await appA.emit("app-b", "m", {});
|
|
107
|
+
await wait(200);
|
|
108
|
+
|
|
109
|
+
const aSnap = appA.getMetrics();
|
|
110
|
+
const bSnap = appB.getMetrics();
|
|
111
|
+
expect(aSnap.emit.direct).toBe(2);
|
|
112
|
+
expect(bSnap.events.handled).toBe(2);
|
|
113
|
+
});
|
|
114
|
+
});
|