-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigWatcher_test.go
52 lines (45 loc) · 1.15 KB
/
configWatcher_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
package etcd
import (
"fmt"
"testing"
"time"
"github.com/freshcn/log"
)
func init() {
log.SetDebug(true)
}
var (
testConfig = Config{
Endpoints: []string{"127.0.0.1:2379"},
}
)
func TestConfigWatcher(t *testing.T) {
New(testConfig)
test := NewConfigWatcher("config")
defer test.Close()
for i := 0; i < 5; i++ {
Put(fmt.Sprintf("CONFIG/test%d", i), fmt.Sprintf("%d", time.Now().Unix()))
time.Sleep(time.Second * 1)
fmt.Println("config key", fmt.Sprintf("test%d", i), " data:", test.Get(fmt.Sprintf("test%d", i)).Int())
}
}
func TestConfigWatcherHooks(t *testing.T) {
New(testConfig)
test := NewConfigWatcher("config")
defer test.Close()
// 添加前缀hook
test.AddHook("test", func(new ConfigWatcherItem, old ConfigWatcherItem) {
fmt.Println("```key:", new.Key, " New value:", new.Int(), " old value:", old.Int())
}, true)
var key string
for i := 0; i < 5; i++ {
key = fmt.Sprintf("CONFIG/test%d", 0)
Put(key, fmt.Sprintf("%d", time.Now().Unix()))
time.Sleep(time.Second * 1)
if i == 3 {
fmt.Println("=== del key ", key)
Del(key)
}
fmt.Println("|||config key:", key, " data:", test.Get(fmt.Sprintf("test%d", 0)).Int())
}
}