txn_test.go 1.8 KB

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