runtime_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2014 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 runtime
  14. import (
  15. "fmt"
  16. "testing"
  17. )
  18. func TestHandleCrash(t *testing.T) {
  19. defer func() {
  20. if x := recover(); x == nil {
  21. t.Errorf("Expected a panic to recover from")
  22. }
  23. }()
  24. defer HandleCrash()
  25. panic("Test Panic")
  26. }
  27. func TestCustomHandleCrash(t *testing.T) {
  28. old := PanicHandlers
  29. defer func() { PanicHandlers = old }()
  30. var result interface{}
  31. PanicHandlers = []func(interface{}){
  32. func(r interface{}) {
  33. result = r
  34. },
  35. }
  36. func() {
  37. defer func() {
  38. if x := recover(); x == nil {
  39. t.Errorf("Expected a panic to recover from")
  40. }
  41. }()
  42. defer HandleCrash()
  43. panic("test")
  44. }()
  45. if result != "test" {
  46. t.Errorf("did not receive custom handler")
  47. }
  48. }
  49. func TestCustomHandleError(t *testing.T) {
  50. old := ErrorHandlers
  51. defer func() { ErrorHandlers = old }()
  52. var result error
  53. ErrorHandlers = []func(error){
  54. func(err error) {
  55. result = err
  56. },
  57. }
  58. err := fmt.Errorf("test")
  59. HandleError(err)
  60. if result != err {
  61. t.Errorf("did not receive custom handler")
  62. }
  63. }