...
1 package ldcomponents
2
3 import (
4 "errors"
5 "testing"
6 "time"
7
8 "github.com/launchdarkly/go-server-sdk/v6/subsystems"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 type mockBigSegmentStoreFactory struct {
15 fakeError error
16 }
17
18 func (m mockBigSegmentStoreFactory) Build(subsystems.ClientContext) (subsystems.BigSegmentStore, error) {
19 return mockBigSegmentStore{}, m.fakeError
20 }
21
22 type mockBigSegmentStore struct{}
23
24 func (m mockBigSegmentStore) Close() error { return nil }
25
26 func (m mockBigSegmentStore) GetMetadata() (subsystems.BigSegmentStoreMetadata, error) {
27 return subsystems.BigSegmentStoreMetadata{}, nil
28 }
29
30 func (m mockBigSegmentStore) GetMembership(string) (subsystems.BigSegmentMembership, error) {
31 return nil, nil
32 }
33
34 func TestBigSegmentsConfigurationBuilder(t *testing.T) {
35 context := basicClientContext()
36
37 t.Run("defaults", func(t *testing.T) {
38 c, err := BigSegments(mockBigSegmentStoreFactory{}).Build(context)
39 require.NoError(t, err)
40
41 assert.Equal(t, mockBigSegmentStore{}, c.GetStore())
42 assert.Equal(t, DefaultBigSegmentsContextCacheSize, c.GetContextCacheSize())
43 assert.Equal(t, DefaultBigSegmentsContextCacheTime, c.GetContextCacheTime())
44 assert.Equal(t, DefaultBigSegmentsStatusPollInterval, c.GetStatusPollInterval())
45 assert.Equal(t, DefaultBigSegmentsStaleAfter, c.GetStaleAfter())
46 })
47
48 t.Run("store creation fails", func(t *testing.T) {
49 fakeError := errors.New("sorry")
50 storeFactory := mockBigSegmentStoreFactory{fakeError: fakeError}
51 _, err := BigSegments(storeFactory).Build(context)
52 require.Equal(t, fakeError, err)
53 })
54
55 t.Run("ContextCacheSize", func(t *testing.T) {
56 c, err := BigSegments(mockBigSegmentStoreFactory{}).
57 ContextCacheSize(999).
58 Build(context)
59 require.NoError(t, err)
60 assert.Equal(t, 999, c.GetContextCacheSize())
61 })
62
63 t.Run("ContextCacheTime", func(t *testing.T) {
64 c, err := BigSegments(mockBigSegmentStoreFactory{}).
65 ContextCacheTime(time.Second * 999).
66 Build(context)
67 require.NoError(t, err)
68 assert.Equal(t, time.Second*999, c.GetContextCacheTime())
69 })
70
71 t.Run("StatusPollInterval", func(t *testing.T) {
72 c, err := BigSegments(mockBigSegmentStoreFactory{}).
73 StatusPollInterval(time.Second * 999).
74 Build(context)
75 require.NoError(t, err)
76 assert.Equal(t, time.Second*999, c.GetStatusPollInterval())
77 })
78
79 t.Run("StaleAfter", func(t *testing.T) {
80 c, err := BigSegments(mockBigSegmentStoreFactory{}).
81 StaleAfter(time.Second * 999).
82 Build(context)
83 require.NoError(t, err)
84 assert.Equal(t, time.Second*999, c.GetStaleAfter())
85 })
86 }
87
View as plain text