autotel 4.1.0 → 4.2.1
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/dist/auto.cjs +5 -3
- package/dist/auto.cjs.map +1 -1
- package/dist/auto.js +3 -3
- package/dist/auto.js.map +1 -1
- package/dist/chunk-C_NdSu1c.cjs +34 -0
- package/dist/correlation-id.cjs +1 -1
- package/dist/correlation-id.d.cts.map +1 -1
- package/dist/correlation-id.d.ts.map +1 -1
- package/dist/correlation-id.js +1 -1
- package/dist/decorators.cjs +1 -1
- package/dist/decorators.js +1 -1
- package/dist/{event-ByBTV9M2.js → event-531asIM6.js} +4 -4
- package/dist/{event-ByBTV9M2.js.map → event-531asIM6.js.map} +1 -1
- package/dist/{event-BhHREDJk.cjs → event-CcZYwp50.cjs} +4 -4
- package/dist/{event-BhHREDJk.cjs.map → event-CcZYwp50.cjs.map} +1 -1
- package/dist/event.cjs +1 -1
- package/dist/event.js +1 -1
- package/dist/{functional-zpzNLhky.cjs → functional-C8B0Qa7o.cjs} +10 -7
- package/dist/functional-C8B0Qa7o.cjs.map +1 -0
- package/dist/{functional-DtI0u4vx.js → functional-r-AUIRy_.js} +9 -9
- package/dist/functional-r-AUIRy_.js.map +1 -0
- package/dist/functional.cjs +1 -1
- package/dist/functional.js +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.js +1 -1
- package/dist/index.cjs +15 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -14
- package/dist/index.js.map +1 -1
- package/dist/{init-D-jnNMix.js → init-BS2JVkrL.js} +2 -2
- package/dist/{init-D-jnNMix.js.map → init-BS2JVkrL.js.map} +1 -1
- package/dist/{init-BX7AmFRl.cjs → init-BXiuPK6j.cjs} +3 -3
- package/dist/{init-BX7AmFRl.cjs.map → init-BXiuPK6j.cjs.map} +1 -1
- package/dist/instrumentation.cjs +2 -2
- package/dist/instrumentation.js +2 -2
- package/dist/logger.cjs +236 -8
- package/dist/logger.cjs.map +1 -0
- package/dist/messaging.cjs +1 -1
- package/dist/messaging.js +1 -1
- package/dist/{node-require-DF5QBX6z.cjs → node-require-CZ_PU448.cjs} +6 -4
- package/dist/node-require-CZ_PU448.cjs.map +1 -0
- package/dist/{node-require-Db1oDpLj.js → node-require-vROmTeJ8.js} +5 -5
- package/dist/node-require-vROmTeJ8.js.map +1 -0
- package/dist/{operation-context-C-2hmmtP.js → operation-context-CKBoA4Qy.js} +3 -3
- package/dist/operation-context-CKBoA4Qy.js.map +1 -0
- package/dist/{operation-context-n4_obUwq.cjs → operation-context-D6LDf4W_.cjs} +3 -1
- package/dist/operation-context-D6LDf4W_.cjs.map +1 -0
- package/dist/register.cjs +3 -1
- package/dist/register.cjs.map +1 -1
- package/dist/register.js +2 -2
- package/dist/register.js.map +1 -1
- package/dist/semantic-helpers.cjs +1 -1
- package/dist/semantic-helpers.js +1 -1
- package/dist/{stable-hash-Cg5cT34Q.js → stable-hash-ChFBIhNt.js} +3 -3
- package/dist/stable-hash-ChFBIhNt.js.map +1 -0
- package/dist/{stable-hash-BNTMrmdB.cjs → stable-hash-brKISGf1.cjs} +4 -2
- package/dist/stable-hash-brKISGf1.cjs.map +1 -0
- package/dist/trace-context-Cijqoi6e.d.cts.map +1 -1
- package/dist/trace-context-Cijqoi6e.d.ts.map +1 -1
- package/dist/trace-helpers.cjs +1 -1
- package/dist/trace-helpers.js +1 -1
- package/dist/{track-wc0HafS_.js → track-COUuU48p.js} +5 -5
- package/dist/track-COUuU48p.js.map +1 -0
- package/dist/{track-D59FfpL0.cjs → track-Cb3Q4QmS.cjs} +4 -2
- package/dist/track-Cb3Q4QmS.cjs.map +1 -0
- package/dist/validate.cjs +1 -1
- package/dist/validate.js +1 -1
- package/dist/webhook.cjs +1 -1
- package/dist/webhook.js +1 -1
- package/dist/workflow-distributed.cjs +1 -1
- package/dist/workflow-distributed.js +1 -1
- package/dist/workflow.cjs +3 -1
- package/dist/workflow.cjs.map +1 -1
- package/dist/workflow.d.cts.map +1 -1
- package/dist/workflow.d.ts.map +1 -1
- package/dist/workflow.js +3 -3
- package/dist/workflow.js.map +1 -1
- package/dist/yaml-config.cjs +233 -4
- package/dist/yaml-config.cjs.map +1 -0
- package/dist/yaml-config.d.cts.map +1 -1
- package/dist/yaml-config.d.ts.map +1 -1
- package/dist/yaml-config.js +8 -7
- package/dist/yaml-config.js.map +1 -1
- package/package.json +1 -2
- package/dist/functional-DtI0u4vx.js.map +0 -1
- package/dist/functional-zpzNLhky.cjs.map +0 -1
- package/dist/logger-thMPLpOG.cjs +0 -487
- package/dist/logger-thMPLpOG.cjs.map +0 -1
- package/dist/node-require-DF5QBX6z.cjs.map +0 -1
- package/dist/node-require-Db1oDpLj.js.map +0 -1
- package/dist/operation-context-C-2hmmtP.js.map +0 -1
- package/dist/operation-context-n4_obUwq.cjs.map +0 -1
- package/dist/stable-hash-BNTMrmdB.cjs.map +0 -1
- package/dist/stable-hash-Cg5cT34Q.js.map +0 -1
- package/dist/track-D59FfpL0.cjs.map +0 -1
- package/dist/track-wc0HafS_.js.map +0 -1
- package/dist/yaml-config-Ck2uB0Dp.cjs +0 -273
- package/dist/yaml-config-Ck2uB0Dp.cjs.map +0 -1
- package/src/attribute-redacting-processor.test.ts +0 -763
- package/src/attribute-redacting-processor.ts +0 -621
- package/src/attributes/attachers.ts +0 -161
- package/src/attributes/builders.ts +0 -529
- package/src/attributes/domains.ts +0 -42
- package/src/attributes/index.ts +0 -81
- package/src/attributes/registry.ts +0 -323
- package/src/attributes/types.ts +0 -211
- package/src/attributes/utils.ts +0 -64
- package/src/attributes/validators.ts +0 -266
- package/src/attributes.test.ts +0 -292
- package/src/auto.ts +0 -67
- package/src/autotel-logger.test.ts +0 -548
- package/src/autotel-logger.ts +0 -364
- package/src/baggage-span-processor.test.ts +0 -202
- package/src/baggage-span-processor.ts +0 -100
- package/src/business-baggage.test.ts +0 -500
- package/src/business-baggage.ts +0 -669
- package/src/circuit-breaker.test.ts +0 -341
- package/src/circuit-breaker.ts +0 -184
- package/src/config.test.ts +0 -94
- package/src/config.ts +0 -172
- package/src/correlated-events.test.ts +0 -151
- package/src/correlated-events.ts +0 -47
- package/src/correlation-id.test.ts +0 -163
- package/src/correlation-id.ts +0 -206
- package/src/db.test.ts +0 -252
- package/src/db.ts +0 -447
- package/src/decorators.test.ts +0 -153
- package/src/decorators.ts +0 -188
- package/src/define-event.test.ts +0 -41
- package/src/define-event.ts +0 -58
- package/src/devtools.ts +0 -60
- package/src/drain-pipeline.test.ts +0 -68
- package/src/drain-pipeline.ts +0 -199
- package/src/drain-toolkit.test.ts +0 -113
- package/src/drain-toolkit.ts +0 -129
- package/src/enricher-toolkit.test.ts +0 -67
- package/src/enricher-toolkit.ts +0 -79
- package/src/enrichers.test.ts +0 -150
- package/src/enrichers.ts +0 -145
- package/src/env-config.test.ts +0 -323
- package/src/env-config.ts +0 -309
- package/src/error-catalog.test.ts +0 -133
- package/src/error-catalog.ts +0 -262
- package/src/event-queue.test.ts +0 -864
- package/src/event-queue.ts +0 -699
- package/src/event-subscriber.ts +0 -262
- package/src/event-testing.ts +0 -197
- package/src/event.test.ts +0 -1104
- package/src/event.ts +0 -988
- package/src/events-config.ts +0 -235
- package/src/exporters.ts +0 -165
- package/src/filtering-span-processor.test.ts +0 -281
- package/src/filtering-span-processor.ts +0 -111
- package/src/flatten-attributes.test.ts +0 -76
- package/src/flatten-attributes.ts +0 -80
- package/src/functional.strict-types.typecheck.ts +0 -53
- package/src/functional.test.ts +0 -1464
- package/src/functional.ts +0 -2539
- package/src/functional.types.test.ts +0 -135
- package/src/hook.mjs +0 -15
- package/src/http.test.ts +0 -485
- package/src/http.ts +0 -424
- package/src/index.ts +0 -433
- package/src/init-auto-redactor.test.ts +0 -53
- package/src/init-redactor.test.ts +0 -8
- package/src/init.customization.test.ts +0 -665
- package/src/init.integrations.test.ts +0 -399
- package/src/init.openllmetry.test.ts +0 -194
- package/src/init.protocol.test.ts +0 -215
- package/src/init.ts +0 -2439
- package/src/instrumentation.test.ts +0 -108
- package/src/instrumentation.ts +0 -319
- package/src/logger.test.ts +0 -125
- package/src/logger.ts +0 -341
- package/src/messaging-adapters.test.ts +0 -595
- package/src/messaging-adapters.ts +0 -583
- package/src/messaging-testing.test.ts +0 -573
- package/src/messaging-testing.ts +0 -935
- package/src/messaging.test.ts +0 -1646
- package/src/messaging.ts +0 -2245
- package/src/metric-helpers.ts +0 -47
- package/src/metric-testing.ts +0 -197
- package/src/metric.ts +0 -446
- package/src/metrics.test.ts +0 -241
- package/src/node-require.ts +0 -123
- package/src/operation-context.ts +0 -93
- package/src/parse-error.test.ts +0 -73
- package/src/parse-error.ts +0 -112
- package/src/posthog-logs.test.ts +0 -115
- package/src/posthog-logs.ts +0 -77
- package/src/pretty-console-exporter.test.ts +0 -545
- package/src/pretty-console-exporter.ts +0 -413
- package/src/pretty-log-formatter.test.ts +0 -123
- package/src/pretty-log-formatter.ts +0 -210
- package/src/processors/canonical-log-line-processor.test.ts +0 -523
- package/src/processors/canonical-log-line-processor.ts +0 -396
- package/src/processors.ts +0 -152
- package/src/rate-limiter.test.ts +0 -199
- package/src/rate-limiter.ts +0 -98
- package/src/redact-values.test.ts +0 -90
- package/src/redact-values.ts +0 -34
- package/src/register.ts +0 -37
- package/src/request-logger.test.ts +0 -545
- package/src/request-logger.ts +0 -342
- package/src/sampling.test.ts +0 -1060
- package/src/sampling.ts +0 -737
- package/src/security-schema.test.ts +0 -45
- package/src/security-schema.ts +0 -107
- package/src/semantic-conventions.ts +0 -15
- package/src/semantic-helpers.test.ts +0 -226
- package/src/semantic-helpers.ts +0 -438
- package/src/shutdown.test.ts +0 -364
- package/src/shutdown.ts +0 -246
- package/src/span-name-normalizer.test.ts +0 -377
- package/src/span-name-normalizer.ts +0 -213
- package/src/stable-hash.ts +0 -27
- package/src/structured-error.test.ts +0 -191
- package/src/structured-error.ts +0 -157
- package/src/stub.integration.test.ts +0 -361
- package/src/tail-sampling-processor.test.ts +0 -230
- package/src/tail-sampling-processor.ts +0 -55
- package/src/test-span-collector.test.ts +0 -234
- package/src/test-span-collector.ts +0 -150
- package/src/testing.ts +0 -705
- package/src/trace-context.test.ts +0 -73
- package/src/trace-context.ts +0 -567
- package/src/trace-helpers.new.test.ts +0 -278
- package/src/trace-helpers.test.ts +0 -290
- package/src/trace-helpers.ts +0 -710
- package/src/trace-hybrid.test.ts +0 -42
- package/src/trace-hybrid.ts +0 -37
- package/src/tracer-provider.test.ts +0 -183
- package/src/tracer-provider.ts +0 -266
- package/src/track.test.ts +0 -154
- package/src/track.ts +0 -216
- package/src/validate.test.ts +0 -287
- package/src/validate.ts +0 -307
- package/src/validation-attributes.ts +0 -43
- package/src/validation.test.ts +0 -330
- package/src/validation.ts +0 -246
- package/src/variable-name-inference.test.ts +0 -178
- package/src/variable-name-inference.ts +0 -242
- package/src/webhook.test.ts +0 -649
- package/src/webhook.ts +0 -637
- package/src/workflow-distributed.test.ts +0 -786
- package/src/workflow-distributed.ts +0 -916
- package/src/workflow.async-safety.integration.test.ts +0 -345
- package/src/workflow.test.ts +0 -647
- package/src/workflow.ts +0 -810
- package/src/yaml-config.test.ts +0 -373
- package/src/yaml-config.ts +0 -351
package/src/event-queue.test.ts
DELETED
|
@@ -1,864 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for events queue guardrails
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
6
|
-
import { EventQueue, type EventDropReason } from './event-queue';
|
|
7
|
-
import { configure, resetConfig } from './config';
|
|
8
|
-
|
|
9
|
-
// Mock adapter for testing
|
|
10
|
-
type MockEvent = {
|
|
11
|
-
name: string;
|
|
12
|
-
attributes?: Record<string, unknown>;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
class MockAdapter {
|
|
16
|
-
public name = 'MockAdapter';
|
|
17
|
-
public events: MockEvent[] = [];
|
|
18
|
-
public callCount = 0;
|
|
19
|
-
public shouldFail = false;
|
|
20
|
-
|
|
21
|
-
async trackEvent(
|
|
22
|
-
name: string,
|
|
23
|
-
attributes?: Record<string, unknown>,
|
|
24
|
-
): Promise<void> {
|
|
25
|
-
this.callCount++;
|
|
26
|
-
if (this.shouldFail) {
|
|
27
|
-
throw new Error('Adapter failed');
|
|
28
|
-
}
|
|
29
|
-
this.events.push({ name, attributes });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async trackFunnelStep(): Promise<void> {}
|
|
33
|
-
async trackOutcome(): Promise<void> {}
|
|
34
|
-
async trackValue(): Promise<void> {}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
describe('EventQueue', () => {
|
|
38
|
-
let mockAdapter: MockAdapter;
|
|
39
|
-
let queue: EventQueue;
|
|
40
|
-
|
|
41
|
-
beforeEach(() => {
|
|
42
|
-
mockAdapter = new MockAdapter();
|
|
43
|
-
queue = new EventQueue([mockAdapter], {
|
|
44
|
-
maxSize: 10,
|
|
45
|
-
batchSize: 3,
|
|
46
|
-
flushInterval: 100,
|
|
47
|
-
maxRetries: 2,
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('Batching', () => {
|
|
52
|
-
it('should enqueue events without immediate sending', () => {
|
|
53
|
-
queue.enqueue({ name: 'test1', attributes: {}, timestamp: Date.now() });
|
|
54
|
-
queue.enqueue({ name: 'test2', attributes: {}, timestamp: Date.now() });
|
|
55
|
-
|
|
56
|
-
expect(queue.size()).toBe(2);
|
|
57
|
-
expect(mockAdapter.callCount).toBe(0); // Not sent yet
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should flush after interval', async () => {
|
|
61
|
-
queue.enqueue({ name: 'test1', attributes: {}, timestamp: Date.now() });
|
|
62
|
-
queue.enqueue({ name: 'test2', attributes: {}, timestamp: Date.now() });
|
|
63
|
-
|
|
64
|
-
// Wait for flush interval
|
|
65
|
-
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
66
|
-
|
|
67
|
-
expect(queue.size()).toBe(0);
|
|
68
|
-
expect(mockAdapter.events.length).toBeGreaterThan(0);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should batch events efficiently', async () => {
|
|
72
|
-
// Enqueue 5 events (batch size is 3)
|
|
73
|
-
for (let i = 0; i < 5; i++) {
|
|
74
|
-
queue.enqueue({
|
|
75
|
-
name: `test${i}`,
|
|
76
|
-
attributes: {},
|
|
77
|
-
timestamp: Date.now(),
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Manual flush
|
|
82
|
-
await queue.flush();
|
|
83
|
-
|
|
84
|
-
expect(queue.size()).toBe(0);
|
|
85
|
-
expect(mockAdapter.events.length).toBe(5);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
describe('Backpressure', () => {
|
|
90
|
-
it('should drop oldest when queue is full in production', () => {
|
|
91
|
-
const originalEnv = process.env.NODE_ENV;
|
|
92
|
-
process.env.NODE_ENV = 'production';
|
|
93
|
-
|
|
94
|
-
// Fill queue to max (10 events)
|
|
95
|
-
for (let i = 0; i < 12; i++) {
|
|
96
|
-
queue.enqueue({
|
|
97
|
-
name: `test${i}`,
|
|
98
|
-
attributes: { index: i },
|
|
99
|
-
timestamp: Date.now(),
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Queue should be at max size
|
|
104
|
-
expect(queue.size()).toBe(10);
|
|
105
|
-
|
|
106
|
-
process.env.NODE_ENV = originalEnv;
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should drop oldest and log when queue is full (consistent behavior)', () => {
|
|
110
|
-
// Fill queue to max
|
|
111
|
-
for (let i = 0; i < 10; i++) {
|
|
112
|
-
queue.enqueue({
|
|
113
|
-
name: `test${i}`,
|
|
114
|
-
attributes: {},
|
|
115
|
-
timestamp: Date.now(),
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Next enqueue should drop oldest (not throw)
|
|
120
|
-
expect(() => {
|
|
121
|
-
queue.enqueue({
|
|
122
|
-
name: 'test11',
|
|
123
|
-
attributes: {},
|
|
124
|
-
timestamp: Date.now(),
|
|
125
|
-
});
|
|
126
|
-
}).not.toThrow();
|
|
127
|
-
|
|
128
|
-
// Verify queue is still at max size
|
|
129
|
-
expect(queue['queue']).toHaveLength(10);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
describe('Retry logic', () => {
|
|
134
|
-
it('should retry on failure', async () => {
|
|
135
|
-
mockAdapter.shouldFail = true;
|
|
136
|
-
|
|
137
|
-
queue.enqueue({ name: 'test1', attributes: {}, timestamp: Date.now() });
|
|
138
|
-
|
|
139
|
-
await queue.flush();
|
|
140
|
-
|
|
141
|
-
// Should have tried maxRetries + 1 times (initial + 2 retries = 3)
|
|
142
|
-
expect(mockAdapter.callCount).toBeGreaterThanOrEqual(3);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('should succeed after transient failure', async () => {
|
|
146
|
-
let failCount = 0;
|
|
147
|
-
mockAdapter.trackEvent = async () => {
|
|
148
|
-
if (failCount < 2) {
|
|
149
|
-
failCount++;
|
|
150
|
-
throw new Error('Transient failure');
|
|
151
|
-
}
|
|
152
|
-
mockAdapter.events.push({ name: 'test', attributes: {} });
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
queue.enqueue({ name: 'test1', attributes: {}, timestamp: Date.now() });
|
|
156
|
-
|
|
157
|
-
await queue.flush();
|
|
158
|
-
|
|
159
|
-
// Should eventually succeed
|
|
160
|
-
expect(mockAdapter.events.length).toBe(1);
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
describe('Metrics semantics', () => {
|
|
165
|
-
function createMockMeter() {
|
|
166
|
-
const counters = new Map<string, { add: ReturnType<typeof vi.fn> }>();
|
|
167
|
-
const histograms = new Map<
|
|
168
|
-
string,
|
|
169
|
-
{ record: ReturnType<typeof vi.fn> }
|
|
170
|
-
>();
|
|
171
|
-
const mockMeter = {
|
|
172
|
-
createObservableGauge: vi.fn(() => ({
|
|
173
|
-
addCallback: vi.fn(),
|
|
174
|
-
removeCallback: vi.fn(),
|
|
175
|
-
})),
|
|
176
|
-
createCounter: vi.fn((name: string) => {
|
|
177
|
-
const counter = { add: vi.fn() };
|
|
178
|
-
counters.set(name, counter);
|
|
179
|
-
return counter;
|
|
180
|
-
}),
|
|
181
|
-
createHistogram: vi.fn((name: string) => {
|
|
182
|
-
const histogram = { record: vi.fn() };
|
|
183
|
-
histograms.set(name, histogram);
|
|
184
|
-
return histogram;
|
|
185
|
-
}),
|
|
186
|
-
};
|
|
187
|
-
return { mockMeter, counters, histograms };
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
it('should not increment failed counter when a retry succeeds', async () => {
|
|
191
|
-
const { mockMeter, counters } = createMockMeter();
|
|
192
|
-
configure({ meter: mockMeter as any });
|
|
193
|
-
|
|
194
|
-
try {
|
|
195
|
-
const adapter = new MockAdapter();
|
|
196
|
-
let failCount = 0;
|
|
197
|
-
adapter.trackEvent = async () => {
|
|
198
|
-
if (failCount < 1) {
|
|
199
|
-
failCount++;
|
|
200
|
-
throw new Error('Transient failure');
|
|
201
|
-
}
|
|
202
|
-
adapter.events.push({ name: 'test', attributes: {} });
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 1 });
|
|
206
|
-
localQueue.enqueue({
|
|
207
|
-
name: 'test1',
|
|
208
|
-
attributes: {},
|
|
209
|
-
timestamp: Date.now(),
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
await localQueue.flush();
|
|
213
|
-
|
|
214
|
-
const failedCounter = counters.get(
|
|
215
|
-
'autotel.event_delivery.queue.failed',
|
|
216
|
-
);
|
|
217
|
-
expect(failedCounter?.add).toHaveBeenCalledTimes(0);
|
|
218
|
-
} finally {
|
|
219
|
-
resetConfig();
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it('should increment failed counter after all retries exhausted', async () => {
|
|
224
|
-
const { mockMeter, counters } = createMockMeter();
|
|
225
|
-
configure({ meter: mockMeter as any });
|
|
226
|
-
|
|
227
|
-
try {
|
|
228
|
-
const adapter = new MockAdapter();
|
|
229
|
-
adapter.shouldFail = true; // Always fail
|
|
230
|
-
|
|
231
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 2 });
|
|
232
|
-
localQueue.enqueue({
|
|
233
|
-
name: 'test1',
|
|
234
|
-
attributes: {},
|
|
235
|
-
timestamp: Date.now(),
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
await localQueue.flush();
|
|
239
|
-
|
|
240
|
-
const failedCounter = counters.get(
|
|
241
|
-
'autotel.event_delivery.queue.failed',
|
|
242
|
-
);
|
|
243
|
-
// Should be called once per event per subscriber after retries exhausted
|
|
244
|
-
expect(failedCounter?.add).toHaveBeenCalledTimes(1);
|
|
245
|
-
expect(failedCounter?.add).toHaveBeenCalledWith(1, {
|
|
246
|
-
subscriber: 'mockadapter',
|
|
247
|
-
});
|
|
248
|
-
} finally {
|
|
249
|
-
resetConfig();
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('should count failed events per event, not per batch', async () => {
|
|
254
|
-
const { mockMeter, counters } = createMockMeter();
|
|
255
|
-
configure({ meter: mockMeter as any });
|
|
256
|
-
|
|
257
|
-
try {
|
|
258
|
-
const adapter = new MockAdapter();
|
|
259
|
-
adapter.trackEvent = async (name) => {
|
|
260
|
-
if (name === 'bad') {
|
|
261
|
-
throw new Error('Per-event failure');
|
|
262
|
-
}
|
|
263
|
-
adapter.events.push({ name, attributes: {} });
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
const localQueue = new EventQueue([adapter], {
|
|
267
|
-
batchSize: 2,
|
|
268
|
-
maxRetries: 0,
|
|
269
|
-
});
|
|
270
|
-
localQueue.enqueue({
|
|
271
|
-
name: 'good',
|
|
272
|
-
attributes: {},
|
|
273
|
-
timestamp: Date.now(),
|
|
274
|
-
});
|
|
275
|
-
localQueue.enqueue({
|
|
276
|
-
name: 'bad',
|
|
277
|
-
attributes: {},
|
|
278
|
-
timestamp: Date.now(),
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
await localQueue.flush();
|
|
282
|
-
|
|
283
|
-
const failedCounter = counters.get(
|
|
284
|
-
'autotel.event_delivery.queue.failed',
|
|
285
|
-
);
|
|
286
|
-
expect(failedCounter?.add).toHaveBeenCalledTimes(1);
|
|
287
|
-
} finally {
|
|
288
|
-
resetConfig();
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
it('should increment delivered counter on successful delivery', async () => {
|
|
293
|
-
const { mockMeter, counters } = createMockMeter();
|
|
294
|
-
configure({ meter: mockMeter as any });
|
|
295
|
-
|
|
296
|
-
try {
|
|
297
|
-
const adapter = new MockAdapter();
|
|
298
|
-
|
|
299
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 1 });
|
|
300
|
-
localQueue.enqueue({
|
|
301
|
-
name: 'test1',
|
|
302
|
-
attributes: {},
|
|
303
|
-
timestamp: Date.now(),
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
await localQueue.flush();
|
|
307
|
-
|
|
308
|
-
const deliveredCounter = counters.get(
|
|
309
|
-
'autotel.event_delivery.queue.delivered',
|
|
310
|
-
);
|
|
311
|
-
expect(deliveredCounter?.add).toHaveBeenCalledTimes(1);
|
|
312
|
-
expect(deliveredCounter?.add).toHaveBeenCalledWith(1, {
|
|
313
|
-
subscriber: 'mockadapter',
|
|
314
|
-
});
|
|
315
|
-
} finally {
|
|
316
|
-
resetConfig();
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
it('should increment delivered counter when retry eventually succeeds', async () => {
|
|
321
|
-
const { mockMeter, counters } = createMockMeter();
|
|
322
|
-
configure({ meter: mockMeter as any });
|
|
323
|
-
|
|
324
|
-
try {
|
|
325
|
-
const adapter = new MockAdapter();
|
|
326
|
-
let failCount = 0;
|
|
327
|
-
adapter.trackEvent = async () => {
|
|
328
|
-
if (failCount < 2) {
|
|
329
|
-
failCount++;
|
|
330
|
-
throw new Error('Transient failure');
|
|
331
|
-
}
|
|
332
|
-
adapter.events.push({ name: 'test', attributes: {} });
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 3 });
|
|
336
|
-
localQueue.enqueue({
|
|
337
|
-
name: 'test1',
|
|
338
|
-
attributes: {},
|
|
339
|
-
timestamp: Date.now(),
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
await localQueue.flush();
|
|
343
|
-
|
|
344
|
-
const deliveredCounter = counters.get(
|
|
345
|
-
'autotel.event_delivery.queue.delivered',
|
|
346
|
-
);
|
|
347
|
-
const failedCounter = counters.get(
|
|
348
|
-
'autotel.event_delivery.queue.failed',
|
|
349
|
-
);
|
|
350
|
-
|
|
351
|
-
// Delivered should be incremented once (eventual success)
|
|
352
|
-
expect(deliveredCounter?.add).toHaveBeenCalledTimes(1);
|
|
353
|
-
// Failed should NOT be incremented (retry succeeded)
|
|
354
|
-
expect(failedCounter?.add).toHaveBeenCalledTimes(0);
|
|
355
|
-
} finally {
|
|
356
|
-
resetConfig();
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
it('should not double-count delivered events when retrying a mixed-success batch', async () => {
|
|
361
|
-
const { mockMeter, counters } = createMockMeter();
|
|
362
|
-
configure({ meter: mockMeter as any });
|
|
363
|
-
|
|
364
|
-
try {
|
|
365
|
-
const adapter = new MockAdapter();
|
|
366
|
-
let badFailCount = 0;
|
|
367
|
-
adapter.trackEvent = async (name) => {
|
|
368
|
-
if (name === 'bad' && badFailCount < 1) {
|
|
369
|
-
badFailCount++;
|
|
370
|
-
throw new Error('Transient failure');
|
|
371
|
-
}
|
|
372
|
-
adapter.events.push({ name, attributes: {} });
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
const localQueue = new EventQueue([adapter], {
|
|
376
|
-
batchSize: 2,
|
|
377
|
-
maxRetries: 1,
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
localQueue.enqueue({
|
|
381
|
-
name: 'good',
|
|
382
|
-
attributes: {},
|
|
383
|
-
timestamp: Date.now(),
|
|
384
|
-
});
|
|
385
|
-
localQueue.enqueue({
|
|
386
|
-
name: 'bad',
|
|
387
|
-
attributes: {},
|
|
388
|
-
timestamp: Date.now(),
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
await localQueue.flush();
|
|
392
|
-
|
|
393
|
-
const deliveredCounter = counters.get(
|
|
394
|
-
'autotel.event_delivery.queue.delivered',
|
|
395
|
-
);
|
|
396
|
-
// Expect one delivery per event (good + bad), not double-counting good on retry
|
|
397
|
-
expect(deliveredCounter?.add).toHaveBeenCalledTimes(2);
|
|
398
|
-
} finally {
|
|
399
|
-
resetConfig();
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
it('should not re-send to healthy subscribers when retrying failed ones', async () => {
|
|
404
|
-
const { mockMeter } = createMockMeter();
|
|
405
|
-
configure({ meter: mockMeter as any });
|
|
406
|
-
|
|
407
|
-
try {
|
|
408
|
-
const failingAdapter = new MockAdapter();
|
|
409
|
-
failingAdapter.name = 'FailingAdapter';
|
|
410
|
-
let failCount = 0;
|
|
411
|
-
failingAdapter.trackEvent = async () => {
|
|
412
|
-
if (failCount < 1) {
|
|
413
|
-
failCount++;
|
|
414
|
-
throw new Error('Transient failure');
|
|
415
|
-
}
|
|
416
|
-
failingAdapter.events.push({ name: 'test', attributes: {} });
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
const healthyAdapter = new MockAdapter();
|
|
420
|
-
healthyAdapter.name = 'HealthyAdapter';
|
|
421
|
-
|
|
422
|
-
const localQueue = new EventQueue([failingAdapter, healthyAdapter], {
|
|
423
|
-
maxRetries: 1,
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
localQueue.enqueue({
|
|
427
|
-
name: 'test1',
|
|
428
|
-
attributes: {},
|
|
429
|
-
timestamp: Date.now(),
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
await localQueue.flush();
|
|
433
|
-
|
|
434
|
-
// Healthy subscriber should only receive the event once
|
|
435
|
-
expect(healthyAdapter.callCount).toBe(1);
|
|
436
|
-
} finally {
|
|
437
|
-
resetConfig();
|
|
438
|
-
}
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
it('should retry only failed events and deliver each event once (three-event batch)', async () => {
|
|
442
|
-
const { mockMeter, counters } = createMockMeter();
|
|
443
|
-
configure({ meter: mockMeter as any });
|
|
444
|
-
|
|
445
|
-
try {
|
|
446
|
-
const adapter = new MockAdapter();
|
|
447
|
-
let middleFailCount = 0;
|
|
448
|
-
adapter.trackEvent = async (name) => {
|
|
449
|
-
if (name === 'middle' && middleFailCount < 1) {
|
|
450
|
-
middleFailCount++;
|
|
451
|
-
throw new Error('Transient failure');
|
|
452
|
-
}
|
|
453
|
-
adapter.events.push({ name, attributes: {} });
|
|
454
|
-
};
|
|
455
|
-
|
|
456
|
-
const localQueue = new EventQueue([adapter], {
|
|
457
|
-
batchSize: 3,
|
|
458
|
-
maxRetries: 1,
|
|
459
|
-
});
|
|
460
|
-
|
|
461
|
-
localQueue.enqueue({
|
|
462
|
-
name: 'first',
|
|
463
|
-
attributes: {},
|
|
464
|
-
timestamp: Date.now(),
|
|
465
|
-
});
|
|
466
|
-
localQueue.enqueue({
|
|
467
|
-
name: 'middle',
|
|
468
|
-
attributes: {},
|
|
469
|
-
timestamp: Date.now(),
|
|
470
|
-
});
|
|
471
|
-
localQueue.enqueue({
|
|
472
|
-
name: 'last',
|
|
473
|
-
attributes: {},
|
|
474
|
-
timestamp: Date.now(),
|
|
475
|
-
});
|
|
476
|
-
|
|
477
|
-
await localQueue.flush();
|
|
478
|
-
|
|
479
|
-
// Adapter receives exactly three events, each once (first and last on first attempt, middle on retry)
|
|
480
|
-
expect(adapter.events).toHaveLength(3);
|
|
481
|
-
expect(adapter.events.map((e) => e.name)).toEqual([
|
|
482
|
-
'first',
|
|
483
|
-
'last',
|
|
484
|
-
'middle',
|
|
485
|
-
]);
|
|
486
|
-
|
|
487
|
-
const deliveredCounter = counters.get(
|
|
488
|
-
'autotel.event_delivery.queue.delivered',
|
|
489
|
-
);
|
|
490
|
-
expect(deliveredCounter?.add).toHaveBeenCalledTimes(3);
|
|
491
|
-
} finally {
|
|
492
|
-
resetConfig();
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
it('should record latency histogram on successful delivery', async () => {
|
|
497
|
-
const { mockMeter, histograms } = createMockMeter();
|
|
498
|
-
configure({ meter: mockMeter as any });
|
|
499
|
-
|
|
500
|
-
try {
|
|
501
|
-
const adapter = new MockAdapter();
|
|
502
|
-
|
|
503
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 1 });
|
|
504
|
-
localQueue.enqueue({
|
|
505
|
-
name: 'test1',
|
|
506
|
-
attributes: {},
|
|
507
|
-
timestamp: Date.now(),
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
await localQueue.flush();
|
|
511
|
-
|
|
512
|
-
const latencyHistogram = histograms.get(
|
|
513
|
-
'autotel.event_delivery.queue.latency_ms',
|
|
514
|
-
);
|
|
515
|
-
expect(latencyHistogram?.record).toHaveBeenCalledTimes(1);
|
|
516
|
-
// First argument is the latency value (number), second is attributes
|
|
517
|
-
expect(latencyHistogram?.record).toHaveBeenCalledWith(
|
|
518
|
-
expect.any(Number),
|
|
519
|
-
{ subscriber: 'mockadapter' },
|
|
520
|
-
);
|
|
521
|
-
} finally {
|
|
522
|
-
resetConfig();
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
it('should not record latency when delivery fails', async () => {
|
|
527
|
-
const { mockMeter, histograms } = createMockMeter();
|
|
528
|
-
configure({ meter: mockMeter as any });
|
|
529
|
-
|
|
530
|
-
try {
|
|
531
|
-
const adapter = new MockAdapter();
|
|
532
|
-
adapter.shouldFail = true;
|
|
533
|
-
|
|
534
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 1 });
|
|
535
|
-
localQueue.enqueue({
|
|
536
|
-
name: 'test1',
|
|
537
|
-
attributes: {},
|
|
538
|
-
timestamp: Date.now(),
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
await localQueue.flush();
|
|
542
|
-
|
|
543
|
-
const latencyHistogram = histograms.get(
|
|
544
|
-
'autotel.event_delivery.queue.latency_ms',
|
|
545
|
-
);
|
|
546
|
-
// No latency recorded for failed deliveries
|
|
547
|
-
expect(latencyHistogram?.record).toHaveBeenCalledTimes(0);
|
|
548
|
-
} finally {
|
|
549
|
-
resetConfig();
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
it('should mark subscriber unhealthy on transient failure', async () => {
|
|
554
|
-
const { mockMeter } = createMockMeter();
|
|
555
|
-
configure({ meter: mockMeter as any });
|
|
556
|
-
|
|
557
|
-
try {
|
|
558
|
-
const adapter = new MockAdapter();
|
|
559
|
-
let failCount = 0;
|
|
560
|
-
adapter.trackEvent = async () => {
|
|
561
|
-
if (failCount < 1) {
|
|
562
|
-
failCount++;
|
|
563
|
-
throw new Error('Transient failure');
|
|
564
|
-
}
|
|
565
|
-
adapter.events.push({ name: 'test', attributes: {} });
|
|
566
|
-
};
|
|
567
|
-
|
|
568
|
-
const localQueue = new EventQueue([adapter], { maxRetries: 2 });
|
|
569
|
-
|
|
570
|
-
// Initially healthy
|
|
571
|
-
expect(localQueue.isSubscriberHealthy('mockadapter')).toBe(true);
|
|
572
|
-
|
|
573
|
-
localQueue.enqueue({
|
|
574
|
-
name: 'test1',
|
|
575
|
-
attributes: {},
|
|
576
|
-
timestamp: Date.now(),
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
await localQueue.flush();
|
|
580
|
-
|
|
581
|
-
// After successful retry, should be healthy again
|
|
582
|
-
expect(localQueue.isSubscriberHealthy('mockadapter')).toBe(true);
|
|
583
|
-
} finally {
|
|
584
|
-
resetConfig();
|
|
585
|
-
}
|
|
586
|
-
});
|
|
587
|
-
|
|
588
|
-
it('should handle multiple subscribers with mixed success/failure', async () => {
|
|
589
|
-
const { mockMeter, counters } = createMockMeter();
|
|
590
|
-
configure({ meter: mockMeter as any });
|
|
591
|
-
|
|
592
|
-
try {
|
|
593
|
-
const adapter1 = new MockAdapter();
|
|
594
|
-
adapter1.name = 'SuccessAdapter';
|
|
595
|
-
|
|
596
|
-
const adapter2 = new MockAdapter();
|
|
597
|
-
adapter2.name = 'FailAdapter';
|
|
598
|
-
adapter2.shouldFail = true;
|
|
599
|
-
|
|
600
|
-
const localQueue = new EventQueue([adapter1, adapter2], {
|
|
601
|
-
maxRetries: 1,
|
|
602
|
-
});
|
|
603
|
-
localQueue.enqueue({
|
|
604
|
-
name: 'test1',
|
|
605
|
-
attributes: {},
|
|
606
|
-
timestamp: Date.now(),
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
await localQueue.flush();
|
|
610
|
-
|
|
611
|
-
const deliveredCounter = counters.get(
|
|
612
|
-
'autotel.event_delivery.queue.delivered',
|
|
613
|
-
);
|
|
614
|
-
const failedCounter = counters.get(
|
|
615
|
-
'autotel.event_delivery.queue.failed',
|
|
616
|
-
);
|
|
617
|
-
|
|
618
|
-
// One subscriber succeeded, one failed
|
|
619
|
-
expect(deliveredCounter?.add).toHaveBeenCalledWith(1, {
|
|
620
|
-
subscriber: 'successadapter',
|
|
621
|
-
});
|
|
622
|
-
expect(failedCounter?.add).toHaveBeenCalledWith(1, {
|
|
623
|
-
subscriber: 'failadapter',
|
|
624
|
-
});
|
|
625
|
-
|
|
626
|
-
// Verify counters were NOT called for the wrong subscribers
|
|
627
|
-
expect(deliveredCounter?.add).not.toHaveBeenCalledWith(1, {
|
|
628
|
-
subscriber: 'failadapter',
|
|
629
|
-
});
|
|
630
|
-
expect(failedCounter?.add).not.toHaveBeenCalledWith(1, {
|
|
631
|
-
subscriber: 'successadapter',
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
// Delivered once (successful subscriber only; retry only sends to failed subscriber)
|
|
635
|
-
expect(deliveredCounter?.add).toHaveBeenCalledTimes(1);
|
|
636
|
-
|
|
637
|
-
// Failed counter only called once after all retries exhausted
|
|
638
|
-
expect(failedCounter?.add).toHaveBeenCalledTimes(1);
|
|
639
|
-
} finally {
|
|
640
|
-
resetConfig();
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
});
|
|
644
|
-
|
|
645
|
-
describe('Graceful flush', () => {
|
|
646
|
-
it('should flush all remaining events', async () => {
|
|
647
|
-
for (let i = 0; i < 5; i++) {
|
|
648
|
-
queue.enqueue({
|
|
649
|
-
name: `test${i}`,
|
|
650
|
-
attributes: {},
|
|
651
|
-
timestamp: Date.now(),
|
|
652
|
-
});
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
expect(queue.size()).toBe(5);
|
|
656
|
-
|
|
657
|
-
await queue.flush();
|
|
658
|
-
|
|
659
|
-
expect(queue.size()).toBe(0);
|
|
660
|
-
expect(mockAdapter.events.length).toBe(5);
|
|
661
|
-
});
|
|
662
|
-
|
|
663
|
-
it('should handle empty queue flush', async () => {
|
|
664
|
-
await expect(queue.flush()).resolves.not.toThrow();
|
|
665
|
-
});
|
|
666
|
-
|
|
667
|
-
it('should allow enqueuing after flush', async () => {
|
|
668
|
-
queue.enqueue({ name: 'test1', attributes: {}, timestamp: Date.now() });
|
|
669
|
-
await queue.flush();
|
|
670
|
-
|
|
671
|
-
queue.enqueue({ name: 'test2', attributes: {}, timestamp: Date.now() });
|
|
672
|
-
await queue.flush();
|
|
673
|
-
|
|
674
|
-
expect(mockAdapter.events.map((event) => event.name)).toEqual([
|
|
675
|
-
'test1',
|
|
676
|
-
'test2',
|
|
677
|
-
]);
|
|
678
|
-
});
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
describe('Multiple adapters', () => {
|
|
682
|
-
it('should send to all adapters', async () => {
|
|
683
|
-
const adapter1 = new MockAdapter();
|
|
684
|
-
const adapter2 = new MockAdapter();
|
|
685
|
-
const multiQueue = new EventQueue([adapter1, adapter2]);
|
|
686
|
-
|
|
687
|
-
multiQueue.enqueue({
|
|
688
|
-
name: 'test1',
|
|
689
|
-
attributes: {},
|
|
690
|
-
timestamp: Date.now(),
|
|
691
|
-
});
|
|
692
|
-
|
|
693
|
-
await multiQueue.flush();
|
|
694
|
-
|
|
695
|
-
expect(adapter1.events.length).toBe(1);
|
|
696
|
-
expect(adapter2.events.length).toBe(1);
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
it('should handle partial adapter failures', async () => {
|
|
700
|
-
const adapter1 = new MockAdapter();
|
|
701
|
-
const adapter2 = new MockAdapter();
|
|
702
|
-
adapter1.shouldFail = true; // One adapter fails
|
|
703
|
-
|
|
704
|
-
const multiQueue = new EventQueue([adapter1, adapter2], {
|
|
705
|
-
maxRetries: 1,
|
|
706
|
-
});
|
|
707
|
-
|
|
708
|
-
multiQueue.enqueue({
|
|
709
|
-
name: 'test1',
|
|
710
|
-
attributes: {},
|
|
711
|
-
timestamp: Date.now(),
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
// Should not throw, just log error
|
|
715
|
-
await expect(multiQueue.flush()).resolves.not.toThrow();
|
|
716
|
-
});
|
|
717
|
-
});
|
|
718
|
-
|
|
719
|
-
describe('Subscriber health tracking', () => {
|
|
720
|
-
it('should track subscriber health status', () => {
|
|
721
|
-
const adapter = new MockAdapter();
|
|
722
|
-
adapter.name = 'TestAdapter';
|
|
723
|
-
const healthQueue = new EventQueue([adapter]);
|
|
724
|
-
|
|
725
|
-
// All subscribers start healthy
|
|
726
|
-
expect(healthQueue.isSubscriberHealthy('testadapter')).toBe(true);
|
|
727
|
-
});
|
|
728
|
-
|
|
729
|
-
it('should mark subscriber as unhealthy on persistent failure', async () => {
|
|
730
|
-
const adapter = new MockAdapter();
|
|
731
|
-
adapter.name = 'FailingAdapter';
|
|
732
|
-
adapter.shouldFail = true;
|
|
733
|
-
|
|
734
|
-
const healthQueue = new EventQueue([adapter], {
|
|
735
|
-
maxRetries: 1,
|
|
736
|
-
});
|
|
737
|
-
|
|
738
|
-
healthQueue.enqueue({
|
|
739
|
-
name: 'test1',
|
|
740
|
-
attributes: {},
|
|
741
|
-
timestamp: Date.now(),
|
|
742
|
-
});
|
|
743
|
-
|
|
744
|
-
await healthQueue.flush();
|
|
745
|
-
|
|
746
|
-
// After failure, subscriber should be marked unhealthy
|
|
747
|
-
expect(healthQueue.isSubscriberHealthy('failingadapter')).toBe(false);
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
it('should mark subscriber as healthy on success', async () => {
|
|
751
|
-
const adapter = new MockAdapter();
|
|
752
|
-
adapter.name = 'HealthyAdapter';
|
|
753
|
-
|
|
754
|
-
const healthQueue = new EventQueue([adapter]);
|
|
755
|
-
|
|
756
|
-
// Manually mark as unhealthy first
|
|
757
|
-
healthQueue.setSubscriberHealth('healthyadapter', false);
|
|
758
|
-
expect(healthQueue.isSubscriberHealthy('healthyadapter')).toBe(false);
|
|
759
|
-
|
|
760
|
-
// Successful delivery should mark as healthy
|
|
761
|
-
healthQueue.enqueue({
|
|
762
|
-
name: 'test1',
|
|
763
|
-
attributes: {},
|
|
764
|
-
timestamp: Date.now(),
|
|
765
|
-
});
|
|
766
|
-
|
|
767
|
-
await healthQueue.flush();
|
|
768
|
-
|
|
769
|
-
expect(healthQueue.isSubscriberHealthy('healthyadapter')).toBe(true);
|
|
770
|
-
});
|
|
771
|
-
|
|
772
|
-
it('should return health status for all subscribers', () => {
|
|
773
|
-
const adapter1 = new MockAdapter();
|
|
774
|
-
adapter1.name = 'Adapter1';
|
|
775
|
-
const adapter2 = new MockAdapter();
|
|
776
|
-
adapter2.name = 'Adapter2';
|
|
777
|
-
|
|
778
|
-
const healthQueue = new EventQueue([adapter1, adapter2]);
|
|
779
|
-
|
|
780
|
-
healthQueue.setSubscriberHealth('adapter1', false);
|
|
781
|
-
|
|
782
|
-
const healthMap = healthQueue.getSubscriberHealth();
|
|
783
|
-
expect(healthMap.get('adapter1')).toBe(false);
|
|
784
|
-
expect(healthMap.get('adapter2')).toBe(true);
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
it('should not mark healthy subscribers as unhealthy when another fails', async () => {
|
|
788
|
-
const failingAdapter = new MockAdapter();
|
|
789
|
-
failingAdapter.name = 'FailingAdapter';
|
|
790
|
-
failingAdapter.shouldFail = true;
|
|
791
|
-
|
|
792
|
-
const healthyAdapter = new MockAdapter();
|
|
793
|
-
healthyAdapter.name = 'HealthyAdapter';
|
|
794
|
-
|
|
795
|
-
const healthQueue = new EventQueue([failingAdapter, healthyAdapter], {
|
|
796
|
-
maxRetries: 0,
|
|
797
|
-
});
|
|
798
|
-
|
|
799
|
-
healthQueue.enqueue({
|
|
800
|
-
name: 'test1',
|
|
801
|
-
attributes: {},
|
|
802
|
-
timestamp: Date.now(),
|
|
803
|
-
});
|
|
804
|
-
|
|
805
|
-
await healthQueue.flush();
|
|
806
|
-
|
|
807
|
-
expect(healthQueue.isSubscriberHealthy('failingadapter')).toBe(false);
|
|
808
|
-
expect(healthQueue.isSubscriberHealthy('healthyadapter')).toBe(true);
|
|
809
|
-
});
|
|
810
|
-
});
|
|
811
|
-
|
|
812
|
-
describe('Shutdown behavior', () => {
|
|
813
|
-
it('should reject events during shutdown', async () => {
|
|
814
|
-
const adapter = new MockAdapter();
|
|
815
|
-
const shutdownQueue = new EventQueue([adapter], {
|
|
816
|
-
flushInterval: 100,
|
|
817
|
-
});
|
|
818
|
-
|
|
819
|
-
// Enqueue some events
|
|
820
|
-
shutdownQueue.enqueue({
|
|
821
|
-
name: 'test1',
|
|
822
|
-
attributes: {},
|
|
823
|
-
timestamp: Date.now(),
|
|
824
|
-
});
|
|
825
|
-
|
|
826
|
-
// Start shutdown (sets isShuttingDown, then flushes)
|
|
827
|
-
const shutdownPromise = shutdownQueue.shutdown();
|
|
828
|
-
|
|
829
|
-
// Try to enqueue during shutdown - should be rejected
|
|
830
|
-
shutdownQueue.enqueue({
|
|
831
|
-
name: 'test2',
|
|
832
|
-
attributes: {},
|
|
833
|
-
timestamp: Date.now(),
|
|
834
|
-
});
|
|
835
|
-
|
|
836
|
-
await shutdownPromise;
|
|
837
|
-
|
|
838
|
-
// Only first event should be delivered
|
|
839
|
-
expect(adapter.events.length).toBe(1);
|
|
840
|
-
expect(adapter.events[0].name).toBe('test1');
|
|
841
|
-
});
|
|
842
|
-
});
|
|
843
|
-
|
|
844
|
-
describe('Correlation ID enrichment', () => {
|
|
845
|
-
it('should enrich events with correlation ID', async () => {
|
|
846
|
-
const adapter = new MockAdapter();
|
|
847
|
-
const correlationQueue = new EventQueue([adapter], {
|
|
848
|
-
flushInterval: 50,
|
|
849
|
-
});
|
|
850
|
-
|
|
851
|
-
correlationQueue.enqueue({
|
|
852
|
-
name: 'test1',
|
|
853
|
-
attributes: {},
|
|
854
|
-
timestamp: Date.now(),
|
|
855
|
-
});
|
|
856
|
-
|
|
857
|
-
await correlationQueue.flush();
|
|
858
|
-
|
|
859
|
-
// The queue should have enriched the event with _correlationId
|
|
860
|
-
// We can verify this by checking that the event was delivered
|
|
861
|
-
expect(adapter.events.length).toBe(1);
|
|
862
|
-
});
|
|
863
|
-
});
|
|
864
|
-
});
|