-
Notifications
You must be signed in to change notification settings - Fork 80
/
fake_client.go
132 lines (114 loc) · 4.06 KB
/
fake_client.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Package testclient contains test helpers that reference the SDK-related packages. These are in
// sharedtest/testclient rather than just sharedtest so that sharedtest can be used by those packages
// without a circular reference.
package testclient
import (
"sync"
"testing"
"time"
helpers "github.com/launchdarkly/go-test-helpers/v3"
"github.com/launchdarkly/ld-relay/v8/config"
"github.com/launchdarkly/ld-relay/v8/internal/sdks"
"github.com/launchdarkly/ld-relay/v8/internal/sharedtest"
"github.com/launchdarkly/ld-relay/v8/internal/store"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
ld "github.com/launchdarkly/go-server-sdk/v7"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
)
func CreateDummyClient(sdkKey config.SDKKey, sdkConfig ld.Config, timeout time.Duration) (sdks.LDClientContext, error) {
store, _ := sdkConfig.DataStore.(*store.SSERelayDataStoreAdapter).Build(
subsystems.BasicClientContext{SDKKey: string(sdkKey)})
err := store.Init(sharedtest.AllData)
if err != nil {
panic(err)
}
return &FakeLDClient{Key: sdkKey, CloseCh: make(chan struct{}), initialized: true}, nil
}
type FakeLDClient struct {
Key config.SDKKey
CloseCh chan struct{}
dataSourceStatus *interfaces.DataSourceStatus
initialized bool
lock sync.Mutex
}
type CapturedLDClient struct {
Key config.SDKKey
Client sdks.LDClientContext
}
func (c *FakeLDClient) Initialized() bool {
return c.initialized
}
func (c *FakeLDClient) SecureModeHash(context ldcontext.Context) string {
return FakeHashForContext(context)
}
func (c *FakeLDClient) GetDataSourceStatus() interfaces.DataSourceStatus {
c.lock.Lock()
defer c.lock.Unlock()
if c.dataSourceStatus != nil {
return *c.dataSourceStatus
}
state := interfaces.DataSourceStateValid
if !c.initialized {
state = interfaces.DataSourceStateInitializing
}
return interfaces.DataSourceStatus{State: state}
}
func (c *FakeLDClient) GetDataStoreStatus() sdks.DataStoreStatusInfo {
return sdks.DataStoreStatusInfo{Available: true}
}
func (c *FakeLDClient) Close() error {
if c.CloseCh != nil {
close(c.CloseCh)
}
return nil
}
func (c *FakeLDClient) SetDataSourceStatus(newStatus interfaces.DataSourceStatus) {
c.lock.Lock()
defer c.lock.Unlock()
c.dataSourceStatus = &newStatus
}
func (c *FakeLDClient) AwaitClose(t *testing.T, timeout time.Duration) {
if !helpers.AssertChannelClosed(t, c.CloseCh, timeout, "timed out waiting for SDK client to be closed") {
t.FailNow()
}
}
func FakeHashForContext(context ldcontext.Context) string {
return "fake-hash-" + context.Key()
}
func FakeLDClientFactory(shouldBeInitialized bool) sdks.ClientFactoryFunc {
return FakeLDClientFactoryWithChannel(shouldBeInitialized, nil)
}
func FakeLDClientFactoryWithChannel(shouldBeInitialized bool, createdCh chan<- *FakeLDClient) sdks.ClientFactoryFunc {
return func(sdkKey config.SDKKey, config ld.Config, timeout time.Duration) (sdks.LDClientContext, error) {
// We're not creating a real client, but we still need to invoke the DataStoreFactory as the
// SDK would do, since that's how Relay obtains its shared reference to the data store.
if config.DataStore != nil {
_, err := config.DataStore.Build(
subsystems.BasicClientContext{},
)
if err != nil {
return nil, err
}
}
c := &FakeLDClient{Key: sdkKey, CloseCh: make(chan struct{}), initialized: shouldBeInitialized}
if createdCh != nil {
createdCh <- c
}
return c, nil
}
}
func RealLDClientFactoryWithChannel(shouldBeInitialized bool, createdCh chan<- CapturedLDClient) sdks.ClientFactoryFunc {
return func(sdkKey config.SDKKey, config ld.Config, timeout time.Duration) (sdks.LDClientContext, error) {
c, err := sdks.DefaultClientFactory()(sdkKey, config, timeout)
if c != nil && createdCh != nil {
createdCh <- CapturedLDClient{Key: sdkKey, Client: c}
}
return c, err
}
}
func ClientFactoryThatFails(err error) sdks.ClientFactoryFunc {
return func(sdkKey config.SDKKey, config ld.Config, timeout time.Duration) (sdks.LDClientContext, error) {
return nil, err
}
}