txn_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. donec <- struct{}{}
  47. // and ensure the put didn't take
  48. gresp, gerr := kv.Get("foo", 0)
  49. if gerr != nil {
  50. t.Fatal(gerr)
  51. }
  52. if len(gresp.Kvs) != 0 {
  53. t.Fatalf("expected no keys, got %v", gresp.Kvs)
  54. }
  55. donec <- struct{}{}
  56. }()
  57. select {
  58. case <-time.After(5 * time.Second):
  59. t.Fatalf("timed out waiting for restart")
  60. case <-donec:
  61. }
  62. select {
  63. case <-time.After(5 * time.Second):
  64. t.Fatalf("timed out waiting for get")
  65. case <-donec:
  66. }
  67. }
  68. func TestTxnReadRetry(t *testing.T) {
  69. defer testutil.AfterTest(t)
  70. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  71. defer clus.Terminate(t)
  72. kv := clientv3.NewKV(clus.Client(0))
  73. clus.Members[0].Stop(t)
  74. <-clus.Members[0].StopNotify()
  75. donec := make(chan struct{})
  76. go func() {
  77. _, err := kv.Txn().Then(clientv3.OpGet("foo", 0)).Commit()
  78. if err != nil {
  79. t.Fatalf("expected response, got error %v", err)
  80. }
  81. donec <- struct{}{}
  82. }()
  83. // wait for txn to fail on disconnect
  84. time.Sleep(100 * time.Millisecond)
  85. // restart node; client should resume
  86. clus.Members[0].Restart(t)
  87. select {
  88. case <-donec:
  89. case <-time.After(5 * time.Second):
  90. t.Fatalf("waited too long")
  91. }
  92. }
  93. func TestTxnSuccess(t *testing.T) {
  94. defer testutil.AfterTest(t)
  95. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  96. defer clus.Terminate(t)
  97. kv := clientv3.NewKV(clus.Client(0))
  98. _, err := kv.Txn().Then(clientv3.OpPut("foo", "bar", 0)).Commit()
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. resp, err := kv.Get("foo", 0)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" {
  107. t.Fatalf("unexpected Get response %v", resp)
  108. }
  109. }