Skip to content

Commit

Permalink
fix: consider default client when loading states
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Warmuth <[email protected]>
  • Loading branch information
warber committed Nov 28, 2024
1 parent 73c0e85 commit 1cb57c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 12 additions & 5 deletions openfeature/event_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (e *eventExecutor) GetClientRegistry(client string) scopedCallback {
// emitOnRegistration fulfils the spec requirement to fire events if the
// event type and the state of the associated provider are compatible.
func (e *eventExecutor) emitOnRegistration(domain string, providerReference providerReference, eventType EventType, callback EventCallback) {
state, ok := e.states.Load(domain)
state, ok := e.loadState(domain)
if !ok {
return
}
Expand All @@ -213,12 +213,19 @@ func (e *eventExecutor) emitOnRegistration(domain string, providerReference prov
}
}

func (e *eventExecutor) State(domain string) State {
s, ok := e.states.Load(domain)
func (e *eventExecutor) loadState(domain string) (State, bool) {
state, ok := e.states.Load(domain)
if !ok {
return NotReadyState
if state, ok = e.states.Load(defaultClient); !ok {
return NotReadyState, false
}
}
return s.(State)
return state.(State), true
}

func (e *eventExecutor) State(domain string) State {
state, _ := e.loadState(domain)
return state
}

// registerDefaultProvider registers the default FeatureProvider and remove the old default provider if available
Expand Down
17 changes: 8 additions & 9 deletions openfeature/event_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
defer t.Cleanup(initSingleton)

eventingImpl := &ProviderEventing{
c: make(chan Event, 1),
c: make(chan Event),
}

readyEventingProvider := struct {
Expand Down Expand Up @@ -673,7 +673,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
defer t.Cleanup(initSingleton)

eventingImpl := &ProviderEventing{
c: make(chan Event, 1),
c: make(chan Event),
}

readyEventingProvider := struct {
Expand All @@ -685,7 +685,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
}

clientAssociation := "clientA"
err := SetNamedProvider(clientAssociation, readyEventingProvider)
err := SetNamedProviderAndWait(clientAssociation, readyEventingProvider)
if err != nil {
t.Fatal(err)
}
Expand All @@ -695,7 +695,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
rsp <- e
}

client := NewClient(clientAssociation)
client := api.GetNamedClient(clientAssociation)
client.AddHandler(ProviderReady, &callback)

select {
Expand All @@ -710,9 +710,8 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
defer t.Cleanup(initSingleton)

eventingImpl := &ProviderEventing{
c: make(chan Event, 1),
c: make(chan Event),
}
eventingImpl.Invoke(Event{EventType: ProviderConfigChange})

readyEventingProvider := struct {
FeatureProvider
Expand All @@ -722,7 +721,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
eventingImpl,
}

err := SetProvider(readyEventingProvider)
err := SetProviderAndWait(readyEventingProvider)
if err != nil {
t.Fatal(err)
}
Expand All @@ -732,7 +731,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
rsp <- e
}

client := NewClient("someClient")
client := api.GetNamedClient("someClient")
client.AddHandler(ProviderReady, &callback)

select {
Expand Down Expand Up @@ -770,7 +769,7 @@ func TestEventHandler_ProviderReadiness(t *testing.T) {
rsp <- e
}

client := NewClient("someClient")
client := api.GetNamedClient("someClient")
client.AddHandler(ProviderReady, &callback)

select {
Expand Down

0 comments on commit 1cb57c5

Please sign in to comment.