forked from Unleash/unleash-client-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository_test.go
129 lines (116 loc) · 3.03 KB
/
repository_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
package unleash
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/Unleash/unleash-client-go/v3/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// TestRepository_GetFeaturesFail tests that OnReady isn't fired unless
// /client/features has returned successfully.
func TestRepository_GetFeaturesFail(t *testing.T) {
assert := assert.New(t)
featuresCalls := make(chan int, 10)
var sendStatus200 int32
prevStatus := 0
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
switch req.Method + " " + req.URL.Path {
case "POST /client/register":
case "GET /client/features":
status200 := atomic.LoadInt32(&sendStatus200) == 1
status := 0
if status200 {
status = 200
rw.WriteHeader(200)
writeJSON(rw, api.FeatureResponse{})
} else {
status = 400
rw.WriteHeader(400)
}
if status != prevStatus {
featuresCalls <- status
prevStatus = status
}
case "POST /client/metrics":
default:
t.Fatalf("Unexpected request: %+v", req)
}
}))
defer srv.Close()
ready := make(chan struct{})
mockListener := &MockedListener{}
mockListener.On("OnReady").Run(func(args mock.Arguments) { close(ready) }).Return()
mockListener.On("OnRegistered", mock.AnythingOfType("ClientData"))
mockListener.On("OnError", mock.MatchedBy(func(e error) bool {
return strings.HasSuffix(e.Error(), "/client/features returned status code 400")
})).Return()
mockListener.On("OnSent", mock.AnythingOfType("MetricsData")).Return()
client, err := NewClient(
WithUrl(srv.URL),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
WithRefreshInterval(time.Millisecond),
)
assert.Nil(err, "client should not return an error")
assert.Equal(400, <-featuresCalls)
select {
case <-ready:
t.Fatal("client is ready but it shouldn't be")
case <-time.NewTimer(time.Second).C:
}
atomic.StoreInt32(&sendStatus200, 1)
assert.Equal(200, <-featuresCalls)
select {
case <-ready:
case <-time.NewTimer(time.Second).C:
t.Fatal("client isn't ready but should be")
}
client.Close()
}
func TestRepository_ParseAPIResponse(t *testing.T) {
assert := assert.New(t)
data := []byte(`{
"version": 2,
"features": [
{
"strategies": [],
"impressionData": false,
"enabled": false,
"name": "my-feature",
"description": "",
"project": "default",
"stale": false,
"type": "release",
"variants": []
},
{
"strategies": [],
"impressionData": false,
"enabled": false,
"name": "my-new-feature",
"description": "",
"project": "default",
"stale": false,
"type": "release",
"variants": []
}
],
"query": {
"inlineSegmentConstraints": true
}
}`)
reader := bytes.NewReader(data);
dec := json.NewDecoder(reader)
var response api.FeatureResponse
err := dec.Decode(&response)
assert.Nil(err)
assert.Equal(2, len(response.Features))
assert.Equal(0, len(response.Segments))
}