...
1 package ldcomponents
2
3 import (
4 "errors"
5 "testing"
6 "time"
7
8 "github.com/launchdarkly/go-server-sdk/v6/internal/sharedtest/mocks"
9
10 "github.com/launchdarkly/go-sdk-common/v3/ldlog"
11 "github.com/launchdarkly/go-sdk-common/v3/ldvalue"
12 "github.com/launchdarkly/go-server-sdk/v6/interfaces"
13 "github.com/launchdarkly/go-server-sdk/v6/internal"
14 "github.com/launchdarkly/go-server-sdk/v6/internal/datastore"
15 "github.com/launchdarkly/go-server-sdk/v6/internal/sharedtest"
16 "github.com/launchdarkly/go-server-sdk/v6/subsystems"
17
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
20 )
21
22 func TestPersistentDataStoreBuilder(t *testing.T) {
23 t.Run("factory", func(t *testing.T) {
24 pdsf := &mockPersistentDataStoreFactory{}
25 f := PersistentDataStore(pdsf)
26 assert.Equal(t, pdsf, f.persistentDataStoreFactory)
27 })
28
29 t.Run("calls factory", func(t *testing.T) {
30 pdsf := &mockPersistentDataStoreFactory{}
31 pdsf.store = mocks.NewMockPersistentDataStore()
32 f := PersistentDataStore(pdsf)
33
34 logConfig := subsystems.LoggingConfiguration{Loggers: ldlog.NewDisabledLoggers()}
35 clientContext := sharedtest.NewTestContext("", nil, &logConfig)
36 broadcaster := internal.NewBroadcaster[interfaces.DataStoreStatus]()
37 clientContext.DataStoreUpdateSink = datastore.NewDataStoreUpdateSinkImpl(broadcaster)
38
39 store, err := f.Build(clientContext)
40 assert.NoError(t, err)
41 require.NotNil(t, store)
42 _ = store.Close()
43 assert.Equal(t, clientContext.GetLogging(), pdsf.receivedContext.GetLogging())
44
45 pdsf.store = nil
46 pdsf.fakeError = errors.New("sorry")
47
48 store, err = f.Build(clientContext)
49 assert.Equal(t, pdsf.fakeError, err)
50 assert.Nil(t, store)
51 })
52
53 t.Run("CacheTime", func(t *testing.T) {
54 pdsf := &mockPersistentDataStoreFactory{}
55 f := PersistentDataStore(pdsf)
56
57 f.CacheTime(time.Hour)
58 assert.Equal(t, time.Hour, f.cacheTTL)
59 })
60
61 t.Run("CacheSeconds", func(t *testing.T) {
62 pdsf := &mockPersistentDataStoreFactory{}
63 f := PersistentDataStore(pdsf)
64
65 f.CacheSeconds(44)
66 assert.Equal(t, 44*time.Second, f.cacheTTL)
67 })
68
69 t.Run("CacheForever", func(t *testing.T) {
70 pdsf := &mockPersistentDataStoreFactory{}
71 f := PersistentDataStore(pdsf)
72
73 f.CacheForever()
74 assert.Equal(t, -1*time.Millisecond, f.cacheTTL)
75 })
76
77 t.Run("NoCaching", func(t *testing.T) {
78 pdsf := &mockPersistentDataStoreFactory{}
79 f := PersistentDataStore(pdsf)
80
81 f.NoCaching()
82 assert.Equal(t, time.Duration(0), f.cacheTTL)
83 })
84
85 t.Run("diagnostic description", func(t *testing.T) {
86 f1 := PersistentDataStore(&mockPersistentDataStoreFactory{})
87 assert.Equal(t, ldvalue.String("custom"), f1.DescribeConfiguration(basicClientContext()))
88
89 f2 := PersistentDataStore(&mockPersistentDataStoreFactoryWithDescription{ldvalue.String("MyDatabase")})
90 assert.Equal(t, ldvalue.String("MyDatabase"), f2.DescribeConfiguration(basicClientContext()))
91 })
92 }
93
94 type mockPersistentDataStoreFactory struct {
95 store subsystems.PersistentDataStore
96 fakeError error
97 receivedContext subsystems.ClientContext
98 }
99
100 func (m *mockPersistentDataStoreFactory) Build(
101 context subsystems.ClientContext,
102 ) (subsystems.PersistentDataStore, error) {
103 m.receivedContext = context
104 return m.store, m.fakeError
105 }
106
107 type mockPersistentDataStoreFactoryWithDescription struct {
108 description ldvalue.Value
109 }
110
111 func (m *mockPersistentDataStoreFactoryWithDescription) Build(
112 context subsystems.ClientContext,
113 ) (subsystems.PersistentDataStore, error) {
114 return nil, nil
115 }
116
117 func (m *mockPersistentDataStoreFactoryWithDescription) DescribeConfiguration(context subsystems.ClientContext) ldvalue.Value {
118 return m.description
119 }
120
View as plain text