txn_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. resp, err := kv.Txn().Then(clientv3.OpPut("foo", "bar", 0)).Commit()
  30. if err == nil {
  31. t.Fatalf("expected error, got response %v", resp)
  32. }
  33. // reconnect so cluster terminate doesn't complain about double-close
  34. clus.Members[0].Restart(t)
  35. // and ensure the put didn't take
  36. gresp, gerr := kv.Get("foo", 0)
  37. if gerr != nil {
  38. t.Fatal(gerr)
  39. }
  40. if len(gresp.Kvs) != 0 {
  41. t.Fatalf("expected no keys, got %v", gresp.Kvs)
  42. }
  43. }
  44. func TestTxnReadRetry(t *testing.T) {
  45. defer testutil.AfterTest(t)
  46. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  47. defer clus.Terminate(t)
  48. kv := clientv3.NewKV(clus.Client(0))
  49. clus.Members[0].Stop(t)
  50. <-clus.Members[0].StopNotify()
  51. donec := make(chan struct{})
  52. go func() {
  53. _, err := kv.Txn().Then(clientv3.OpGet("foo", 0)).Commit()
  54. if err != nil {
  55. t.Fatalf("expected response, got error %v", err)
  56. }
  57. donec <- struct{}{}
  58. }()
  59. // wait for txn to fail on disconnect
  60. time.Sleep(100 * time.Millisecond)
  61. // restart node; client should resume
  62. clus.Members[0].Restart(t)
  63. select {
  64. case <-donec:
  65. case <-time.After(5 * time.Second):
  66. t.Fatalf("waited too long")
  67. }
  68. }
  69. func TestTxnSuccess(t *testing.T) {
  70. defer testutil.AfterTest(t)
  71. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  72. defer clus.Terminate(t)
  73. kv := clientv3.NewKV(clus.Client(0))
  74. _, err := kv.Txn().Then(clientv3.OpPut("foo", "bar", 0)).Commit()
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. resp, err := kv.Get("foo", 0)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" {
  83. t.Fatalf("unexpected Get response %v", resp)
  84. }
  85. }