// Package etcd... // // Description : etcd... // // Author : go_developer@163.com<白茶清欢> // // Date : 2021-11-23 12:27 下午 package etcd import ( "fmt" "testing" "time" "go.etcd.io/etcd/clientv3" ) func init() { err := InitEtcdClient(clientv3.Config{ Endpoints: []string{"localhost:2379"}, }) if nil != err { panic(err.Error()) } } // TestPut ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:27 下午 2021/11/23 func TestPut(t *testing.T) { fmt.Println(Put(nil, "name", "zhangdeman", 0)) } // TestGet ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:30 下午 2021/11/23 func TestGet(t *testing.T) { fmt.Println(Get(nil, "name", 0)) } // 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) }