diff --git a/internal/datasystem/data_availability.go b/internal/datasystem/data_availability.go index 5e0fecbd..e3ad2a73 100644 --- a/internal/datasystem/data_availability.go +++ b/internal/datasystem/data_availability.go @@ -11,3 +11,9 @@ const ( // Refreshed means the SDK has obtained, at least once, the latest known data from LaunchDarkly. Refreshed = DataAvailability("refreshed") ) + +// AtLeast returns true if the DataAvailability is at least as good as the +// other DataAvailability in terms of data quality. +func (da DataAvailability) AtLeast(other DataAvailability) bool { + return da == Refreshed || (da == Cached && other == Defaults) +} diff --git a/internal/datasystem/data_availability_test.go b/internal/datasystem/data_availability_test.go new file mode 100644 index 00000000..bb35e14c --- /dev/null +++ b/internal/datasystem/data_availability_test.go @@ -0,0 +1,21 @@ +package datasystem + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDataAvailibilityAtLeast(t *testing.T) { + assert.True(t, Refreshed.AtLeast(Refreshed)) + assert.True(t, Refreshed.AtLeast(Cached)) + assert.True(t, Refreshed.AtLeast(Defaults)) + + assert.False(t, Cached.AtLeast(Refreshed)) + assert.True(t, Cached.AtLeast(Cached)) + assert.True(t, Cached.AtLeast(Defaults)) + + assert.False(t, Defaults.AtLeast(Refreshed)) + assert.False(t, Defaults.AtLeast(Cached)) + assert.True(t, Defaults.AtLeast(Defaults)) +} diff --git a/ldclient.go b/ldclient.go index 308822cf..ce5b5662 100644 --- a/ldclient.go +++ b/ldclient.go @@ -345,7 +345,7 @@ func MakeCustomClient(sdkKey string, config Config, waitFor time.Duration) (*LDC for { select { case <-closeWhenReady: - if client.dataSystem.DataAvailability() != client.dataSystem.TargetAvailability() { + if client.dataSystem.DataAvailability().AtLeast(client.dataSystem.TargetAvailability()) { loggers.Warn("LaunchDarkly client initialization failed") return client, ErrInitializationFailed }