-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathldclient_end_to_end_fdv2_test.go
275 lines (212 loc) · 10.4 KB
/
ldclient_end_to_end_fdv2_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package ldclient
import (
"crypto/x509"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-server-sdk/v7/internal/fdv2proto"
"github.com/launchdarkly/go-server-sdk/v7/internal/sharedtest"
"github.com/launchdarkly/go-server-sdk/v7/testhelpers/ldservicesv2"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
"github.com/launchdarkly/go-server-sdk/v7/testhelpers/ldservices"
"github.com/launchdarkly/go-test-helpers/v3/httphelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// This tests the sequential two-phase nature of the Flag Delivery V2 protocol. First, a polling initializer
// attempts to grab a payload, but the mock handler will return a 500. The initializer will fail and
// the primary synchronizer (streaming mode) will then make a streaming request. This succeeds, returning a payload
// containing a true flag.
func TestFDV2DefaultIsTwoPhaseInit(t *testing.T) {
data := ldservicesv2.NewServerSDKData().Flags(alwaysTrueFlag)
// The polling initializer will fail since we return a 500.
pollRecordingHandler, _ := httphelpers.RecordingHandler(httphelpers.HandlerWithStatus(500))
protocol := ldservicesv2.NewStreamingProtocol().
WithIntent(fdv2proto.ServerIntent{Payload: fdv2proto.Payload{
ID: "fake-id", Target: 0, Code: "xfer-full", Reason: "payload-missing",
}}).
WithPutObjects(data.ToPutObjects()).
WithTransferred(1)
// The streaming synchronizer will receive the FDv2 protocol messages, including the true flag.
streamHandler, streamSender := ldservices.ServerSideStreamingServiceHandler(protocol.Next())
protocol.Enqueue(streamSender)
streamRecordingHandler, _ := httphelpers.RecordingHandler(streamHandler)
// Use a sequential handler so that the first request is serviced by the polling handler, and the second
// by the streaming.
handler := httphelpers.SequentialHandler(pollRecordingHandler, streamRecordingHandler)
httphelpers.WithServer(handler, func(server *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
DataSystem: ldcomponents.DataSystem().WithRelayProxyEndpoints(server.URL).Default(),
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
})
}
func TestFDV2StreamingSynchronizer(t *testing.T) {
data := ldservicesv2.NewServerSDKData().Flags(alwaysTrueFlag)
protocol := ldservicesv2.NewStreamingProtocol().
WithIntent(fdv2proto.ServerIntent{Payload: fdv2proto.Payload{
ID: "fake-id", Target: 0, Code: "xfer-full", Reason: "payload-missing",
}}).
WithPutObjects(data.ToPutObjects()).
WithTransferred(1)
streamHandler, streamSender := ldservices.ServerSideStreamingServiceHandler(protocol.Next())
protocol.Enqueue(streamSender)
handler, requestsCh := httphelpers.RecordingHandler(streamHandler)
httphelpers.WithServer(handler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
defer logCapture.DumpIfTestFailed(t)
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
DataSystem: ldcomponents.DataSystem().WithEndpoints(
ldcomponents.Endpoints{Streaming: streamServer.URL},
).Streaming(),
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
r := <-requestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
assert.Len(t, logCapture.GetOutput(ldlog.Warn), 0)
})
}
func TestFDV2StreamingSynchronizeReconnectsWithNonFatalError(t *testing.T) {
data := ldservicesv2.NewServerSDKData().Flags(alwaysTrueFlag)
protocol := ldservicesv2.NewStreamingProtocol().
WithIntent(fdv2proto.ServerIntent{Payload: fdv2proto.Payload{
ID: "fake-id", Target: 0, Code: "xfer-full", Reason: "payload-missing",
}}).
WithPutObjects(data.ToPutObjects()).
WithTransferred(1)
streamHandler, streamSender := ldservices.ServerSideStreamingServiceHandler(protocol.Next())
protocol.Enqueue(streamSender)
failThenSucceedHandler := httphelpers.SequentialHandler(httphelpers.HandlerWithStatus(503), streamHandler)
handler, requestsCh := httphelpers.RecordingHandler(failThenSucceedHandler)
httphelpers.WithServer(handler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
DataSystem: ldcomponents.DataSystem().WithEndpoints(
ldcomponents.Endpoints{Streaming: streamServer.URL}).Streaming(),
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NoError(t, err)
defer client.Close()
assert.Equal(t, string(interfaces.DataSourceStateValid), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
r0 := <-requestsCh
assert.Equal(t, testSdkKey, r0.Request.Header.Get("Authorization"))
r1 := <-requestsCh
assert.Equal(t, testSdkKey, r1.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
expectedWarning := "Error in stream connection (will retry): HTTP error 503"
assert.Equal(t, []string{expectedWarning}, logCapture.GetOutput(ldlog.Warn))
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
})
}
func TestFDV2PollingSynchronizerFailsToStartWith401Error(t *testing.T) {
handler, requestsCh := httphelpers.RecordingHandler(httphelpers.HandlerWithStatus(401))
httphelpers.WithServer(handler, func(pollServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
DataSystem: ldcomponents.DataSystem().
WithEndpoints(ldcomponents.Endpoints{Polling: pollServer.URL}).Polling(),
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*5)
require.Error(t, err)
require.NotNil(t, client)
defer client.Close()
assert.Equal(t, initializationFailedErrorMessage, err.Error())
assert.Equal(t, string(interfaces.DataSourceStateOff), string(client.GetDataSourceStatusProvider().GetStatus().State))
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.False(t, value)
r := <-requestsCh
assert.Equal(t, testSdkKey, r.Request.Header.Get("Authorization"))
assertNoMoreRequests(t, requestsCh)
expectedError := "Error on polling request (giving up permanently): HTTP error 401 (invalid SDK key)"
assert.Equal(t, []string{expectedError}, logCapture.GetOutput(ldlog.Error))
assert.Equal(t, []string{pollingModeWarningMessage, initializationFailedErrorMessage}, logCapture.GetOutput(ldlog.Warn))
})
}
func TestFDV2StreamingSynchronizerUsesCustomTLSConfiguration(t *testing.T) {
data := ldservicesv2.NewServerSDKData().Flags(alwaysTrueFlag)
protocol := ldservicesv2.NewStreamingProtocol().
WithIntent(fdv2proto.ServerIntent{Payload: fdv2proto.Payload{
ID: "fake-id", Target: 0, Code: "xfer-full", Reason: "payload-missing",
}}).
WithPutObjects(data.ToPutObjects()).
WithTransferred(1)
streamHandler, streamSender := ldservices.ServerSideStreamingServiceHandler(protocol.Next())
protocol.Enqueue(streamSender)
httphelpers.WithSelfSignedServer(streamHandler, func(server *httptest.Server, certData []byte, certs *x509.CertPool) {
config := Config{
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().CACert(certData),
Logging: ldcomponents.Logging().Loggers(sharedtest.NewTestLoggers()),
DataSystem: ldcomponents.DataSystem().WithEndpoints(
ldcomponents.Endpoints{Streaming: server.URL}).Streaming(),
}
client, err := MakeCustomClient(testSdkKey, config, time.Second*50000)
require.NoError(t, err)
defer client.Close()
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.True(t, value)
})
}
func TestFDV2StreamingSynchronizerTimesOut(t *testing.T) {
data := ldservicesv2.NewServerSDKData().Flags(alwaysTrueFlag)
protocol := ldservicesv2.NewStreamingProtocol().
WithIntent(fdv2proto.ServerIntent{Payload: fdv2proto.Payload{
ID: "fake-id", Target: 0, Code: "xfer-full", Reason: "payload-missing",
}}).
WithPutObjects(data.ToPutObjects()).
WithTransferred(1)
streamHandler, streamSender := ldservices.ServerSideStreamingServiceHandler(protocol.Next())
protocol.Enqueue(streamSender)
slowHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(300 * time.Millisecond)
streamHandler.ServeHTTP(w, r)
})
httphelpers.WithServer(slowHandler, func(streamServer *httptest.Server) {
logCapture := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
Logging: ldcomponents.Logging().Loggers(logCapture.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: streamServer.URL},
DataSystem: ldcomponents.DataSystem().WithEndpoints(
ldcomponents.Endpoints{Streaming: streamServer.URL}).Streaming(),
}
client, err := MakeCustomClient(testSdkKey, config, time.Millisecond*100)
require.Error(t, err)
require.NotNil(t, client)
defer client.Close()
assert.Equal(t, "timeout encountered waiting for LaunchDarkly client initialization", err.Error())
value, _ := client.BoolVariation(alwaysTrueFlag.Key, testUser, false)
assert.False(t, value)
assert.Equal(t, []string{"Timeout encountered waiting for LaunchDarkly client initialization"}, logCapture.GetOutput(ldlog.Warn))
assert.Len(t, logCapture.GetOutput(ldlog.Error), 0)
})
}