diff --git a/middleware/etcd/lease_test.go b/middleware/etcd/lease_test.go new file mode 100644 index 0000000..d90da03 --- /dev/null +++ b/middleware/etcd/lease_test.go @@ -0,0 +1,68 @@ +// Package etcd... +// +// Description : etcd... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-11-24 11:40 上午 +package etcd + +import ( + "fmt" + "testing" + "time" +) + +// 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(data *LeaseKeepAliveData) { + fmt.Println(key, data.LeaseDetail.ID, data.LeaseDetail.TTL, data.LeaseCnt) + } + 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) + } +} + +// TestLeaseKeepAliveWithDuration ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:56 下午 2021/11/23 +func TestLeaseKeepAliveWithDuration(t *testing.T) { + key := "lock" + keepAliveHandler := func(data *LeaseKeepAliveData) { + fmt.Println(key, data.LeaseDetail.ID, data.LeaseDetail.TTL, data.LeaseCnt) + } + go func() { + fmt.Println(LeaseKeepAliveWithDuration(nil, key, "lock", 1, keepAliveHandler, nil, 5)) + }() + for i := 0; i < 15; i++ { + r, e := Get(nil, key, 1) + fmt.Println("读取", r, e) + time.Sleep(time.Second) + } +} diff --git a/middleware/etcd/string_test.go b/middleware/etcd/string_test.go index 46ae1da..c5aca44 100644 --- a/middleware/etcd/string_test.go +++ b/middleware/etcd/string_test.go @@ -10,7 +10,6 @@ package etcd import ( "fmt" "testing" - "time" "git.zhangdeman.cn/zhangdeman/gopkg/util" @@ -56,175 +55,3 @@ func TestGetWithPrefix(t *testing.T) { } fmt.Println(GetWithPrefix(nil, prefix, 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(data *LeaseKeepAliveData) { - fmt.Println(key, data.LeaseDetail.ID, data.LeaseDetail.TTL, data.LeaseCnt) - } - 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) - } -} - -// TestLeaseKeepAliveWithDuration ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:56 下午 2021/11/23 -func TestLeaseKeepAliveWithDuration(t *testing.T) { - key := "lock" - keepAliveHandler := func(data *LeaseKeepAliveData) { - fmt.Println(key, data.LeaseDetail.ID, data.LeaseDetail.TTL, data.LeaseCnt) - } - go func() { - fmt.Println(LeaseKeepAliveWithDuration(nil, key, "lock", 1, keepAliveHandler, nil, 5)) - }() - for i := 0; i < 15; i++ { - r, e := Get(nil, key, 1) - fmt.Println("读取", r, e) - time.Sleep(time.Second) - } -} diff --git a/middleware/etcd/watch_test.go b/middleware/etcd/watch_test.go new file mode 100644 index 0000000..18dd513 --- /dev/null +++ b/middleware/etcd/watch_test.go @@ -0,0 +1,134 @@ +// 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) +}