...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package clientv3
16
17 import (
18 "context"
19 "testing"
20 "time"
21
22 "go.etcd.io/etcd/client/pkg/v3/testutil"
23 )
24
25 func TestTxnPanics(t *testing.T) {
26 testutil.RegisterLeakDetection(t)
27
28 kv := &kv{}
29
30 errc := make(chan string, 1)
31 df := func() {
32 if s := recover(); s != nil {
33 errc <- s.(string)
34 }
35 }
36
37 cmp := Compare(CreateRevision("foo"), "=", 0)
38 op := OpPut("foo", "bar")
39
40 tests := []struct {
41 f func()
42
43 err string
44 }{
45 {
46 f: func() {
47 defer df()
48 kv.Txn(context.TODO()).If(cmp).If(cmp)
49 },
50
51 err: "cannot call If twice!",
52 },
53 {
54 f: func() {
55 defer df()
56 kv.Txn(context.TODO()).Then(op).If(cmp)
57 },
58
59 err: "cannot call If after Then!",
60 },
61 {
62 f: func() {
63 defer df()
64 kv.Txn(context.TODO()).Else(op).If(cmp)
65 },
66
67 err: "cannot call If after Else!",
68 },
69 {
70 f: func() {
71 defer df()
72 kv.Txn(context.TODO()).Then(op).Then(op)
73 },
74
75 err: "cannot call Then twice!",
76 },
77 {
78 f: func() {
79 defer df()
80 kv.Txn(context.TODO()).Else(op).Then(op)
81 },
82
83 err: "cannot call Then after Else!",
84 },
85 {
86 f: func() {
87 defer df()
88 kv.Txn(context.TODO()).Else(op).Else(op)
89 },
90
91 err: "cannot call Else twice!",
92 },
93 }
94
95 for i, tt := range tests {
96 go tt.f()
97 select {
98 case err := <-errc:
99 if err != tt.err {
100 t.Errorf("#%d: got %s, wanted %s", i, err, tt.err)
101 }
102 case <-time.After(time.Second):
103 t.Errorf("#%d: did not panic, wanted panic %s", i, tt.err)
104 }
105 }
106 }
107
View as plain text