gopkg/middleware/etcd/watch_test.go

135 lines
3.3 KiB
Go
Raw Permalink Normal View History

2021-11-24 11:41:15 +08:00
// Package etcd...
//
// Description : etcd...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-24 11:40 上午
package etcd
import (
"fmt"
"testing"
"time"
"go.etcd.io/etcd/clientv3"
)
// TestWatchKey ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:06 下午 2021/11/23
func TestWatchKey(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)
}
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
}()
WatchKey(nil, key, dealFunc)
}
// TestWatchKeyWithCancel ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:57 下午 2021/11/23
func TestWatchKeyWithCancel(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)
}
cancelFunc := func(key string, data interface{}) {
fmt.Println("取消监听 : ", key, data)
}
cancelChan := make(chan interface{}, 1)
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
time.Sleep(10 * time.Second)
cancelChan <- "Hello World"
}()
WatchKeyWithCancel(nil, key, dealFunc, cancelChan, cancelFunc)
}
// TestWatchKeyWithCancelByChangeCallback ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:27 下午 2021/11/23
func TestWatchKeyWithCancelByChangeCallback(t *testing.T) {
key := "name"
cancelChan := make(chan interface{}, 1)
dealFunc := func(data *clientv3.Event) {
fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision)
if string(data.Kv.Value) == "test-29" {
cancelChan <- "Hello World!"
}
}
cancelFunc := func(key string, data interface{}) {
fmt.Println("取消监听 : ", key, data)
}
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
}()
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)
}