txn_test.go 3.4 KB

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