txn_test.go 1.9 KB

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