chaosmonkey_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package chaosmonkey
  14. import (
  15. "sync/atomic"
  16. "testing"
  17. )
  18. func TestDoWithPanic(t *testing.T) {
  19. var counter int64
  20. cm := New(func() {})
  21. tests := []Test{
  22. // No panic
  23. func(sem *Semaphore) {
  24. defer atomic.AddInt64(&counter, 1)
  25. sem.Ready()
  26. },
  27. // Panic after sem.Ready()
  28. func(sem *Semaphore) {
  29. defer atomic.AddInt64(&counter, 1)
  30. sem.Ready()
  31. panic("Panic after calling sem.Ready()")
  32. },
  33. // Panic before sem.Ready()
  34. func(sem *Semaphore) {
  35. defer atomic.AddInt64(&counter, 1)
  36. panic("Panic before calling sem.Ready()")
  37. },
  38. }
  39. for _, test := range tests {
  40. cm.Register(test)
  41. }
  42. cm.Do()
  43. // Check that all funcs in tests were called.
  44. if int(counter) != len(tests) {
  45. t.Errorf("Expected counter to be %v, but it was %v", len(tests), counter)
  46. }
  47. }