-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync_test.go
103 lines (83 loc) · 2.94 KB
/
sync_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
package featureprobe
import (
"encoding/json"
"io/ioutil"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestSyncInitSuccess(t *testing.T) {
repo, jsonStr := setup(t)
var repo2 Repository
synchronizer := NewSynchronizer("https://featureprobe.com/api/toggles", 1000 * time.Millisecond, "sdk_key", &repo2)
httpmock.ActivateNonDefault(&synchronizer.httpClient)
httpmock.RegisterResponder("GET", "https://featureprobe.com/api/toggles",
httpmock.NewStringResponder(200, jsonStr))
synchronizer.Start(make(chan<- struct{}))
defer synchronizer.Stop()
time.Sleep(4 * time.Second)
count := httpmock.GetTotalCallCount()
assert.True(t, count > 2)
assert.True(t, synchronizer.Initialized())
synchronizer.mu.Lock() // for go test -race
assert.Equal(t, repo, repo2)
httpmock.DeactivateAndReset()
synchronizer.mu.Unlock()
}
func TestSyncInitFailed(t *testing.T) {
var repo2 Repository
synchronizer := NewSynchronizer("https://featureprobe.com/api/toggles", 500 * time.Millisecond, "sdk_key", &repo2)
httpmock.ActivateNonDefault(&synchronizer.httpClient)
httpmock.RegisterResponder("GET", "https://featureprobe.com/api/toggles",
httpmock.NewStringResponder(500, ``))
synchronizer.Start(make(chan<- struct{}))
defer synchronizer.Stop()
time.Sleep(1 * time.Second)
assert.False(t, synchronizer.Initialized())
synchronizer.mu.Lock() // for go test -race
httpmock.DeactivateAndReset()
synchronizer.mu.Unlock()
}
func TestSyncInvalidJson(t *testing.T) {
var repo2 Repository
synchronizer := NewSynchronizer("https://featureprobe.com/api/toggles", 500 * time.Millisecond, "sdk_key", &repo2)
invalidJson := `{ `
httpmock.RegisterResponder("GET", "https://featureprobe.com/api/toggles",
httpmock.NewStringResponder(200, invalidJson))
httpmock.ActivateNonDefault(&synchronizer.httpClient)
synchronizer.Start(make(chan<- struct{}))
defer synchronizer.Stop()
time.Sleep(1 * time.Second)
count := httpmock.GetTotalCallCount()
assert.True(t, count >= 1)
synchronizer.mu.Lock()
httpmock.DeactivateAndReset()
synchronizer.mu.Unlock()
}
func TestSyncInvalidUrl(t *testing.T) {
var repo2 Repository
invalidUrl := string([]byte{1, 2, 3})
synchronizer := NewSynchronizer(invalidUrl, 100 * time.Millisecond, "sdk_key", &repo2)
_, jsonStr := setup(t)
httpmock.ActivateNonDefault(&synchronizer.httpClient)
synchronizer.Start(make(chan<- struct{}))
defer synchronizer.Stop()
httpmock.RegisterResponder("GET", "https://featureprobe.com/api/toggles",
httpmock.NewStringResponder(200, jsonStr))
time.Sleep(1 * time.Second)
synchronizer.mu.Lock()
httpmock.DeactivateAndReset()
synchronizer.mu.Unlock()
//TODO: check error
}
func setup(t *testing.T) (Repository, string) {
var repo Repository
bytes, _ := ioutil.ReadFile("./resources/fixtures/repo.json")
jsonStr := string(bytes)
repoData := RepositoryData{}
err := json.Unmarshal(bytes, &repoData)
repo.flush(repoData)
assert.Equal(t, nil, err)
return repo, jsonStr
}