txn_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 clientv3
  15. import (
  16. "testing"
  17. "time"
  18. "github.com/coreos/etcd/pkg/testutil"
  19. )
  20. func TestTxnPanics(t *testing.T) {
  21. defer testutil.AfterTest(t)
  22. kv := NewKV(&Client{})
  23. errc := make(chan string)
  24. df := func() {
  25. if s := recover(); s != nil {
  26. errc <- s.(string)
  27. }
  28. }
  29. k, tgt := CreatedRevision("foo")
  30. cmp := Compare(k, tgt, "=", 0)
  31. op := OpPut("foo", "bar", 0)
  32. tests := []struct {
  33. f func()
  34. err string
  35. }{
  36. {
  37. f: func() {
  38. defer df()
  39. kv.Txn().If(cmp).If(cmp)
  40. },
  41. err: "cannot call If twice!",
  42. },
  43. {
  44. f: func() {
  45. defer df()
  46. kv.Txn().Then(op).If(cmp)
  47. },
  48. err: "cannot call If after Then!",
  49. },
  50. {
  51. f: func() {
  52. defer df()
  53. kv.Txn().Else(op).If(cmp)
  54. },
  55. err: "cannot call If after Else!",
  56. },
  57. {
  58. f: func() {
  59. defer df()
  60. kv.Txn().Then(op).Then(op)
  61. },
  62. err: "cannot call Then twice!",
  63. },
  64. {
  65. f: func() {
  66. defer df()
  67. kv.Txn().Else(op).Then(op)
  68. },
  69. err: "cannot call Then after Else!",
  70. },
  71. {
  72. f: func() {
  73. defer df()
  74. kv.Txn().Else(op).Else(op)
  75. },
  76. err: "cannot call Else twice!",
  77. },
  78. }
  79. for i, tt := range tests {
  80. go tt.f()
  81. select {
  82. case err := <-errc:
  83. if err != tt.err {
  84. t.Errorf("#%d: got %s, wanted %s", i, err, tt.err)
  85. }
  86. case <-time.After(time.Second):
  87. t.Errorf("#%d: did not panic, wanted panic %s", i, tt.err)
  88. }
  89. }
  90. }