forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_test.go
167 lines (115 loc) · 3.1 KB
/
repository_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
164
165
166
167
package agollo
import (
"testing"
"github.com/zouyx/agollo/test"
"time"
"encoding/json"
)
//init param
func init() {
//wait 1s for another go routine update apollo config
time.Sleep(1*time.Second)
createMockApolloConfig(configCacheExpireTime)
}
func createMockApolloConfig(expireTime int)map[string]string{
configs:=make(map[string]string,0)
//string
configs["string"]="value"
//int
configs["int"]="1"
//float
configs["float"]="190.3"
//bool
configs["bool"]="true"
updateApolloConfigCache(configs,expireTime)
return configs
}
func TestUpdateApolloConfigNull(t *testing.T) {
time.Sleep(1*time.Second)
var currentConfig *ApolloConnConfig
currentJson,err:=json.Marshal(currentConnApolloConfig)
test.Nil(t,err)
t.Log("currentJson:",string(currentJson))
test.Equal(t,false,string(currentJson)=="")
json.Unmarshal(currentJson,¤tConfig)
test.NotNil(t,currentConfig)
updateApolloConfig(nil)
//make sure currentConnApolloConfig was not modified
test.Equal(t,currentConfig.NamespaceName,currentConnApolloConfig.NamespaceName)
test.Equal(t,currentConfig.AppId,currentConnApolloConfig.AppId)
test.Equal(t,currentConfig.Cluster,currentConnApolloConfig.Cluster)
test.Equal(t,currentConfig.ReleaseKey,currentConnApolloConfig.ReleaseKey)
}
func TestGetApolloConfigCache(t *testing.T) {
cache:=GetApolloConfigCache()
test.NotNil(t,cache)
}
func TestGetConfigValueTimeout(t *testing.T) {
expireTime:=5
configs:=createMockApolloConfig(expireTime)
for k,v:=range configs{
test.Equal(t,v,getValue(k))
}
time.Sleep(time.Duration(expireTime)*time.Second)
for k,_:=range configs{
test.Equal(t,"",getValue(k))
}
}
func TestGetConfigValueNullApolloConfig(t *testing.T) {
//clear Configurations
apolloConfigCache.Clear()
//test getValue
value:=getValue("joe")
test.Equal(t,empty,value)
//test GetStringValue
defaultValue:="j"
//test default
v:=GetStringValue("joe",defaultValue)
test.Equal(t,defaultValue,v)
createMockApolloConfig(configCacheExpireTime)
}
func TestGetIntValue(t *testing.T) {
defaultValue:=100000
//test default
v:=GetIntValue("joe",defaultValue)
test.Equal(t,defaultValue,v)
//normal value
v=GetIntValue("int",defaultValue)
test.Equal(t,1,v)
//error type
v=GetIntValue("float",defaultValue)
test.Equal(t,defaultValue,v)
}
func TestGetFloatValue(t *testing.T) {
defaultValue:=100000.1
//test default
v:=GetFloatValue("joe",defaultValue)
test.Equal(t,defaultValue,v)
//normal value
v=GetFloatValue("float",defaultValue)
test.Equal(t,190.3,v)
//error type
v=GetFloatValue("int",defaultValue)
test.Equal(t,float64(1),v)
}
func TestGetBoolValue(t *testing.T) {
defaultValue:=false
//test default
v:=GetBoolValue("joe",defaultValue)
test.Equal(t,defaultValue,v)
//normal value
v=GetBoolValue("bool",defaultValue)
test.Equal(t,true,v)
//error type
v=GetBoolValue("float",defaultValue)
test.Equal(t,defaultValue,v)
}
func TestGetStringValue(t *testing.T) {
defaultValue:="j"
//test default
v:=GetStringValue("joe",defaultValue)
test.Equal(t,defaultValue,v)
//normal value
v=GetStringValue("string",defaultValue)
test.Equal(t,"value",v)
}