txn_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "fmt"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  21. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  22. "github.com/coreos/etcd/integration"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. "golang.org/x/net/context"
  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 := clientv3.NewKV(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, v3rpc.MaxOpsPerTxn+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 := clientv3.NewKV(clus.Client(0))
  50. ctx := context.TODO()
  51. clus.Members[0].Stop(t)
  52. <-clus.Members[0].StopNotify()
  53. donec := make(chan struct{})
  54. go func() {
  55. resp, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  56. if err == nil {
  57. t.Fatalf("expected error, got response %v", resp)
  58. }
  59. donec <- struct{}{}
  60. }()
  61. dialTimeout := 5 * time.Second
  62. select {
  63. case <-time.After(2*dialTimeout + time.Second):
  64. t.Fatalf("timed out waiting for txn to fail")
  65. case <-donec:
  66. // don't restart cluster until txn errors out
  67. }
  68. go func() {
  69. // reconnect so terminate doesn't complain about double-close
  70. clus.Members[0].Restart(t)
  71. // wait for etcdserver to get established (CI races and get req times out)
  72. time.Sleep(2 * time.Second)
  73. donec <- struct{}{}
  74. // and ensure the put didn't take
  75. gresp, gerr := kv.Get(ctx, "foo")
  76. if gerr != nil {
  77. t.Fatal(gerr)
  78. }
  79. if len(gresp.Kvs) != 0 {
  80. t.Fatalf("expected no keys, got %v", gresp.Kvs)
  81. }
  82. donec <- struct{}{}
  83. }()
  84. select {
  85. case <-time.After(5 * time.Second):
  86. t.Fatalf("timed out waiting for restart")
  87. case <-donec:
  88. }
  89. select {
  90. case <-time.After(5 * time.Second):
  91. t.Fatalf("timed out waiting for get")
  92. case <-donec:
  93. }
  94. }
  95. func TestTxnReadRetry(t *testing.T) {
  96. defer testutil.AfterTest(t)
  97. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  98. defer clus.Terminate(t)
  99. kv := clientv3.NewKV(clus.Client(0))
  100. clus.Members[0].Stop(t)
  101. <-clus.Members[0].StopNotify()
  102. donec := make(chan struct{})
  103. go func() {
  104. ctx := context.TODO()
  105. _, err := kv.Txn(ctx).Then(clientv3.OpGet("foo")).Commit()
  106. if err != nil {
  107. t.Fatalf("expected response, got error %v", err)
  108. }
  109. donec <- struct{}{}
  110. }()
  111. // wait for txn to fail on disconnect
  112. time.Sleep(100 * time.Millisecond)
  113. // restart node; client should resume
  114. clus.Members[0].Restart(t)
  115. select {
  116. case <-donec:
  117. case <-time.After(5 * time.Second):
  118. t.Fatalf("waited too long")
  119. }
  120. }
  121. func TestTxnSuccess(t *testing.T) {
  122. defer testutil.AfterTest(t)
  123. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  124. defer clus.Terminate(t)
  125. kv := clientv3.NewKV(clus.Client(0))
  126. ctx := context.TODO()
  127. _, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. resp, err := kv.Get(ctx, "foo")
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" {
  136. t.Fatalf("unexpected Get response %v", resp)
  137. }
  138. }