-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlazy_loading_policy_test.go
113 lines (91 loc) · 2.99 KB
/
lazy_loading_policy_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
package configcat
import (
"net/http"
"testing"
"time"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
)
func TestLazyLoadingPolicy_NoAsync(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponseJSON(rootNodeWithKeyValue("key", "value1"))
cfg := srv.config()
cfg.PollingMode = Lazy
cfg.PollInterval = 50 * time.Millisecond
client := NewCustomClient(cfg)
defer client.Close()
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value1")
srv.setResponse(configResponse{
body: marshalJSON(rootNodeWithKeyValue("key", "value2")),
sleep: 40 * time.Millisecond,
})
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value1")
time.Sleep(100 * time.Millisecond)
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value2")
}
func TestLazyLoadingPolicy_FetchFail(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponse(configResponse{
status: http.StatusInternalServerError,
body: `something failed`,
})
cfg := srv.config()
cfg.PollingMode = Lazy
cfg.PollInterval = 50 * time.Millisecond
client := NewCustomClient(cfg)
defer client.Close()
c.Assert(client.fetcher.current(), qt.IsNil)
}
func TestLazyLoadingPolicy_Async(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponseJSON(rootNodeWithKeyValue("key", "value1"))
cfg := srv.config()
cfg.PollingMode = Lazy
cfg.PollInterval = 50 * time.Millisecond
cfg.NoWaitForRefresh = true
client := NewCustomClient(cfg)
defer client.Close()
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "")
// Wait for the response to arrive.
time.Sleep(10 * time.Millisecond)
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value1")
srv.setResponse(configResponse{
body: marshalJSON(rootNodeWithKeyValue("key", "value2")),
sleep: 40 * time.Millisecond,
})
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value1")
time.Sleep(100 * time.Millisecond)
// The config is fetched lazily and takes at least 40ms, so
// we'll still see the old value.
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value1")
time.Sleep(50 * time.Millisecond)
c.Assert(client.GetStringValue("key", "", nil), qt.Equals, "value2")
}
func TestLazyLoadingPolicy_NotModified(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponse(configResponse{
body: `{"test":1}`,
sleep: time.Millisecond,
})
cfg := srv.config()
cfg.PollingMode = Lazy
cfg.PollInterval = 10 * time.Millisecond
client := NewCustomClient(cfg)
defer client.Close()
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":1}`)
time.Sleep(20 * time.Millisecond)
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":1}`)
c.Assert(srv.allResponses(), deepEquals, []configResponse{{
status: http.StatusOK,
body: `{"test":1}`,
sleep: time.Millisecond,
}, {
status: http.StatusNotModified,
sleep: time.Millisecond,
}})
}
var deepEquals = qt.CmpEquals(cmp.AllowUnexported(configResponse{}))