txn_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 := 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 := 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.Fatalf("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.Fatalf("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.Fatal(gerr)
  72. }
  73. if len(gresp.Kvs) != 0 {
  74. t.Fatalf("expected no keys, got %v", gresp.Kvs)
  75. }
  76. }()
  77. select {
  78. case <-time.After(2 * 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. clus.Members[0].Stop(t)
  92. <-clus.Members[0].StopNotify()
  93. donec := make(chan struct{})
  94. go func() {
  95. ctx := context.TODO()
  96. _, err := kv.Txn(ctx).Then(clientv3.OpGet("foo")).Commit()
  97. if err != nil {
  98. t.Fatalf("expected response, got error %v", err)
  99. }
  100. donec <- struct{}{}
  101. }()
  102. // wait for txn to fail on disconnect
  103. time.Sleep(100 * time.Millisecond)
  104. // restart node; client should resume
  105. clus.Members[0].Restart(t)
  106. select {
  107. case <-donec:
  108. case <-time.After(2 * clus.Members[1].ServerConfig.ReqTimeout()):
  109. t.Fatalf("waited too long")
  110. }
  111. }
  112. func TestTxnSuccess(t *testing.T) {
  113. defer testutil.AfterTest(t)
  114. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  115. defer clus.Terminate(t)
  116. kv := clus.Client(0)
  117. ctx := context.TODO()
  118. _, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. resp, err := kv.Get(ctx, "foo")
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" {
  127. t.Fatalf("unexpected Get response %v", resp)
  128. }
  129. }