txn_test.go 3.9 KB

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