-
Notifications
You must be signed in to change notification settings - Fork 6
/
auto_polling_policy_test.go
93 lines (80 loc) · 2.61 KB
/
auto_polling_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
package configcat
import (
"context"
"net/http"
"testing"
"time"
qt "github.com/frankban/quicktest"
)
func TestAutoPollingPolicy_PollChange(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponse(configResponse{body: `{"test":1}`})
cfg := srv.config()
cfg.PollInterval = 10 * time.Millisecond
client := NewCustomClient(cfg)
defer client.Close()
err := client.Refresh(context.Background())
c.Assert(err, qt.Equals, nil)
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":1}`)
srv.setResponse(configResponse{body: `{"test":2}`})
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":1}`)
time.Sleep(40 * time.Millisecond)
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":2}`)
}
func TestAutoPollingPolicy_FetchFail(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponse(configResponse{
status: http.StatusInternalServerError,
body: `something wrong`,
})
cfg := srv.config()
cfg.PollInterval = 2 * time.Second
client := NewCustomClient(cfg)
defer client.Close()
err := client.Refresh(context.Background())
c.Assert(err, qt.ErrorMatches, `config fetch failed: unexpected HTTP response was received while trying to fetch config JSON: 500 Internal Server Error`)
conf := client.Snapshot(nil).config
c.Assert(conf, qt.IsNil)
}
func TestAutoPollingPolicy_DoubleClose(t *testing.T) {
srv := newConfigServer(t)
srv.setResponse(configResponse{body: `{"test":1}`})
cfg := srv.config()
cfg.PollInterval = time.Millisecond
client := NewCustomClient(cfg)
client.Close()
client.Close()
}
func TestAutoPollingPolicy_WithNotify(t *testing.T) {
c := qt.New(t)
srv := newConfigServer(t)
srv.setResponse(configResponse{body: `{"test":1}`})
cfg := srv.config()
notifyc := make(chan struct{})
cfg.PollInterval = time.Millisecond
cfg.Hooks = &Hooks{OnConfigChanged: func() { notifyc <- struct{}{} }}
client := NewCustomClient(cfg)
defer client.Close()
select {
case <-notifyc:
case <-time.After(time.Second):
t.Fatalf("timed out waiting for notification")
}
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":1}`)
// Change the content and we should see another notification.
srv.setResponse(configResponse{body: `{"test":2}`})
select {
case <-notifyc:
case <-time.After(time.Second):
t.Fatalf("timed out waiting for notification")
}
c.Assert(string(client.Snapshot(nil).config.jsonBody), qt.Equals, `{"test":2}`)
// Check that we don't see any more notifications.
select {
case <-notifyc:
t.Fatalf("unexpected notification received")
case <-time.After(20 * time.Millisecond):
}
}