-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathagollo_test.go
163 lines (133 loc) · 3.18 KB
/
agollo_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package agollo
import (
"os"
"path"
"testing"
"time"
"github.com/philchia/agollo/v4/internal/mockserver"
)
func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}
func setup() {
go mockserver.Run()
// wait for mock server to run
time.Sleep(time.Millisecond * 5)
}
func teardown() {
mockserver.Close()
}
func TestAgolloStart(t *testing.T) {
conf, err := NewConf("./testdata/app.properties")
if err != nil {
t.Error(err)
return
}
if err := Start(conf); err != nil {
t.Errorf("Start with app.properties should return nil, got :%v", err)
return
}
client := defaultClient.(*client)
f, err := os.Stat(path.Dir(client.getDumpFileName()))
if err != nil {
t.Errorf("dump file dir should exists, got err:%v", err)
return
}
if !f.IsDir() {
t.Errorf("dump file dir should be a dir, got file")
return
}
defer Stop()
defer os.Remove(client.getDumpFileName())
if err := client.loadLocal(client.getDumpFileName()); err != nil {
t.Errorf("loadLocal should return nil, got: %v", err)
return
}
mockserver.Set("application", "key", "value")
updates := make(chan struct{}, 1)
defer close(updates)
OnUpdate(func(event *ChangeEvent) {
updates <- struct{}{}
})
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val := GetString("key")
if val != "value" {
t.Errorf("GetStringValue of key should = value, got %v", val)
return
}
keys := GetAllKeys()
if len(keys) != 1 {
t.Errorf("GetAllKeys should return 1 key")
return
}
releasekey := GetReleaseKey()
if releasekey != "" {
t.Errorf("GetReleaseKey return empty release key")
return
}
mockserver.Set("application", "key", "newvalue")
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetString("key")
if val != "newvalue" {
t.Errorf("GetStringValue of key should = newvalue, got %v", val)
return
}
content := GetPropertiesContent()
if content != "key=newvalue\n" {
t.Errorf("GetPropertiesContent of application = %s, want %v", content, "key=newvalue\n")
return
}
keys = GetAllKeys()
if len(keys) != 1 {
t.Errorf("GetAllKeys should return 1 key")
return
}
mockserver.Delete("application", "key")
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetString("key")
if val != "" {
t.Errorf("GetStringValue of key should = defaultValue, got %v", val)
return
}
keys = GetAllKeys()
if len(keys) != 0 {
t.Errorf("GetAllKeys should return 0 key")
return
}
mockserver.Set("client.json", "content", `{"name":"agollo"}`)
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetContent(WithNamespace("client.json"))
if val != `{"name":"agollo"}` {
t.Errorf(`GetStringValue of client.json content should = {"name":"agollo"}, got %v`, val)
return
}
if err := SubscribeToNamespaces("new_namespace.json"); err != nil {
t.Error(err)
return
}
mockserver.Set("new_namespace.json", "content", "1")
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetContent(WithNamespace("new_namespace.json"))
if val != `1` {
t.Errorf(`GetStringValueWithNameSpace of new_namespace.json content should = 1, got %v`, val)
return
}
}