-
Notifications
You must be signed in to change notification settings - Fork 18
/
ldclient_offline_test.go
77 lines (66 loc) · 2.34 KB
/
ldclient_offline_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
package ldclient
import (
"testing"
"github.com/launchdarkly/go-server-sdk/v7/internal/sharedtest/mocks"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/internal/datastore"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
"github.com/stretchr/testify/assert"
)
type clientOfflineTestParams struct {
client *LDClient
store subsystems.DataStore
mockLog *ldlogtest.MockLog
}
func withClientOfflineTestParams(callback func(clientExternalUpdatesTestParams)) {
p := clientExternalUpdatesTestParams{}
p.store = datastore.NewInMemoryDataStore(ldlog.NewDisabledLoggers())
p.mockLog = ldlogtest.NewMockLog()
config := Config{
Offline: true,
DataStore: mocks.SingleComponentConfigurer[subsystems.DataStore]{Instance: p.store},
Logging: ldcomponents.Logging().Loggers(p.mockLog.Loggers),
}
p.client, _ = MakeCustomClient("sdk_key", config, 0)
defer p.client.Close()
callback(p)
}
func TestClientOfflineMode(t *testing.T) {
t.Run("is initialized", func(t *testing.T) {
withClientOfflineTestParams(func(p clientExternalUpdatesTestParams) {
assert.True(t, p.client.Initialized())
assert.Equal(t, interfaces.DataSourceStateValid,
p.client.GetDataSourceStatusProvider().GetStatus().State)
})
})
t.Run("reports offline status", func(t *testing.T) {
withClientOfflineTestParams(func(p clientExternalUpdatesTestParams) {
assert.True(t, p.client.IsOffline())
})
})
t.Run("logs appropriate message at startup", func(t *testing.T) {
withClientOfflineTestParams(func(p clientExternalUpdatesTestParams) {
assert.Contains(
t,
p.mockLog.GetOutput(ldlog.Info),
"Starting LaunchDarkly client in offline mode",
)
})
})
t.Run("returns default values", func(t *testing.T) {
withClientOfflineTestParams(func(p clientExternalUpdatesTestParams) {
result, err := p.client.BoolVariation("flagkey", evalTestUser, false)
assert.NoError(t, err)
assert.False(t, result)
})
})
t.Run("returns invalid state from AllFlagsState", func(t *testing.T) {
withClientOfflineTestParams(func(p clientExternalUpdatesTestParams) {
result := p.client.AllFlagsState(evalTestUser)
assert.False(t, result.IsValid())
})
})
}