增加监听一次key变化的处理

This commit is contained in:
2021-11-23 16:55:05 +08:00
parent ada41fa40a
commit 0b5e55cefd
3 changed files with 86 additions and 0 deletions

View File

@ -114,3 +114,48 @@ func TestWatchKeyWithCancelByChangeCallback(t *testing.T) {
}()
WatchKeyWithCancel(nil, key, dealFunc, cancelChan, cancelFunc)
}
// TestWatchKeyOnce ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:50 下午 2021/11/23
func TestWatchKeyOnce(t *testing.T) {
key := "name"
dealFunc := func(data *clientv3.Event) {
fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision)
}
timeoutFunc := func(key string, timeout time.Duration) {
fmt.Println("监听超时", key, timeout)
}
timeout := 10 * time.Second
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
}()
WatchKeyOnce(nil, key, dealFunc, timeout, timeoutFunc)
}
// TestWatchKeyOnceForTimeout ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:53 下午 2021/11/23
func TestWatchKeyOnceForTimeout(t *testing.T) {
key := "name"
dealFunc := func(data *clientv3.Event) {
fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision)
}
timeoutFunc := func(key string, timeout time.Duration) {
fmt.Println("监听超时", key, timeout)
}
timeout := time.Second
WatchKeyOnce(nil, key, dealFunc, timeout, timeoutFunc)
}