// 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) } // 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) } // TestLeaseOnce ... // // Author : go_developer@163.com<白茶清欢> // // Date : 7:37 下午 2021/11/23 func TestLeaseOnce(t *testing.T) { key := "lock" fmt.Println(LeaseOnce(nil, key, "lock", 10)) for i := 0; i < 15; i++ { fmt.Println(Get(nil, key, 1)) time.Sleep(time.Second) } } // TestLeaseKeepAliveForever ... // // Author : go_developer@163.com<白茶清欢> // // Date : 7:54 下午 2021/11/23 func TestLeaseKeepAliveForever(t *testing.T) { key := "lock" keepAliveHandler := func(key string, data *clientv3.LeaseKeepAliveResponse) { fmt.Println(key, data.ID, data.TTL, data.Size(), data.String()) } go func() { fmt.Println(LeaseKeepAliveForever(nil, key, "lock", 10, keepAliveHandler)) }() for i := 0; i < 15; i++ { r, e := Get(nil, key, 1) fmt.Println("读取", r, e) time.Sleep(time.Second) } }