txn_test.go 3.2 KB

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