@@ -127,7 +127,7 @@ func (e *EventManagerTestSuite) TestTickerWhenODPConfigIsUpdated() {
127
127
em := NewBatchEventManager (WithFlushInterval (flushInterval ),
128
128
WithAPIManager (e .eventAPIManager ))
129
129
e .eventAPIManager .wg .Add (1 )
130
- em .ProcessEvent ("a" , "b" , Event {})
130
+ em .ProcessEvent ("a" , "b" , Event {Action : "123" })
131
131
eg := newExecutionContext ()
132
132
odpConfig := config .NewConfig ("a" , "b" , nil )
133
133
eg .Go (func (ctx context.Context ) {
@@ -142,15 +142,15 @@ func (e *EventManagerTestSuite) TestTickerWhenODPConfigIsUpdated() {
142
142
// Check events fired with updated config using ticker
143
143
e .eventAPIManager .wg .Add (1 )
144
144
odpConfig .Update ("c" , "d" , nil )
145
- em .ProcessEvent ("a" , "b" , Event {})
145
+ em .ProcessEvent ("a" , "b" , Event {Action : "123" })
146
146
e .eventAPIManager .wg .Wait ()
147
147
e .Equal ("c" , e .eventAPIManager .apiKey )
148
148
e .Equal ("d" , e .eventAPIManager .apiHost )
149
149
}
150
150
151
151
func (e * EventManagerTestSuite ) TestEventsDispatchedWhenContextIsTerminated () {
152
152
eg := newExecutionContext ()
153
- e .eventManager .eventQueue .Add (Event {})
153
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
154
154
e .eventAPIManager .wg .Add (1 )
155
155
e .Equal (1 , e .eventManager .eventQueue .Size ())
156
156
eg .Go (func (ctx context.Context ) {
@@ -165,7 +165,7 @@ func (e *EventManagerTestSuite) TestEventsDispatchedWhenContextIsTerminated() {
165
165
func (e * EventManagerTestSuite ) TestEventsDispatchedWhenFlushIntervalReached () {
166
166
eg := newExecutionContext ()
167
167
e .eventManager .flushInterval = 50 * time .Millisecond
168
- e .eventManager .eventQueue .Add (Event {})
168
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
169
169
e .eventAPIManager .wg .Add (1 )
170
170
e .Equal (1 , e .eventManager .eventQueue .Size ())
171
171
eg .Go (func (ctx context.Context ) {
@@ -202,7 +202,7 @@ func (e *EventManagerTestSuite) TestIdentifyUserWhenODPIntegrated() {
202
202
203
203
func (e * EventManagerTestSuite ) TestProcessEventWithInvalidODPConfig () {
204
204
em := NewBatchEventManager (WithAPIManager (& MockEventAPIManager {}))
205
- e .Error (em .ProcessEvent ("" , "" , Event {}))
205
+ e .Error (em .ProcessEvent ("" , "" , Event {Action : "123" }))
206
206
e .Equal (0 , em .eventQueue .Size ())
207
207
}
208
208
@@ -224,6 +224,37 @@ func (e *EventManagerTestSuite) TestProcessEventWithValidEventData() {
224
224
e .Equal (1 , e .eventManager .eventQueue .Size ())
225
225
}
226
226
227
+ func (e * EventManagerTestSuite ) TestProcessEventWithValidUserIdentifiers () {
228
+ em := NewBatchEventManager (WithAPIManager (& MockEventAPIManager {}))
229
+ validIdentifiers := []string {utils .OdpFSUserIDKey , "fs-user-id" , "FS-USER-ID" , "FS_USER_ID" }
230
+ for i , userID := range validIdentifiers {
231
+ expectedValue := fmt .Sprintf ("%d" , i )
232
+ tmpEvent := Event {
233
+ Type : "t1" ,
234
+ Action : "a1" ,
235
+ Identifiers : map [string ]string {userID : expectedValue },
236
+ Data : map [string ]interface {}{
237
+ "key11" : "value-1" ,
238
+ },
239
+ }
240
+
241
+ e .NoError (em .ProcessEvent ("1" , "2" , tmpEvent ))
242
+ e .Equal (i + 1 , em .eventQueue .Size ())
243
+ // Check event was added to queue
244
+ sentEvent := em .eventQueue .Get (i + 1 )
245
+ fEvent , exists := sentEvent [i ].(Event )
246
+ e .True (exists )
247
+
248
+ // Check valid key was added
249
+ e .Equal (expectedValue , fEvent .Identifiers [utils .OdpFSUserIDKey ])
250
+ // Check old key was deleted
251
+ if validIdentifiers [i ] != utils .OdpFSUserIDKey {
252
+ _ , exists := fEvent.Identifiers [validIdentifiers [i ]]
253
+ e .False (exists )
254
+ }
255
+ }
256
+ }
257
+
227
258
func (e * EventManagerTestSuite ) TestProcessEventWithInvalidEventData () {
228
259
tmpEvent := Event {
229
260
Type : "t1" ,
@@ -238,16 +269,44 @@ func (e *EventManagerTestSuite) TestProcessEventWithInvalidEventData() {
238
269
e .Equal (0 , e .eventManager .eventQueue .Size ())
239
270
}
240
271
272
+ func (e * EventManagerTestSuite ) TestProcessEventWithValidAction () {
273
+ tmpEvent := Event {
274
+ Type : "t1" ,
275
+ Action : "a1" ,
276
+ Identifiers : map [string ]string {"id-key-1" : "id-value-1" },
277
+ Data : map [string ]interface {}{
278
+ "key11" : "value-1" ,
279
+ },
280
+ }
281
+
282
+ e .NoError (e .eventManager .ProcessEvent ("1" , "2" , tmpEvent ))
283
+ e .Equal (1 , e .eventManager .eventQueue .Size ())
284
+ }
285
+
286
+ func (e * EventManagerTestSuite ) TestProcessEventWithInvalidAction () {
287
+ tmpEvent := Event {
288
+ Type : "t1" ,
289
+ Action : "" ,
290
+ Identifiers : map [string ]string {"id-key-1" : "id-value-1" },
291
+ Data : map [string ]interface {}{
292
+ "key11" : "value-1" ,
293
+ },
294
+ }
295
+
296
+ e .Equal (errors .New (utils .OdpInvalidAction ), e .eventManager .ProcessEvent ("a" , "b" , tmpEvent ))
297
+ e .Equal (0 , e .eventManager .eventQueue .Size ())
298
+ }
299
+
241
300
func (e * EventManagerTestSuite ) TestProcessEventDiscardsEventExceedingMaxQueueSize () {
242
301
e .eventManager .maxQueueSize = 1
243
- e .eventManager .eventQueue .Add (Event {})
244
- e .Error (e .eventManager .ProcessEvent ("a" , "b" , Event {}))
302
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
303
+ e .Error (e .eventManager .ProcessEvent ("a" , "b" , Event {Action : "123" }))
245
304
e .Equal (1 , e .eventManager .eventQueue .Size ())
246
305
}
247
306
248
307
func (e * EventManagerTestSuite ) TestProcessEventWithBatchSizeNotReached () {
249
308
em := NewBatchEventManager (WithAPIManager (& MockEventAPIManager {}))
250
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
309
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
251
310
e .Equal (1 , em .eventQueue .Size ())
252
311
e .Equal (0 , e .eventAPIManager .timesSendEventsCalled )
253
312
}
@@ -257,7 +316,7 @@ func (e *EventManagerTestSuite) TestProcessEventWithBatchSizeReached() {
257
316
em := NewBatchEventManager (WithAPIManager (apiManager ), WithFlushInterval (0 ))
258
317
e .Equal (0 , em .eventQueue .Size ())
259
318
apiManager .wg .Add (1 )
260
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
319
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
261
320
// Wait for event fire through go routine
262
321
apiManager .wg .Wait ()
263
322
e .Equal (0 , em .eventQueue .Size ())
@@ -267,12 +326,12 @@ func (e *EventManagerTestSuite) TestProcessEventWithBatchSizeReached() {
267
326
func (e * EventManagerTestSuite ) TestProcessEventsExceedingBatchSize () {
268
327
apiManager := & MockEventAPIManager {}
269
328
em := NewBatchEventManager (WithAPIManager (apiManager ), WithFlushInterval (0 ))
270
- em .eventQueue .Add (Event {})
271
- em .eventQueue .Add (Event {})
329
+ em .eventQueue .Add (Event {Action : "123" })
330
+ em .eventQueue .Add (Event {Action : "123" })
272
331
e .Equal (2 , em .eventQueue .Size ())
273
332
// Three batch events should be fired
274
333
apiManager .wg .Add (3 )
275
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
334
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
276
335
// Wait for event fire through go routine
277
336
apiManager .wg .Wait ()
278
337
// Since all events fired successfully, queue should be empty
@@ -283,7 +342,7 @@ func (e *EventManagerTestSuite) TestProcessEventsExceedingBatchSize() {
283
342
func (e * EventManagerTestSuite ) TestProcessEventFirstEventFailsWithRetries () {
284
343
apiManager := & MockEventAPIManager {}
285
344
em := NewBatchEventManager (WithAPIManager (apiManager ), WithFlushInterval (0 ))
286
- em .eventQueue .Add (Event {})
345
+ em .eventQueue .Add (Event {Action : "123" })
287
346
e .Equal (1 , em .eventQueue .Size ())
288
347
// Return true for retry for all calls
289
348
apiManager .retryResponses = []bool {true , true , true }
@@ -293,7 +352,7 @@ func (e *EventManagerTestSuite) TestProcessEventFirstEventFailsWithRetries() {
293
352
// Total 2 events in queue which make 2 batches
294
353
// first batch will be retried thrice, second one wont be fired since first failed thrice
295
354
apiManager .wg .Add (maxRetries )
296
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
355
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
297
356
// Wait for three retries
298
357
apiManager .wg .Wait ()
299
358
// Since all events failed, queue should contain all events
@@ -304,14 +363,14 @@ func (e *EventManagerTestSuite) TestProcessEventFirstEventFailsWithRetries() {
304
363
func (e * EventManagerTestSuite ) TestProcessEventFirstEventFailsWithRetryNotAllowed () {
305
364
apiManager := & MockEventAPIManager {}
306
365
em := NewBatchEventManager (WithAPIManager (apiManager ), WithFlushInterval (0 ))
307
- em .eventQueue .Add (Event {})
366
+ em .eventQueue .Add (Event {Action : "123" })
308
367
e .Equal (1 , em .eventQueue .Size ())
309
368
apiManager .retryResponses = []bool {false }
310
369
tmpError := errors .New ("" )
311
370
apiManager .errResponses = []error {tmpError }
312
371
// first batch will not be retried, second one wont be fired since first failed
313
372
apiManager .wg .Add (1 )
314
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
373
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
315
374
// Wait for three retries
316
375
apiManager .wg .Wait ()
317
376
// Since first batch of 2 events failed with no retry allowed, queue should only contain 1 event
@@ -322,7 +381,7 @@ func (e *EventManagerTestSuite) TestProcessEventFirstEventFailsWithRetryNotAllow
322
381
func (e * EventManagerTestSuite ) TestProcessEventSecondEventFailsWithRetriesLaterPasses () {
323
382
apiManager := & MockEventAPIManager {}
324
383
em := NewBatchEventManager (WithAPIManager (apiManager ), WithFlushInterval (0 ))
325
- em .eventQueue .Add (Event {})
384
+ em .eventQueue .Add (Event {Action : "123" })
326
385
e .Equal (1 , em .eventQueue .Size ())
327
386
// Return true for retry for all second batch calls
328
387
apiManager .retryResponses = []bool {false , true , true , true , false , false }
@@ -332,7 +391,7 @@ func (e *EventManagerTestSuite) TestProcessEventSecondEventFailsWithRetriesLater
332
391
// Total 2 events in queue which make 2 batches
333
392
// first batch will be successfully dispatched, second will be retried thrice
334
393
apiManager .wg .Add (4 )
335
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
394
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
336
395
// Wait for events to fire
337
396
apiManager .wg .Wait ()
338
397
// Since second batch of 1 event failed, queue should be contain 1 event
@@ -344,7 +403,7 @@ func (e *EventManagerTestSuite) TestProcessEventSecondEventFailsWithRetriesLater
344
403
time .Sleep (200 * time .Millisecond )
345
404
346
405
apiManager .wg .Add (2 )
347
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
406
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
348
407
// Wait for events to fire
349
408
apiManager .wg .Wait ()
350
409
// Queue should be empty since remaining event was sent now
@@ -356,7 +415,7 @@ func (e *EventManagerTestSuite) TestProcessEventSecondEventFailsWithRetriesLater
356
415
func (e * EventManagerTestSuite ) TestProcessEventFirstEventPassesWithRetries () {
357
416
apiManager := & MockEventAPIManager {}
358
417
em := NewBatchEventManager (WithAPIManager (apiManager ), WithFlushInterval (0 ))
359
- em .eventQueue .Add (Event {})
418
+ em .eventQueue .Add (Event {Action : "123" })
360
419
e .Equal (1 , em .eventQueue .Size ())
361
420
// Return true for first batch call only
362
421
apiManager .retryResponses = []bool {true , false , false }
@@ -366,7 +425,7 @@ func (e *EventManagerTestSuite) TestProcessEventFirstEventPassesWithRetries() {
366
425
// Total 2 events in queue which make 2 batches
367
426
// first batch will be retried once, second will be successful immediately
368
427
apiManager .wg .Add (3 )
369
- e .NoError (em .ProcessEvent ("a" , "b" , Event {}))
428
+ e .NoError (em .ProcessEvent ("a" , "b" , Event {Action : "123" }))
370
429
// Wait for events to fire
371
430
apiManager .wg .Wait ()
372
431
// Since all events were successful, queue should be empty
@@ -384,7 +443,7 @@ func (e *EventManagerTestSuite) TestEventManagerAsyncBehaviour() {
384
443
eg := newExecutionContext ()
385
444
callAllMethods := func (id string ) {
386
445
eventManager .IdentifyUser ("-1" , "-1" , id )
387
- eventManager .ProcessEvent ("-1" , "-1" , Event {})
446
+ eventManager .ProcessEvent ("-1" , "-1" , Event {Action : "123" })
388
447
}
389
448
for i := 0 ; i < iterations ; i ++ {
390
449
eg .Go (func (ctx context.Context ) {
@@ -428,7 +487,7 @@ func (e *EventManagerTestSuite) TestFlushEventsAsyncBehaviour() {
428
487
}
429
488
430
489
func (e * EventManagerTestSuite ) TestAddCommonData () {
431
- userEvent := Event {}
490
+ userEvent := Event {Action : "123" }
432
491
e .eventManager .addCommonData (& userEvent )
433
492
e .NotNil (userEvent .Data )
434
493
e .NotEmpty (userEvent .Data ["idempotence_id" ])
@@ -437,6 +496,23 @@ func (e *EventManagerTestSuite) TestAddCommonData() {
437
496
e .Equal (event .Version , userEvent .Data ["data_source_version" ])
438
497
}
439
498
499
+ func (e * EventManagerTestSuite ) TestConvertIdentifiers () {
500
+ validKeys := []string {utils .OdpFSUserIDKey , "fs-user-id" , "FS-USER-ID" , "FS_USER_ID" }
501
+ expectedValue := "123"
502
+
503
+ for _ , k := range validKeys {
504
+ userEvent := Event {Identifiers : map [string ]string {k : "123" }}
505
+ e .eventManager .convertIdentifiers (& userEvent )
506
+ // Check valid key was added
507
+ e .Equal (expectedValue , userEvent .Identifiers [utils .OdpFSUserIDKey ])
508
+ // Check old key was deleted
509
+ if k != utils .OdpFSUserIDKey {
510
+ _ , exists := userEvent .Identifiers [k ]
511
+ e .False (exists )
512
+ }
513
+ }
514
+ }
515
+
440
516
func (e * EventManagerTestSuite ) TestUserDataOverridesCommonData () {
441
517
userEvent := Event {Data : map [string ]interface {}{
442
518
"abc" : nil ,
@@ -456,17 +532,17 @@ func (e *EventManagerTestSuite) TestUserDataOverridesCommonData() {
456
532
457
533
func (e * EventManagerTestSuite ) TestIsOdpServiceIntegrated () {
458
534
e .True (e .eventManager .IsOdpServiceIntegrated ("a" , "b" ))
459
- e .eventManager .eventQueue .Add (Event {})
535
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
460
536
e .Equal (1 , e .eventManager .eventQueue .Size ())
461
537
462
538
e .False (e .eventManager .IsOdpServiceIntegrated ("" , "" ))
463
539
e .Equal (0 , e .eventManager .eventQueue .Size ())
464
540
465
- e .eventManager .eventQueue .Add (Event {})
541
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
466
542
e .False (e .eventManager .IsOdpServiceIntegrated ("a" , "" ))
467
543
e .Equal (0 , e .eventManager .eventQueue .Size ())
468
544
469
- e .eventManager .eventQueue .Add (Event {})
545
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
470
546
e .False (e .eventManager .IsOdpServiceIntegrated ("" , "b" ))
471
547
e .Equal (0 , e .eventManager .eventQueue .Size ())
472
548
}
@@ -475,7 +551,7 @@ func (e *EventManagerTestSuite) TestEventQueueRaceCondition() {
475
551
testIterations := 10
476
552
var wg sync.WaitGroup
477
553
asyncfunc := func () {
478
- e .eventManager .eventQueue .Add (Event {})
554
+ e .eventManager .eventQueue .Add (Event {Action : "123" })
479
555
e .eventManager .eventQueue .Size ()
480
556
e .eventManager .eventQueue .Get (1 )
481
557
e .eventManager .eventQueue .Remove (1 )
0 commit comments