-
Notifications
You must be signed in to change notification settings - Fork 6
FF-3380 feat: add EppoClient.Initialized() method to wait for initialization #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package eppoclient | |
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
@@ -15,15 +16,15 @@ var ( | |
|
||
func Test_AssignBlankExperiment(t *testing.T) { | ||
var mockLogger = new(mockLogger) | ||
client := newEppoClient(newConfigurationStore(configuration{}), nil, nil, mockLogger, applicationLogger) | ||
client := newEppoClient(newConfigurationStore(), nil, nil, mockLogger, applicationLogger) | ||
|
||
_, err := client.GetStringAssignment("", "subject-1", Attributes{}, "") | ||
assert.Error(t, err) | ||
} | ||
|
||
func Test_AssignBlankSubject(t *testing.T) { | ||
var mockLogger = new(mockLogger) | ||
client := newEppoClient(newConfigurationStore(configuration{}), nil, nil, mockLogger, applicationLogger) | ||
client := newEppoClient(newConfigurationStore(), nil, nil, mockLogger, applicationLogger) | ||
|
||
_, err := client.GetStringAssignment("experiment-1", "", Attributes{}, "") | ||
assert.Error(t, err) | ||
|
@@ -96,7 +97,7 @@ func Test_LogAssignment(t *testing.T) { | |
}, | ||
} | ||
|
||
client := newEppoClient(newConfigurationStore(configuration{flags: config}), nil, nil, mockLogger, applicationLogger) | ||
client := newEppoClient(newConfigurationStoreWithConfig(configuration{flags: config}), nil, nil, mockLogger, applicationLogger) | ||
|
||
assignment, err := client.GetStringAssignment("experiment-key-1", "user-1", Attributes{}, "") | ||
expected := "control" | ||
|
@@ -141,7 +142,7 @@ func Test_client_loggerIsCalledWithProperBanditEvent(t *testing.T) { | |
}, | ||
} | ||
|
||
client := newEppoClient(newConfigurationStore(configuration{flags: flags, bandits: bandits}), nil, nil, logger, applicationLogger) | ||
client := newEppoClient(newConfigurationStoreWithConfig(configuration{flags: flags, bandits: bandits}), nil, nil, logger, applicationLogger) | ||
actions := map[string]ContextAttributes{ | ||
"action1": {}, | ||
} | ||
|
@@ -194,7 +195,7 @@ func Test_GetStringAssignmentHandlesLoggingPanic(t *testing.T) { | |
}, | ||
}} | ||
|
||
client := newEppoClient(newConfigurationStore(configuration{flags: config}), nil, nil, mockLogger, applicationLogger) | ||
client := newEppoClient(newConfigurationStoreWithConfig(configuration{flags: config}), nil, nil, mockLogger, applicationLogger) | ||
|
||
assignment, err := client.GetStringAssignment("experiment-key-1", "user-1", Attributes{}, "") | ||
expected := "control" | ||
|
@@ -236,7 +237,7 @@ func Test_client_handlesBanditLoggerPanic(t *testing.T) { | |
}, | ||
} | ||
|
||
client := newEppoClient(newConfigurationStore(configuration{flags: flags, bandits: bandits}), nil, nil, logger, applicationLogger) | ||
client := newEppoClient(newConfigurationStoreWithConfig(configuration{flags: flags, bandits: bandits}), nil, nil, logger, applicationLogger) | ||
actions := map[string]ContextAttributes{ | ||
"action1": {}, | ||
} | ||
|
@@ -278,7 +279,7 @@ func Test_client_correctActionIsReturnedIfBanditLoggerPanics(t *testing.T) { | |
}, | ||
} | ||
|
||
client := newEppoClient(newConfigurationStore(configuration{flags: flags, bandits: bandits}), nil, nil, logger, applicationLogger) | ||
client := newEppoClient(newConfigurationStoreWithConfig(configuration{flags: flags, bandits: bandits}), nil, nil, logger, applicationLogger) | ||
actions := map[string]ContextAttributes{ | ||
"action1": {}, | ||
} | ||
|
@@ -290,3 +291,39 @@ func Test_client_correctActionIsReturnedIfBanditLoggerPanics(t *testing.T) { | |
Action: &expectedAction, | ||
}, result) | ||
} | ||
|
||
func Test_Initialized_timeout(t *testing.T) { | ||
var mockLogger = new(mockLogger) | ||
client := newEppoClient(newConfigurationStore(), nil, nil, mockLogger, applicationLogger) | ||
|
||
timedOut := false | ||
select { | ||
case <-client.Initialized(): | ||
timedOut = false | ||
case <-time.After(1 * time.Millisecond): | ||
timedOut = true | ||
Comment on lines
+301
to
+304
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to ChatGPT, this select block will wait for one of these two channels to complete, similar to JavaScript's Promise.race() 👌 |
||
} | ||
|
||
assert.True(t, timedOut) | ||
} | ||
|
||
func Test_Initialized_success(t *testing.T) { | ||
var mockLogger = new(mockLogger) | ||
configurationStore := newConfigurationStore() | ||
client := newEppoClient(configurationStore, nil, nil, mockLogger, applicationLogger) | ||
|
||
go func() { | ||
<-time.After(1 * time.Microsecond) | ||
configurationStore.setConfiguration(configuration{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}() | ||
|
||
timedOut := false | ||
select { | ||
case <-client.Initialized(): | ||
timedOut = false | ||
case <-time.After(1 * time.Millisecond): | ||
timedOut = true | ||
} | ||
|
||
assert.False(t, timedOut) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file was extracted from |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package eppoclient | ||
|
||
type configuration struct { | ||
flags configResponse | ||
bandits banditResponse | ||
// flag key -> variation value -> banditVariation. | ||
// | ||
// This is cached from `flags` field for easier access in | ||
// evaluation. | ||
banditFlagAssociations map[string]map[string]banditVariation | ||
} | ||
|
||
func (c *configuration) precompute() { | ||
associations := make(map[string]map[string]banditVariation) | ||
|
||
c.flags.precompute() | ||
|
||
for _, banditVariations := range c.flags.Bandits { | ||
for _, bandit := range banditVariations { | ||
byVariation, ok := associations[bandit.FlagKey] | ||
if !ok { | ||
byVariation = make(map[string]banditVariation) | ||
associations[bandit.FlagKey] = byVariation | ||
} | ||
byVariation[bandit.VariationValue] = bandit | ||
} | ||
} | ||
|
||
c.banditFlagAssociations = associations | ||
} | ||
|
||
func (c configuration) getBanditVariant(flagKey, variation string) (result banditVariation, ok bool) { | ||
byVariation, ok := c.banditFlagAssociations[flagKey] | ||
if !ok { | ||
return result, false | ||
} | ||
result, ok = byVariation[variation] | ||
return result, ok | ||
} | ||
|
||
func (c configuration) getFlagConfiguration(key string) (*flagConfiguration, error) { | ||
flag, ok := c.flags.Flags[key] | ||
if !ok { | ||
return nil, ErrFlagConfigurationNotFound | ||
} | ||
|
||
return flag, nil | ||
} | ||
|
||
func (c configuration) getBanditConfiguration(key string) (banditConfiguration, error) { | ||
bandit, ok := c.bandits.Bandits[key] | ||
if !ok { | ||
return bandit, ErrBanditConfigurationNotFound | ||
} | ||
|
||
return bandit, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package eppoclient | ||
|
||
var __version__ = "6.0.0" | ||
var __version__ = "6.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌