txn_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2016 CoreOS, Inc.
  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. "testing"
  17. "time"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/integration"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestTxnWriteFail(t *testing.T) {
  24. defer testutil.AfterTest(t)
  25. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  26. defer clus.Terminate(t)
  27. kv := clientv3.NewKV(clus.Client(0))
  28. ctx := context.TODO()
  29. clus.Members[0].Stop(t)
  30. <-clus.Members[0].StopNotify()
  31. donec := make(chan struct{})
  32. go func() {
  33. resp, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  34. if err == nil {
  35. t.Fatalf("expected error, got response %v", resp)
  36. }
  37. donec <- struct{}{}
  38. }()
  39. select {
  40. case <-time.After(5 * time.Second):
  41. t.Fatalf("timed out waiting for txn to fail")
  42. case <-donec:
  43. // don't restart cluster until txn errors out
  44. }
  45. go func() {
  46. // reconnect so terminate doesn't complain about double-close
  47. clus.Members[0].Restart(t)
  48. // wait for etcdserver to get established (CI races and get req times out)
  49. time.Sleep(2 * time.Second)
  50. donec <- struct{}{}
  51. // and ensure the put didn't take
  52. gresp, gerr := kv.Get(ctx, "foo")
  53. if gerr != nil {
  54. t.Fatal(gerr)
  55. }
  56. if len(gresp.Kvs) != 0 {
  57. t.Fatalf("expected no keys, got %v", gresp.Kvs)
  58. }
  59. donec <- struct{}{}
  60. }()
  61. select {
  62. case <-time.After(5 * time.Second):
  63. t.Fatalf("timed out waiting for restart")
  64. case <-donec:
  65. }
  66. select {
  67. case <-time.After(5 * time.Second):
  68. t.Fatalf("timed out waiting for get")
  69. case <-donec:
  70. }
  71. }
  72. func TestTxnReadRetry(t *testing.T) {
  73. defer testutil.AfterTest(t)
  74. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  75. defer clus.Terminate(t)
  76. kv := clientv3.NewKV(clus.Client(0))
  77. clus.Members[0].Stop(t)
  78. <-clus.Members[0].StopNotify()
  79. donec := make(chan struct{})
  80. go func() {
  81. ctx := context.TODO()
  82. _, err := kv.Txn(ctx).Then(clientv3.OpGet("foo")).Commit()
  83. if err != nil {
  84. t.Fatalf("expected response, got error %v", err)
  85. }
  86. donec <- struct{}{}
  87. }()
  88. // wait for txn to fail on disconnect
  89. time.Sleep(100 * time.Millisecond)
  90. // restart node; client should resume
  91. clus.Members[0].Restart(t)
  92. select {
  93. case <-donec:
  94. case <-time.After(5 * time.Second):
  95. t.Fatalf("waited too long")
  96. }
  97. }
  98. func TestTxnSuccess(t *testing.T) {
  99. defer testutil.AfterTest(t)
  100. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  101. defer clus.Terminate(t)
  102. kv := clientv3.NewKV(clus.Client(0))
  103. ctx := context.TODO()
  104. _, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar")).Commit()
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. resp, err := kv.Get(ctx, "foo")
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" {
  113. t.Fatalf("unexpected Get response %v", resp)
  114. }
  115. }