-
Notifications
You must be signed in to change notification settings - Fork 33
/
event_executor_test.go
529 lines (428 loc) · 14.4 KB
/
event_executor_test.go
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
package openfeature
import (
"context"
"github.com/go-logr/logr"
"github.com/open-feature/go-sdk/pkg/openfeature/internal"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"reflect"
"testing"
"time"
)
var logger logr.Logger
func init() {
logger = logr.New(internal.Logger{})
}
// Requirement 5.1.1 The provider MAY define a mechanism for signaling the occurrence of one of a set of events,
// including PROVIDER_READY, PROVIDER_ERROR, PROVIDER_CONFIGURATION_CHANGED and PROVIDER_STALE,
// with a provider event details payload.
func TestEventHandler_RegisterUnregisterEventProvider(t *testing.T) {
t.Run("Ignored non-eventing providers", func(t *testing.T) {
handler := newEventHandler(logger)
handler.registerEventingProvider(NoopProvider{})
if len(handler.providerShutdownHook) != 0 {
t.Errorf("implementation should ignore non eventing provider")
}
err := handler.unregisterEventingProvider(NoopProvider{})
if err != nil {
t.Error(err)
}
})
t.Run("Accepts addition and removal of eventing providers", func(t *testing.T) {
eventingImpl := &ProviderEventing{}
eventingProvider := struct {
FeatureProvider
EventHandler
}{
NoopProvider{},
eventingImpl,
}
handler := newEventHandler(logger)
handler.registerEventingProvider(eventingProvider)
if len(handler.providerShutdownHook) != 1 {
t.Errorf("implementation should register eventing provider")
}
err := handler.unregisterEventingProvider(eventingProvider)
if err != nil {
t.Error(err)
}
if len(handler.providerShutdownHook) != 0 {
t.Errorf("implementation should allow removal of eventing provider")
}
// double removal check for nil check & avoid panic
err = handler.unregisterEventingProvider(eventingProvider)
if err != nil {
t.Error(err)
}
})
}
// Requirement 5.1.2 When a provider signals the occurrence of a particular event,
// the associated client and API event handlers MUST run.
func TestEventHandler_Eventing(t *testing.T) {
t.Run("Simple API level event", func(t *testing.T) {
eventingImpl := &ProviderEventing{
c: make(chan Event, 1),
}
eventingProvider := struct {
FeatureProvider
EventHandler
}{
NoopProvider{},
eventingImpl,
}
handler := newEventHandler(logger)
handler.registerEventingProvider(eventingProvider)
rsp := make(chan EventDetails)
callBack := func(details EventDetails) {
rsp <- details
}
handler.registerApiHandler(ProviderReady, &callBack)
fCh := []string{"flagA"}
meta := map[string]interface{}{
"key": "value",
}
// trigger event from provider implementation
eventingImpl.Invoke(Event{
EventType: ProviderReady,
ProviderEventDetails: ProviderEventDetails{
Message: "ReadyMessage",
FlagChanges: fCh,
EventMetadata: meta,
},
})
// wait for execution
var result EventDetails
select {
case result = <-rsp:
break
case <-time.After(handlerExecutionTime):
t.Fatalf("timeout - event did not trigger")
}
if result.Message != "ReadyMessage" {
t.Errorf("expected %s, but got %s", "EventMessage", result.Message)
}
if !slices.Equal(result.FlagChanges, fCh) {
t.Errorf("flag changes are not equal")
}
if !reflect.DeepEqual(result.EventMetadata, meta) {
t.Errorf("metadata are not equal")
}
})
t.Run("Simple Client level event", func(t *testing.T) {
eventingImpl := &ProviderEventing{
c: make(chan Event, 1),
}
// NoopProvider backed event supported provider
eventingProvider := struct {
FeatureProvider
EventHandler
}{
NoopProvider{},
eventingImpl,
}
handler := newEventHandler(logger)
handler.registerEventingProvider(eventingProvider)
rsp := make(chan EventDetails)
callBack := func(details EventDetails) {
rsp <- details
}
// associated to NoopProvider
handler.registerClientHandler(eventingProvider.Metadata().Name, ProviderReady, &callBack)
fCh := []string{"flagA"}
meta := map[string]interface{}{
"key": "value",
}
// trigger event from provider implementation
eventingImpl.Invoke(Event{
ProviderName: eventingProvider.Metadata().Name,
EventType: ProviderReady,
ProviderEventDetails: ProviderEventDetails{
Message: "ReadyMessage",
FlagChanges: fCh,
EventMetadata: meta,
},
})
// wait for execution
var result EventDetails
select {
case result = <-rsp:
break
case <-time.After(handlerExecutionTime):
t.Fatalf("timeout - event did not trigger")
}
if result.client != eventingProvider.Metadata().Name {
t.Errorf("expected %s, but got %s", eventingProvider.Metadata().Name, result.client)
}
if result.Message != "ReadyMessage" {
t.Errorf("expected %s, but got %s", "EventMessage", result.Message)
}
if !slices.Equal(result.FlagChanges, fCh) {
t.Errorf("flag changes are not equal")
}
if !reflect.DeepEqual(result.EventMetadata, meta) {
t.Errorf("metadata are not equal")
}
})
}
// Requirement 5.1.3 When a provider signals the occurrence of a particular event,
// event handlers on clients which are not associated with that provider MUST NOT run.
func TestEventHandler_clientAssociation(t *testing.T) {
eventingImpl := &ProviderEventing{
c: make(chan Event, 1),
}
// NoopProvider backed event supported provider
eventingProvider := struct {
FeatureProvider
EventHandler
}{
NoopProvider{},
eventingImpl,
}
// event handler & provider registration
handler := newEventHandler(logger)
handler.registerEventingProvider(eventingProvider)
rsp := make(chan EventDetails)
callBack := func(details EventDetails) {
rsp <- details
}
event := ProviderReady
// register a random client - no association with registered provider
handler.registerClientHandler("someClient", event, &callBack)
// invoke provider event
eventingImpl.Invoke(Event{
ProviderName: eventingProvider.Metadata().Name,
EventType: event,
ProviderEventDetails: ProviderEventDetails{},
})
select {
case <-rsp:
t.Fatalf("incorrect association - handler must not have been invoked")
case <-time.After(handlerExecutionTime):
break
}
}
// Requirement 5.2.5 If a handler function terminates abnormally, other handler functions MUST run.
func TestEventHandler_ErrorHandling(t *testing.T) {
errorCallback := func(e EventDetails) {
panic("callback panic")
}
rsp := make(chan EventDetails, 1)
successAPICallback := func(e EventDetails) {
rsp <- e
}
rspClient := make(chan EventDetails, 1)
successClientCallback := func(e EventDetails) {
rspClient <- e
}
handler := newEventHandler(logger)
// api level handlers
handler.registerApiHandler(ProviderReady, &errorCallback)
handler.registerApiHandler(ProviderReady, &successAPICallback)
// provider association
provider := "providerA"
// client level handlers
handler.registerClientHandler(provider, ProviderReady, &errorCallback)
handler.registerClientHandler(provider, ProviderReady, &successClientCallback)
// trigger events manually
go func() {
_ = handler.triggerEvent(Event{
ProviderName: provider,
EventType: ProviderReady,
ProviderEventDetails: ProviderEventDetails{},
})
}()
select {
case <-rsp:
break
case <-time.After(handlerExecutionTime):
t.Error("API level callback timeout - handler recovery was not successful")
}
select {
case <-rspClient:
break
case <-time.After(handlerExecutionTime):
t.Error("client callback timeout - handler recovery was not successful")
}
}
// Make sure event handler cannot block
func TestEventHandler_Timeout(t *testing.T) {
timeoutCallback := func(e EventDetails) {
time.Sleep(handlerExecutionTime * 10)
}
handler := newEventHandler(logger)
handler.registerApiHandler(ProviderReady, &timeoutCallback)
group, ctx := errgroup.WithContext(context.Background())
group.Go(func() error {
return handler.triggerEvent(Event{
ProviderName: "provider",
EventType: ProviderReady,
ProviderEventDetails: ProviderEventDetails{},
})
})
select {
case <-ctx.Done():
break
case <-time.After(handlerExecutionTime * 2):
t.Fatalf("timeout while waiting for condition")
}
err := group.Wait()
if err == nil {
t.Errorf("expected timeout error, but got none")
}
}
// Contract tests - registration & removal
func TestEventHandler_Registration(t *testing.T) {
t.Run("API handlers", func(t *testing.T) {
handler := newEventHandler(logger)
// Add multiple - ProviderReady
handler.registerApiHandler(ProviderReady, &h1)
handler.registerApiHandler(ProviderReady, &h2)
handler.registerApiHandler(ProviderReady, &h3)
handler.registerApiHandler(ProviderReady, &h4)
// Add multiple - ProviderError
handler.registerApiHandler(ProviderError, &h2)
handler.registerApiHandler(ProviderError, &h2)
// Add single types
handler.registerApiHandler(ProviderStale, &h3)
handler.registerApiHandler(ProviderConfigChange, &h4)
readyLen := len(handler.apiRegistry[ProviderReady])
if readyLen != 4 {
t.Errorf("expected %d events, but got %d", 4, readyLen)
}
errLen := len(handler.apiRegistry[ProviderError])
if errLen != 2 {
t.Errorf("expected %d events, but got %d", 2, errLen)
}
staleLen := len(handler.apiRegistry[ProviderStale])
if staleLen != 1 {
t.Errorf("expected %d events, but got %d", 1, staleLen)
}
cfgLen := len(handler.apiRegistry[ProviderConfigChange])
if cfgLen != 1 {
t.Errorf("expected %d events, but got %d", 1, cfgLen)
}
})
t.Run("Client handlers", func(t *testing.T) {
handler := newEventHandler(logger)
// Add multiple - client a
handler.registerClientHandler("a", ProviderReady, &h1)
handler.registerClientHandler("a", ProviderReady, &h2)
handler.registerClientHandler("a", ProviderReady, &h3)
handler.registerClientHandler("a", ProviderReady, &h4)
// Add single for rest of the client
handler.registerClientHandler("b", ProviderError, &h2)
handler.registerClientHandler("c", ProviderStale, &h3)
handler.registerClientHandler("d", ProviderConfigChange, &h4)
readyLen := len(handler.scopedRegistry["a"].callbacks[ProviderReady])
if readyLen != 4 {
t.Errorf("expected %d events in client a, but got %d", 4, readyLen)
}
errLen := len(handler.scopedRegistry["b"].callbacks[ProviderError])
if errLen != 1 {
t.Errorf("expected %d events in client b, but got %d", 1, errLen)
}
staleLen := len(handler.scopedRegistry["c"].callbacks[ProviderStale])
if staleLen != 1 {
t.Errorf("expected %d events in client c, but got %d", 1, staleLen)
}
cfgLen := len(handler.scopedRegistry["d"].callbacks[ProviderConfigChange])
if cfgLen != 1 {
t.Errorf("expected %d events in client d, but got %d", 1, cfgLen)
}
})
}
func TestEventHandler_APIRemoval(t *testing.T) {
t.Run("API level removal", func(t *testing.T) {
handler := newEventHandler(logger)
// Add multiple - ProviderReady
handler.registerApiHandler(ProviderReady, &h1)
handler.registerApiHandler(ProviderReady, &h2)
handler.registerApiHandler(ProviderReady, &h3)
handler.registerApiHandler(ProviderReady, &h4)
// Add single types
handler.registerApiHandler(ProviderError, &h2)
handler.registerApiHandler(ProviderStale, &h3)
handler.registerApiHandler(ProviderConfigChange, &h4)
// removal
handler.removeApiHandler(ProviderReady, &h1)
handler.removeApiHandler(ProviderError, &h2)
handler.removeApiHandler(ProviderStale, &h3)
handler.removeApiHandler(ProviderConfigChange, &h4)
readyLen := len(handler.apiRegistry[ProviderReady])
if readyLen != 3 {
t.Errorf("expected %d events, but got %d", 3, readyLen)
}
if !slices.Contains(handler.apiRegistry[ProviderReady], &h2) {
t.Errorf("expected callback to be present")
}
if !slices.Contains(handler.apiRegistry[ProviderReady], &h3) {
t.Errorf("expected callback to be present")
}
if !slices.Contains(handler.apiRegistry[ProviderReady], &h3) {
t.Errorf("expected callback to be present")
}
errLen := len(handler.apiRegistry[ProviderError])
if errLen != 0 {
t.Errorf("expected %d events, but got %d", 0, errLen)
}
staleLen := len(handler.apiRegistry[ProviderStale])
if staleLen != 0 {
t.Errorf("expected %d events, but got %d", 0, staleLen)
}
cfgLen := len(handler.apiRegistry[ProviderConfigChange])
if cfgLen != 0 {
t.Errorf("expected %d events, but got %d", 0, cfgLen)
}
})
t.Run("Client level removal", func(t *testing.T) {
handler := newEventHandler(logger)
// Add multiple - client a
handler.registerClientHandler("a", ProviderReady, &h1)
handler.registerClientHandler("a", ProviderReady, &h2)
handler.registerClientHandler("a", ProviderReady, &h3)
handler.registerClientHandler("a", ProviderReady, &h4)
// Add single
handler.registerClientHandler("b", ProviderError, &h2)
handler.registerClientHandler("c", ProviderStale, &h3)
handler.registerClientHandler("d", ProviderConfigChange, &h4)
// removal
handler.removeClientHandler("a", ProviderReady, &h1)
handler.removeClientHandler("b", ProviderError, &h2)
handler.removeClientHandler("c", ProviderStale, &h3)
handler.removeClientHandler("d", ProviderConfigChange, &h4)
readyLen := len(handler.scopedRegistry["a"].callbacks[ProviderReady])
if readyLen != 3 {
t.Errorf("expected %d events in client a, but got %d", 3, readyLen)
}
if !slices.Contains(handler.scopedRegistry["a"].callbacks[ProviderReady], &h2) {
t.Errorf("expected callback to be present")
}
if !slices.Contains(handler.scopedRegistry["a"].callbacks[ProviderReady], &h3) {
t.Errorf("expected callback to be present")
}
if !slices.Contains(handler.scopedRegistry["a"].callbacks[ProviderReady], &h4) {
t.Errorf("expected callback to be present")
}
errLen := len(handler.scopedRegistry["b"].callbacks[ProviderError])
if errLen != 0 {
t.Errorf("expected %d events in client b, but got %d", 0, errLen)
}
staleLen := len(handler.scopedRegistry["c"].callbacks[ProviderStale])
if staleLen != 0 {
t.Errorf("expected %d events in client c, but got %d", 0, staleLen)
}
cfgLen := len(handler.scopedRegistry["d"].callbacks[ProviderConfigChange])
if cfgLen != 0 {
t.Errorf("expected %d events in client d, but got %d", 0, cfgLen)
}
// removal referenced to non-existing clients does nothing & no panics
handler.removeClientHandler("non-existing", ProviderReady, &h1)
handler.removeClientHandler("b", ProviderReady, &h1)
})
t.Run("remove handlers that were not added", func(t *testing.T) {
handler := newEventHandler(logger)
// removal of non-added handlers shall not panic
handler.removeApiHandler(ProviderReady, &h1)
handler.removeClientHandler("a", ProviderReady, &h1)
})
}