This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathevent_processor-test.js
680 lines (594 loc) · 25.6 KB
/
event_processor-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
const { DiagnosticsManager, DiagnosticId } = require('../diagnostic_events');
const EventProcessor = require('../event_processor');
const { failOnTimeout, TestHttpHandlers, TestHttpServer, withCloseable } = require('launchdarkly-js-test-helpers');
describe('EventProcessor', () => {
const eventsUri = 'http://example.com';
const sdkKey = 'SDK_KEY';
const defaultConfig = {
eventsUri: eventsUri,
capacity: 100,
flushInterval: 30,
contextKeysCapacity: 1000,
contextKeysFlushInterval: 300,
diagnosticRecordingInterval: 900,
logger: {
debug: jest.fn(),
warn: jest.fn()
}
};
const user = { key: 'userKey', name: 'Red' };
const singleKindUser = { ...user, kind: 'user' };
const anonUser = { key: 'anon-user', name: 'Anon', anonymous: true };
const singleKindAnonUser = {key: 'anon-user', kind: 'user', name: 'Anon', anonymous: true };
const filteredUser = { key: 'userKey', kind: 'user', _meta: { redactedAttributes: ['/name'] } };
const numericUser = {
key: 1, ip: 3, country: 4, email: 5, firstName: 6, lastName: 7,
avatar: 8, name: 9, anonymous: false, custom: { age: 99 }
};
const stringifiedNumericUser = {
kind: 'user', key: '1', ip: '3', country: '4', email: '5', firstName: '6',
lastName: '7', avatar: '8', name: '9', age: 99, anonymous: false
};
function eventsServerTest(asyncCallback) {
return async () => withCloseable(TestHttpServer.start, async server => {
server.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(200));
server.forMethodAndPath('post', '/diagnostic', TestHttpHandlers.respond(200));
return await asyncCallback(server);
});
}
async function withEventProcessor(baseConfig, server, asyncCallback) {
const config = Object.assign({}, baseConfig, { eventsUri: server.url, diagnosticOptOut: true });
const ep = EventProcessor(sdkKey, config);
try {
return await asyncCallback(ep);
} finally {
ep.close();
}
}
async function withDiagnosticEventProcessor(baseConfig, server, asyncCallback) {
const config = Object.assign({}, baseConfig, { eventsUri: server.url });
const id = DiagnosticId(sdkKey);
const manager = DiagnosticsManager(config, id, new Date().getTime());
const ep = EventProcessor(sdkKey, config, null, manager);
try {
return await asyncCallback(ep, id, manager);
} finally {
ep.close();
}
}
function headersWithDate(timestamp) {
return { date: new Date(timestamp).toUTCString() };
}
function checkIndexEvent(e, source, context) {
expect(e.kind).toEqual('index');
expect(e.creationDate).toEqual(source.creationDate);
expect(e.context).toEqual(context);
}
function checkFeatureEvent(e, source, debug, contextKeys, inlineContext) {
expect(e.kind).toEqual(debug ? 'debug' : 'feature');
expect(e.creationDate).toEqual(source.creationDate);
expect(e.key).toEqual(source.key);
expect(e.version).toEqual(source.version);
expect(e.variation).toEqual(source.variation);
expect(e.value).toEqual(source.value);
expect(e.default).toEqual(source.default);
expect(e.reason).toEqual(source.reason);
if (inlineContext) {
expect(e.context).toEqual(inlineContext);
} else {
expect(e.contextKeys).toEqual(contextKeys);
}
}
function checkCustomEvent(e, source, contextKeys) {
expect(e.kind).toEqual('custom');
expect(e.creationDate).toEqual(source.creationDate);
expect(e.key).toEqual(source.key);
expect(e.data).toEqual(source.data);
expect(e.metricValue).toBe(source.metricValue);
expect(e.contextKeys).toEqual(contextKeys);
}
function checkSummaryEvent(e) {
expect(e.kind).toEqual('summary');
}
async function getJsonRequest(server) {
return JSON.parse((await server.nextRequest()).body);
}
it('queues identify event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = { kind: 'identify', creationDate: 1000, context: user };
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output).toEqual([{
kind: 'identify',
creationDate: 1000,
context: singleKindUser
}]);
});
}));
it('filters user in identify event', eventsServerTest(async s => {
const config = Object.assign({}, defaultConfig, { allAttributesPrivate: true });
await withEventProcessor(config, s, async ep => {
const e = { kind: 'identify', creationDate: 1000, context: user };
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output).toEqual([{
kind: 'identify',
creationDate: 1000,
context: filteredUser
}]);
});
}));
it('stringifies user attributes in identify event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = { kind: 'identify', creationDate: 1000, context: numericUser };
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output).toEqual([{
kind: 'identify',
creationDate: 1000,
context: stringifiedNumericUser
}]);
});
}));
it('queues individual feature event with index event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: true
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, singleKindUser);
checkFeatureEvent(output[1], e, false, {user: 'userKey'});
checkSummaryEvent(output[2]);
});
}));
it('handles the version being 0', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = { kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 0, variation: 1, value: 'value', trackEvents: true };
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, singleKindUser);
checkFeatureEvent(output[1], e, false, {user: 'userKey'});
checkSummaryEvent(output[2]);
});
}));
it('queues individual feature event with index event for anonymous user', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'feature', creationDate: 1000, context: anonUser, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: true
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, singleKindAnonUser);
checkFeatureEvent(output[1], e, false, {user: 'anon-user'});
checkSummaryEvent(output[2]);
});
}));
it('filters user in index event', eventsServerTest(async s => {
const config = Object.assign({}, defaultConfig, { allAttributesPrivate: true });
await withEventProcessor(config, s, async ep => {
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: true
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, filteredUser);
checkFeatureEvent(output[1], e, false, {user: 'userKey'});
checkSummaryEvent(output[2]);
});
}));
it('stringifies user attributes in index event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'feature', creationDate: 1000, context: numericUser, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: true
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, stringifiedNumericUser);
checkFeatureEvent(output[1], e, false, {user: '1'});
checkSummaryEvent(output[2]);
});
}));
it('can include reason in feature event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: true,
reason: { kind: 'FALLTHROUGH' }
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, singleKindUser);
checkFeatureEvent(output[1], e, false, {user: 'userKey'});
checkSummaryEvent(output[2]);
});
}));
it('sets event kind to debug if event is temporarily in debug mode', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
var futureTime = new Date().getTime() + 1000000;
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: false, debugEventsUntilDate: futureTime
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(3);
checkIndexEvent(output[0], e, singleKindUser);
checkFeatureEvent(output[1], e, true, {user: 'userKey'}, singleKindUser);
checkSummaryEvent(output[2]);
});
}));
it('can both track and debug an event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const futureTime = new Date().getTime() + 1000000;
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: true, debugEventsUntilDate: futureTime
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(4);
checkIndexEvent(output[0], e, singleKindUser);
checkFeatureEvent(output[1], e, false, {user: 'userKey'});
checkFeatureEvent(output[2], e, true, {user: 'userKey'}, singleKindUser);
checkSummaryEvent(output[3]);
});
}));
it('expires debug mode based on client time if client time is later than server time', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
// Pick a server time that is somewhat behind the client time
const serverTime = new Date().getTime() - 20000;
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(200, headersWithDate(serverTime)));
// Send and flush an event we don't care about, just to set the last server time
ep.sendEvent({ kind: 'identify', context: { key: 'otherUser' } });
await ep.flush();
await s.nextRequest();
// Now send an event with debug mode on, with a "debug until" time that is further in
// the future than the server time, but in the past compared to the client.
const debugUntil = serverTime + 1000;
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: false, debugEventsUntilDate: debugUntil
};
ep.sendEvent(e);
await ep.flush();
// Should get a summary event only, not a full feature event
const output = await getJsonRequest(s);
expect(output.length).toEqual(2);
checkIndexEvent(output[0], e, singleKindUser);
checkSummaryEvent(output[1]);
});
}));
it('expires debug mode based on server time if server time is later than client time', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
// Pick a server time that is somewhat ahead of the client time
const serverTime = new Date().getTime() + 20000;
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(200, headersWithDate(serverTime)));
// Send and flush an event we don't care about, just to set the last server time
ep.sendEvent({ kind: 'identify', context: { key: 'otherUser' } });
await ep.flush();
await s.nextRequest();
// Now send an event with debug mode on, with a "debug until" time that is further in
// the future than the client time, but in the past compared to the server.
const debugUntil = serverTime - 1000;
const e = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey',
version: 11, variation: 1, value: 'value', trackEvents: false, debugEventsUntilDate: debugUntil
};
ep.sendEvent(e);
await ep.flush();
// Should get a summary event only, not a full feature event
const output = await getJsonRequest(s);
expect(output.length).toEqual(2);
checkIndexEvent(output[0], e, singleKindUser);
checkSummaryEvent(output[1]);
});
}));
it('generates only one index event from two feature events for same user', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e1 = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey1',
version: 11, variation: 1, value: 'value', trackEvents: true
};
const e2 = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey2',
version: 11, variation: 1, value: 'value', trackEvents: true
};
ep.sendEvent(e1);
ep.sendEvent(e2);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(4);
checkIndexEvent(output[0], e1, singleKindUser);
checkFeatureEvent(output[1], e1, false, {user: 'userKey'});
checkFeatureEvent(output[2], e2, false, {user: 'userKey'});
checkSummaryEvent(output[3]);
});
}));
it('summarizes nontracked events', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e1 = {
kind: 'feature', creationDate: 1000, context: user, key: 'flagkey1',
version: 11, variation: 1, value: 'value1', default: 'default1', trackEvents: false
};
const e2 = {
kind: 'feature', creationDate: 2000, context: user, key: 'flagkey2',
version: 22, variation: 1, value: 'value2', default: 'default2', trackEvents: false
};
ep.sendEvent(e1);
ep.sendEvent(e2);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(2);
checkIndexEvent(output[0], e1, singleKindUser);
const se = output[1];
checkSummaryEvent(se);
expect(se.startDate).toEqual(1000);
expect(se.endDate).toEqual(2000);
expect(se.features).toEqual({
flagkey1: {
default: 'default1',
counters: [{ version: 11, variation: 1, value: 'value1', count: 1 }],
contextKinds: ['user']
},
flagkey2: {
default: 'default2',
counters: [{ version: 22, variation: 1, value: 'value2', count: 1 }],
contextKinds: ['user']
}
});
});
}));
it('queues custom event with user', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'custom', creationDate: 1000, context: user, key: 'eventkey',
data: { thing: 'stuff' }
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(2);
checkIndexEvent(output[0], e, singleKindUser);
checkCustomEvent(output[1], e, {user: 'userKey'});
});
}));
it('queues custom event with anonymous user', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'custom', creationDate: 1000, context: anonUser, key: 'eventkey', data: { thing: 'stuff' }
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(2);
checkIndexEvent(output[0], e, singleKindAnonUser);
checkCustomEvent(output[1], e, {user: 'anon-user'});
});
}));
it('can include metric value in custom event', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = {
kind: 'custom', creationDate: 1000, context: user, key: 'eventkey',
data: { thing: 'stuff' }, metricValue: 1.5
};
ep.sendEvent(e);
await ep.flush();
const output = await getJsonRequest(s);
expect(output.length).toEqual(2);
checkIndexEvent(output[0], e, singleKindUser);
checkCustomEvent(output[1], e, {user: 'userKey'});
});
}));
it('sends nothing if there are no events', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
await ep.flush();
expect(s.requestCount()).toEqual(0);
});
}));
it('sends SDK key', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = { kind: 'identify', creationDate: 1000, context: user };
ep.sendEvent(e);
await ep.flush();
const request = await s.nextRequest();
expect(request.headers['authorization']).toEqual(sdkKey);
});
}));
it('sends unique payload IDs', eventsServerTest(async s => {
await withEventProcessor(defaultConfig, s, async ep => {
const e = { kind: 'identify', creationDate: 1000, context: user };
ep.sendEvent(e);
await ep.flush();
ep.sendEvent(e);
await ep.flush();
const req0 = await s.nextRequest();
const req1 = await s.nextRequest();
const id0 = req0.headers['x-launchdarkly-payload-id'];
const id1 = req1.headers['x-launchdarkly-payload-id'];
expect(id0).toBeTruthy();
expect(id1).toBeTruthy();
expect(id0).not.toEqual(id1);
});
}));
function verifyUnrecoverableHttpError(status) {
return eventsServerTest(async s => {
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(status));
await withEventProcessor(defaultConfig, s, async ep => {
const e = { kind: 'identify', creationDate: 1000, context: user };
ep.sendEvent(e);
await expect(ep.flush()).rejects.toThrow('error ' + status);
expect(s.requestCount()).toEqual(1);
await s.nextRequest();
ep.sendEvent(e);
await expect(ep.flush()).rejects.toThrow(/SDK key is invalid/);
expect(s.requestCount()).toEqual(1);
});
});
}
function verifyRecoverableHttpError(status) {
return eventsServerTest(async s => {
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(status));
await withEventProcessor(defaultConfig, s, async ep => {
var e = { kind: 'identify', creationDate: 1000, context: user };
ep.sendEvent(e);
await expect(ep.flush()).rejects.toThrow('error ' + status);
expect(s.requestCount()).toEqual(2);
const req0 = await s.nextRequest();
const req1 = await s.nextRequest();
expect(req0.body).toEqual(req1.body);
const id0 = req0.headers['x-launchdarkly-payload-id'];
expect(req1.headers['x-launchdarkly-payload-id']).toEqual(id0);
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(200));
ep.sendEvent(e);
await ep.flush();
expect(s.requestCount()).toEqual(3);
const req2 = await s.nextRequest();
expect(req2.headers['x-launchdarkly-payload-id']).not.toEqual(id0);
});
});
}
it('retries after a 400 error', verifyRecoverableHttpError(400));
it('stops sending events after a 401 error', verifyUnrecoverableHttpError(401));
it('stops sending events after a 403 error', verifyUnrecoverableHttpError(403));
it('retries after a 408 error', verifyRecoverableHttpError(408));
it('retries after a 429 error', verifyRecoverableHttpError(429));
it('retries after a 503 error', verifyRecoverableHttpError(503));
it('swallows errors from failed background flush', eventsServerTest(async s => {
// This test verifies that when a background flush fails, we don't emit an unhandled
// promise rejection. Jest will fail the test if we do that.
const config = Object.assign({}, defaultConfig, { flushInterval: 0.25 });
await withEventProcessor(config, s, async ep => {
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(500));
ep.sendEvent({ kind: 'identify', creationDate: 1000, context: user });
// unfortunately we must wait for both the flush interval and the 1-second retry interval
await failOnTimeout(s.nextRequest(), 500, 'timed out waiting for event payload');
await failOnTimeout(s.nextRequest(), 1500, 'timed out waiting for event payload');
});
}));
describe('diagnostic events', () => {
it('sends initial diagnostic event', eventsServerTest(async s => {
const startTime = new Date().getTime();
await withDiagnosticEventProcessor(defaultConfig, s, async (ep, id) => {
const req = await s.nextRequest();
expect(req.path).toEqual('/diagnostic');
const data = JSON.parse(req.body);
expect(data.kind).toEqual('diagnostic-init');
expect(data.id).toEqual(id);
expect(data.creationDate).toBeGreaterThanOrEqual(startTime);
expect(data.configuration).toMatchObject({ customEventsURI: true });
expect(data.sdk).toMatchObject({ name: 'node-server-sdk' });
expect(data.platform).toMatchObject({ name: 'Node' });
});
}));
it('sends periodic diagnostic event', eventsServerTest(async s => {
const startTime = new Date().getTime();
const config = Object.assign({}, defaultConfig, { diagnosticRecordingInterval: 0.1 });
await withDiagnosticEventProcessor(config, s, async (ep, id) => {
const req0 = await s.nextRequest();
expect(req0.path).toEqual('/diagnostic');
const req1 = await s.nextRequest();
expect(req1.path).toEqual('/diagnostic');
const data = JSON.parse(req1.body);
expect(data.kind).toEqual('diagnostic');
expect(data.id).toEqual(id);
expect(data.creationDate).toBeGreaterThanOrEqual(startTime);
expect(data.dataSinceDate).toBeGreaterThanOrEqual(startTime);
expect(data.droppedEvents).toEqual(0);
expect(data.deduplicatedUsers).toEqual(0);
expect(data.eventsInLastBatch).toEqual(0);
});
}));
it('counts events in queue from last flush and dropped events', eventsServerTest(async s => {
const startTime = new Date().getTime();
const config = Object.assign({}, defaultConfig, { diagnosticRecordingInterval: 0.1, capacity: 2 });
await withDiagnosticEventProcessor(config, s, async (ep, id) => {
const req0 = await s.nextRequest();
expect(req0.path).toEqual('/diagnostic');
ep.sendEvent({ kind: 'identify', creationDate: 1000, context: user });
ep.sendEvent({ kind: 'identify', creationDate: 1001, context: user });
ep.sendEvent({ kind: 'identify', creationDate: 1002, context: user });
await ep.flush();
// We can't be sure which will be posted first, the regular events or the diagnostic event
const requests = [];
const req1 = await s.nextRequest();
requests.push({ path: req1.path, data: JSON.parse(req1.body) });
const req2 = await s.nextRequest();
requests.push({ path: req2.path, data: JSON.parse(req2.body) });
expect(requests).toContainEqual({
path: '/bulk',
data: expect.arrayContaining([
expect.objectContaining({ kind: 'identify', creationDate: 1000 }),
expect.objectContaining({ kind: 'identify', creationDate: 1001 }),
]),
});
expect(requests).toContainEqual({
path: '/diagnostic',
data: expect.objectContaining({
kind: 'diagnostic',
id: id,
droppedEvents: 1,
deduplicatedUsers: 0,
eventsInLastBatch: 2,
}),
});
});
}));
it('counts deduplicated users', eventsServerTest(async s => {
const startTime = new Date().getTime();
const config = Object.assign({}, defaultConfig, { diagnosticRecordingInterval: 0.1 });
await withDiagnosticEventProcessor(config, s, async (ep, id) => {
const req0 = await s.nextRequest();
expect(req0.path).toEqual('/diagnostic');
ep.sendEvent({ kind: 'track', key: 'eventkey1', creationDate: 1000, context: user });
ep.sendEvent({ kind: 'track', key: 'eventkey2', creationDate: 1001, context: user });
await ep.flush();
// We can't be sure which will be posted first, the regular events or the diagnostic event
const requests = [];
const req1 = await s.nextRequest();
requests.push({ path: req1.path, data: JSON.parse(req1.body) });
const req2 = await s.nextRequest();
requests.push({ path: req2.path, data: JSON.parse(req2.body) });
expect(requests).toContainEqual({
path: '/bulk',
data: expect.arrayContaining([
expect.objectContaining({ kind: 'track', creationDate: 1000 }),
expect.objectContaining({ kind: 'track', creationDate: 1001 }),
]),
});
expect(requests).toContainEqual({
path: '/diagnostic',
data: expect.objectContaining({
kind: 'diagnostic',
id: id,
droppedEvents: 0,
deduplicatedUsers: 1,
eventsInLastBatch: 3, // 2 "track" + 1 "index"
}),
});
});
}));
});
});