This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathldclient_test.go
197 lines (156 loc) · 4.83 KB
/
ldclient_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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package ldclient
import (
"io/ioutil"
"log"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type mockUpdateProcessor struct {
IsInitialized bool
CloseFn func() error
StartFn func(chan<- struct{})
}
func (u mockUpdateProcessor) Initialized() bool {
return u.IsInitialized
}
func (u mockUpdateProcessor) Close() error {
if u.CloseFn == nil {
return nil
}
return u.CloseFn()
}
func (u mockUpdateProcessor) Start(closeWhenReady chan<- struct{}) {
if u.StartFn == nil {
return
}
u.StartFn(closeWhenReady)
}
func updateProcessorFactory(u UpdateProcessor) func(string, Config) (UpdateProcessor, error) {
return func(key string, c Config) (UpdateProcessor, error) {
return u, nil
}
}
type testEventProcessor struct {
events []Event
}
func (t *testEventProcessor) SendEvent(e Event) {
t.events = append(t.events, e)
}
func (t *testEventProcessor) Flush() {}
func (t *testEventProcessor) Close() error {
return nil
}
func TestSecureModeHash(t *testing.T) {
expected := "aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597"
key := "Message"
config := DefaultConfig
config.Offline = true
client, _ := MakeCustomClient("secret", config, 0*time.Second)
hash := client.SecureModeHash(User{Key: &key})
assert.Equal(t, expected, hash)
}
func TestIdentifySendsIdentifyEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := NewUser("userKey")
err := client.Identify(user)
assert.NoError(t, err)
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 1, len(events))
e := events[0].(IdentifyEvent)
assert.Equal(t, user, e.User)
}
func TestIdentifyWithNilUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.Identify(User{})
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 0, len(events))
}
func TestIdentifyWithEmptyUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.Identify(NewUser(""))
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 0, len(events))
}
func TestTrackSendsCustomEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := NewUser("userKey")
key := "eventKey"
err := client.Track(key, user, nil)
assert.NoError(t, err)
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 1, len(events))
e := events[0].(CustomEvent)
assert.Equal(t, user, e.User)
assert.Equal(t, key, e.Key)
assert.Nil(t, e.Data)
}
func TestTrackSendsCustomEventWithData(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := NewUser("userKey")
key := "eventKey"
data := map[string]interface{}{"thing": "stuff"}
err := client.Track(key, user, data)
assert.NoError(t, err)
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 1, len(events))
e := events[0].(CustomEvent)
assert.Equal(t, user, e.User)
assert.Equal(t, key, e.Key)
assert.Equal(t, data, e.Data)
}
func TestTrackWithNilUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.Track("eventkey", User{}, nil)
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 0, len(events))
}
func TestTrackWithEmptyUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.Track("eventkey", NewUser(""), nil)
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*testEventProcessor).events
assert.Equal(t, 0, len(events))
}
func TestMakeCustomClient_WithFailedInitialization(t *testing.T) {
updateProcessor := mockUpdateProcessor{
IsInitialized: false,
StartFn: func(closeWhenReady chan<- struct{}) {
close(closeWhenReady)
},
}
client, err := MakeCustomClient("sdkKey", Config{
Logger: log.New(ioutil.Discard, "", 0),
UpdateProcessorFactory: updateProcessorFactory(updateProcessor),
EventProcessor: &testEventProcessor{},
UserKeysFlushInterval: 30 * time.Second,
}, time.Second)
assert.NotNil(t, client)
assert.Equal(t, err, ErrInitializationFailed)
}
func makeTestClient() *LDClient {
config := Config{
Logger: log.New(os.Stderr, "[LaunchDarkly]", log.LstdFlags),
Offline: false,
SendEvents: true,
FeatureStore: NewInMemoryFeatureStore(nil),
UpdateProcessorFactory: updateProcessorFactory(mockUpdateProcessor{
IsInitialized: true,
}),
EventProcessor: &testEventProcessor{},
UserKeysFlushInterval: 30 * time.Second,
}
client, _ := MakeCustomClient("sdkKey", config, time.Duration(0))
return client
}