Skip to content

Commit 7eb06d7

Browse files
committed
fix: only shut down active providers
The spec says that shutdown must shut down active providers, but the current logic shuts down inactive providers. This change fixes that. Separately, the spec might also need to be modified to say "only" active providers. Signed-off-by: Brandon Duffany <[email protected]>
1 parent 5b0218b commit 7eb06d7

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

openfeature/openfeature.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ func RemoveHandler(eventType EventType, callback EventCallback) {
8888
api.RemoveHandler(eventType, callback)
8989
}
9090

91-
// Shutdown active providers
91+
// Shutdown unconditionally calls shutdown on all registered providers,
92+
// regardless of their state. It resets the state of the API, removing all
93+
// hooks, event handlers, and providers.
9294
func Shutdown() {
9395
api.Shutdown()
96+
initSingleton()
9497
}

openfeature/openfeature_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,59 @@ func TestRequirement_1_6_1(t *testing.T) {
585585
}
586586
}
587587

588+
// The API's `shutdown` function MUST reset all state of the API, removing all
589+
// hooks, event handlers, and providers.
590+
func TestRequirement_1_6_2(t *testing.T) {
591+
t.Cleanup(initSingleton)
592+
593+
// TODO: test that hooks and event handlers are removed as well. This only
594+
// tests that providers are removed.
595+
596+
provider1, _, shutdownSem1 := setupProviderWithSemaphores()
597+
598+
// Setup provider and wait for initialization done
599+
err := SetProviderAndWait(provider1)
600+
if err != nil {
601+
t.Errorf("error setting up provider %v", err)
602+
}
603+
604+
Shutdown()
605+
606+
// Shutdown should be synchronous. Try a non-blocking receive and fail
607+
// immediately if there is not a value in the channel.
608+
select {
609+
case <-shutdownSem1:
610+
break
611+
default:
612+
t.Fatalf("shutdown not invoked")
613+
}
614+
615+
// Try shutting down again, and make sure that provider1 is not shut down
616+
// again, since it is now inactive.
617+
provider2, _, shutdownSem2 := setupProviderWithSemaphores()
618+
619+
err = SetProviderAndWait(provider2)
620+
if err != nil {
621+
t.Errorf("error setting up provider %v", err)
622+
}
623+
624+
Shutdown()
625+
626+
select {
627+
case <-shutdownSem2:
628+
break
629+
default:
630+
t.Fatalf("shutdown not invoked")
631+
}
632+
633+
select {
634+
case <-shutdownSem1:
635+
t.Fatalf("provider1 should not have been shut down again, since it is unregistered")
636+
case <-time.After(100 * time.Millisecond):
637+
break
638+
}
639+
}
640+
588641
func TestRequirement_EventCompliance(t *testing.T) {
589642
// The client MUST provide a function for associating handler functions with a particular provider event type.
590643
// The API and client MUST provide a function allowing the removal of event handlers.

0 commit comments

Comments
 (0)