forked from philchia/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagollo_test.go
135 lines (110 loc) · 3.13 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
package agollo
import (
"log"
"os"
"testing"
"time"
"github.com/philchia/agollo/internal/mockserver"
)
func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}
func setup() {
go func() {
if err := mockserver.Run(); err != nil {
log.Fatal(err)
}
}()
// wait for mock server to run
time.Sleep(time.Millisecond * 10)
}
func teardown() {
mockserver.Close()
}
func TestAgolloStart(t *testing.T) {
if err := Start(); err == nil {
t.Errorf("Start with default app.properties should return err, got :%v", err)
}
if err := StartWithConfFile("fake.properties"); err == nil {
t.Errorf("Start with fake.properties should return err, got :%v", err)
}
if err := StartWithConfFile("./testdata/app.properties"); err != nil {
t.Errorf("Start with app.properties should return nil, got :%v", err)
}
if err := Stop(); err != nil {
t.Errorf("Stop should return nil, got :%v", err)
}
os.Remove(defaultClient.getDumpFileName())
if err := StartWithConfFile("./testdata/app.properties"); err != nil {
t.Errorf("Start with app.properties should return nil, got :%v", err)
}
defer Stop()
defer os.Remove(defaultClient.getDumpFileName())
if err := defaultClient.loadLocal(defaultClient.getDumpFileName()); err != nil {
t.Errorf("loadLocal should return nil, got: %v", err)
}
mockserver.Set("application", "key", "value")
updates := WatchUpdate()
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val := GetStringValue("key", "defaultValue")
if val != "value" {
t.Errorf("GetStringValue of key should = value, got %v", val)
}
keys := GetAllKeys("application")
if len(keys) != 1 {
t.Errorf("GetAllKeys should return 1 key")
}
mockserver.Set("application", "key", "newvalue")
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = defaultClient.GetStringValue("key", "defaultValue")
if val != "newvalue" {
t.Errorf("GetStringValue of key should = newvalue, got %v", val)
}
keys = GetAllKeys("application")
if len(keys) != 1 {
t.Errorf("GetAllKeys should return 1 key")
}
mockserver.Delete("application", "key")
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetStringValue("key", "defaultValue")
if val != "defaultValue" {
t.Errorf("GetStringValue of key should = defaultValue, got %v", val)
}
keys = GetAllKeys("application")
if len(keys) != 0 {
t.Errorf("GetAllKeys should return 0 key")
}
mockserver.Set("client.json", "content", `{"name":"agollo"}`)
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetNameSpaceContent("client.json", "{}")
if val != `{"name":"agollo"}` {
t.Errorf(`GetStringValue of client.json content should = {"name":"agollo"}, got %v`, val)
}
if err := SubscribeToNamespaces("new_namespace.json"); err != nil {
t.Error(err)
}
mockserver.Set("new_namespace.json", "key", "1")
select {
case <-updates:
case <-time.After(time.Millisecond * 30000):
}
val = GetStringValueWithNameSpace("new_namespace.json", "key", "defaultValue")
if val != `1` {
t.Errorf(`GetStringValueWithNameSpace of new_namespace.json content should = 1, got %v`, val)
}
}