txn_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "context"
  17. "fmt"
  18. "testing"
  19. "time"
  20. "go.etcd.io/etcd/clientv3"
  21. "go.etcd.io/etcd/embed"
  22. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  23. "go.etcd.io/etcd/integration"
  24. "go.etcd.io/etcd/pkg/testutil"
  25. )
  26. func TestTxnError(t *testing.T) {
  27. defer testutil.AfterTest(t)
  28. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  29. defer clus.Terminate(t)
  30. kv := clus.RandClient()
  31. ctx := context.TODO()
  32. _, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar1"), clientv3.OpPut("foo", "bar2")).Commit()
  33. if err != rpctypes.ErrDuplicateKey {
  34. t.Fatalf("expected %v, got %v", rpctypes.ErrDuplicateKey, err)
  35. }
  36. ops := make([]clientv3.Op, int(embed.DefaultMaxTxnOps+10))
  37. for i := range ops {
  38. ops[i] = clientv3.OpPut(fmt.Sprintf("foo%d", i), "")
  39. }
  40. _, err = kv.Txn(ctx).Then(ops...).Commit()
  41. if err != rpctypes.ErrTooManyOps {
  42. t.Fatalf("expected %v, got %v", rpctypes.ErrTooManyOps, err)
  43. }
  44. }
  45. func TestTxnWriteFail(t *testing.T) {
  46. defer testutil.AfterTest(t)
  47. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  48. defer clus.Terminate(t)
  49. kv := clus.Client(0)
  50. clus.Members[0].Stop(t)
  51. txnc, getc := make(chan struct{}), make(chan struct{})
  52. go func() {
  53. ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
  54. defer cancel()
  55. resp, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  56. if err == nil {
  57. t.Errorf("expected error, got response %v", resp)
  58. }
  59. close(txnc)
  60. }()
  61. go func() {
  62. defer close(getc)
  63. select {
  64. case <-time.After(5 * time.Second):
  65. t.Errorf("timed out waiting for txn fail")
  66. case <-txnc:
  67. }
  68. // and ensure the put didn't take
  69. gresp, gerr := clus.Client(1).Get(context.TODO(), "foo")
  70. if gerr != nil {
  71. t.Error(gerr)
  72. }
  73. if len(gresp.Kvs) != 0 {
  74. t.Errorf("expected no keys, got %v", gresp.Kvs)
  75. }
  76. }()
  77. select {
  78. case <-time.After(5 * clus.Members[1].ServerConfig.ReqTimeout()):
  79. t.Fatalf("timed out waiting for get")
  80. case <-getc:
  81. }
  82. // reconnect so terminate doesn't complain about double-close
  83. clus.Members[0].Restart(t)
  84. }
  85. func TestTxnReadRetry(t *testing.T) {
  86. t.Skipf("skipping txn read retry test: re-enable after we do retry on txn read request")
  87. defer testutil.AfterTest(t)
  88. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  89. defer clus.Terminate(t)
  90. kv := clus.Client(0)
  91. thenOps := [][]clientv3.Op{
  92. {clientv3.OpGet("foo")},
  93. {clientv3.OpTxn(nil, []clientv3.Op{clientv3.OpGet("foo")}, nil)},
  94. {clientv3.OpTxn(nil, nil, nil)},
  95. {},
  96. }
  97. for i := range thenOps {
  98. clus.Members[0].Stop(t)
  99. <-clus.Members[0].StopNotify()
  100. donec := make(chan struct{})
  101. go func() {
  102. _, err := kv.Txn(context.TODO()).Then(thenOps[i]...).Commit()
  103. if err != nil {
  104. t.Errorf("expected response, got error %v", err)
  105. }
  106. donec <- struct{}{}
  107. }()
  108. // wait for txn to fail on disconnect
  109. time.Sleep(100 * time.Millisecond)
  110. // restart node; client should resume
  111. clus.Members[0].Restart(t)
  112. select {
  113. case <-donec:
  114. case <-time.After(2 * clus.Members[1].ServerConfig.ReqTimeout()):
  115. t.Fatalf("waited too long")
  116. }
  117. }
  118. }
  119. func TestTxnSuccess(t *testing.T) {
  120. defer testutil.AfterTest(t)
  121. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  122. defer clus.Terminate(t)
  123. kv := clus.Client(0)
  124. ctx := context.TODO()
  125. _, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. resp, err := kv.Get(ctx, "foo")
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" {
  134. t.Fatalf("unexpected Get response %v", resp)
  135. }
  136. }
  137. func TestTxnCompareRange(t *testing.T) {
  138. defer testutil.AfterTest(t)
  139. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  140. defer clus.Terminate(t)
  141. kv := clus.Client(0)
  142. fooResp, err := kv.Put(context.TODO(), "foo/", "bar")
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. if _, err = kv.Put(context.TODO(), "foo/a", "baz"); err != nil {
  147. t.Fatal(err)
  148. }
  149. tresp, terr := kv.Txn(context.TODO()).If(
  150. clientv3.Compare(
  151. clientv3.CreateRevision("foo/"), "=", fooResp.Header.Revision).
  152. WithPrefix(),
  153. ).Commit()
  154. if terr != nil {
  155. t.Fatal(terr)
  156. }
  157. if tresp.Succeeded {
  158. t.Fatal("expected prefix compare to false, got compares as true")
  159. }
  160. }
  161. func TestTxnNested(t *testing.T) {
  162. defer testutil.AfterTest(t)
  163. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  164. defer clus.Terminate(t)
  165. kv := clus.Client(0)
  166. tresp, err := kv.Txn(context.TODO()).
  167. If(clientv3.Compare(clientv3.Version("foo"), "=", 0)).
  168. Then(
  169. clientv3.OpPut("foo", "bar"),
  170. clientv3.OpTxn(nil, []clientv3.Op{clientv3.OpPut("abc", "123")}, nil)).
  171. Else(clientv3.OpPut("foo", "baz")).Commit()
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if len(tresp.Responses) != 2 {
  176. t.Errorf("expected 2 top-level txn responses, got %+v", tresp.Responses)
  177. }
  178. // check txn writes were applied
  179. resp, err := kv.Get(context.TODO(), "foo")
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Value) != "bar" {
  184. t.Errorf("unexpected Get response %+v", resp)
  185. }
  186. resp, err = kv.Get(context.TODO(), "abc")
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Value) != "123" {
  191. t.Errorf("unexpected Get response %+v", resp)
  192. }
  193. }