gopkg/middleware/etcd/lease_test.go

69 lines
1.5 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"
)
// 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)
}
}