4
4
"bytes"
5
5
"encoding/json"
6
6
"errors"
7
+ "github.com/cenkalti/backoff/v4"
7
8
"github.com/harness/ff-golang-server-sdk/dto"
8
9
"github.com/harness/ff-golang-server-sdk/evaluation"
9
10
"github.com/harness/ff-golang-server-sdk/log"
@@ -16,6 +17,7 @@ import (
16
17
"net/http"
17
18
"os"
18
19
"testing"
20
+ "time"
19
21
)
20
22
21
23
const (
@@ -180,7 +182,7 @@ func TestCfClient_NewClient(t *testing.T) {
180
182
{
181
183
name : "Synchronous client: Authentication failed with 500 and succeeds after one retry" ,
182
184
newClientFunc : func () (* CfClient , error ) {
183
- return newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithSleeper (test_helpers.MockSleeper {}))
185
+ return newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithAuthRetryStrategy ( getInstantRetryStrategy ()), WithSleeper (test_helpers.MockSleeper {}))
184
186
},
185
187
mockResponder : func () {
186
188
bodyString := `{
@@ -198,7 +200,7 @@ func TestCfClient_NewClient(t *testing.T) {
198
200
{
199
201
name : "Synchronous client: Authentication failed and succeeds just before exceeding max retries" ,
200
202
newClientFunc : func () (* CfClient , error ) {
201
- newClient , err := newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithMaxAuthRetries (10 ), WithSleeper (test_helpers.MockSleeper {}))
203
+ newClient , err := newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithMaxAuthRetries (10 ), WithAuthRetryStrategy ( getInstantRetryStrategy ()), WithSleeper (test_helpers.MockSleeper {}))
202
204
return newClient , err
203
205
},
204
206
mockResponder : func () {
@@ -208,7 +210,7 @@ func TestCfClient_NewClient(t *testing.T) {
208
210
}`
209
211
var responses []httpmock.Responder
210
212
// Add a bunch of error responses
211
- for i := 0 ; i < 10 ; i ++ {
213
+ for i := 0 ; i < 9 ; i ++ {
212
214
responses = append (responses , AuthResponseDetailed (500 , "internal server error" , bodyString ))
213
215
}
214
216
@@ -224,7 +226,7 @@ func TestCfClient_NewClient(t *testing.T) {
224
226
{
225
227
name : "Synchronous client: Authentication failed and exceeds max retries" ,
226
228
newClientFunc : func () (* CfClient , error ) {
227
- newClient , err := newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithMaxAuthRetries (10 ), WithSleeper (test_helpers.MockSleeper {}))
229
+ newClient , err := newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithMaxAuthRetries (10 ), WithAuthRetryStrategy ( getInstantRetryStrategy ()), WithSleeper (test_helpers.MockSleeper {}))
228
230
return newClient , err
229
231
},
230
232
mockResponder : func () {
@@ -234,7 +236,7 @@ func TestCfClient_NewClient(t *testing.T) {
234
236
}`
235
237
var responses []httpmock.Responder
236
238
// Add a bunch of error responses
237
- for i := 0 ; i < 11 ; i ++ {
239
+ for i := 0 ; i < 10 ; i ++ {
238
240
responses = append (responses , AuthResponseDetailed (500 , "internal server error" , bodyString ))
239
241
}
240
242
@@ -503,7 +505,7 @@ func TestCfClient_DefaultVariationReturned(t *testing.T) {
503
505
{
504
506
name : "Evaluations with Synchronous client with a server error" ,
505
507
clientFunc : func () (* CfClient , error ) {
506
- return newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithMaxAuthRetries (2 ), WithSleeper (test_helpers.MockSleeper {}))
508
+ return newClient (http .DefaultClient , ValidSDKKey , WithWaitForInitialized (true ), WithMaxAuthRetries (2 ), WithAuthRetryStrategy ( getInstantRetryStrategy ()), WithSleeper (test_helpers.MockSleeper {}))
507
509
},
508
510
mockResponder : func () {
509
511
bodyString := `{
@@ -558,7 +560,7 @@ func TestCfClient_DefaultVariationReturned(t *testing.T) {
558
560
{
559
561
name : "Evaluations with Async client with a server error" ,
560
562
clientFunc : func () (* CfClient , error ) {
561
- return newClient (http .DefaultClient , ValidSDKKey , WithMaxAuthRetries (2 ), WithSleeper (test_helpers.MockSleeper {}))
563
+ return newClient (http .DefaultClient , ValidSDKKey , WithMaxAuthRetries (2 ), WithAuthRetryStrategy ( getInstantRetryStrategy ()), WithSleeper (test_helpers.MockSleeper {}))
562
564
},
563
565
mockResponder : func () {
564
566
bodyString := `{
@@ -759,3 +761,12 @@ func TestCfClient_Close(t *testing.T) {
759
761
t .Log ("When I close the client for the second time I should an error" )
760
762
assert .NotNil (t , client .Close ())
761
763
}
764
+
765
+ // getInstantRetryStrategy returns a strategy that retries every millisecond for testing purposes
766
+ func getInstantRetryStrategy () * backoff.ExponentialBackOff {
767
+ exponentialBackOff := backoff .NewExponentialBackOff ()
768
+ exponentialBackOff .InitialInterval = 1 * time .Millisecond
769
+ exponentialBackOff .MaxInterval = 1 * time .Millisecond
770
+ exponentialBackOff .Multiplier = 0
771
+ return exponentialBackOff
772
+ }
0 commit comments