-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlm_test.go
More file actions
100 lines (93 loc) · 2.23 KB
/
Copy pathdlm_test.go
File metadata and controls
100 lines (93 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package etcd_dlm
import (
"context"
"reflect"
"sync"
"testing"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
)
func TestLock(t *testing.T) {
ctx := context.TODO()
conf := clientv3.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
DialTimeout: 3 * time.Second,
DialOptions: []grpc.DialOption{grpc.WithBlock()},
}
cli, err := clientv3.New(conf)
if err != nil {
t.Error(err)
}
type args struct {
ctx context.Context
cli *clientv3.Client
cfg DlmConfig
}
tests := []struct {
name string
args args
wantErr error
wantRevoke func()
wantSucceed bool
}{
{name: "TestLock1", args: struct {
ctx context.Context
cli *clientv3.Client
cfg DlmConfig
}{ctx: ctx, cli: cli, cfg: struct {
Key string
Ttl int64
IsKeepaLive bool
CloseFunc func()
}{Key: "test", Ttl: 10, IsKeepaLive: true, CloseFunc: func() {
t.Log("close")
}}}, wantErr: nil, wantRevoke: nil, wantSucceed: true},
{name: "TestLock2", args: struct {
ctx context.Context
cli *clientv3.Client
cfg DlmConfig
}{ctx: ctx, cli: cli, cfg: struct {
Key string
Ttl int64
IsKeepaLive bool
CloseFunc func()
}{Key: "test", Ttl: 10, IsKeepaLive: true, CloseFunc: func() {
t.Log("close")
}}}, wantErr: nil, wantRevoke: nil, wantSucceed: true},
{name: "TestLock3", args: struct {
ctx context.Context
cli *clientv3.Client
cfg DlmConfig
}{ctx: ctx, cli: cli, cfg: struct {
Key string
Ttl int64
IsKeepaLive bool
CloseFunc func()
}{Key: "test", Ttl: 10, IsKeepaLive: true, CloseFunc: func() {
t.Log("close")
}}}, wantErr: nil, wantRevoke: nil, wantSucceed: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
gotErr, gotRevoke, gotSucceed := Lock(tt.args.ctx, tt.args.cli, tt.args.cfg)
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("Lock() gotErr = %v, want %v", gotErr, tt.wantErr)
}
if gotSucceed {
t.Log("抢锁成功")
time.Sleep(2 * time.Second)
gotRevoke()
time.Sleep(2 * time.Second)
}
wg.Done()
}()
}
wg.Wait()
})
}
}