vitess.io/vitess@v0.16.2/go/exit/exit_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreedto in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package exit 18 19 import ( 20 "testing" 21 ) 22 23 type repanicType int 24 25 func TestReturn(t *testing.T) { 26 defer func() { 27 err := recover() 28 if err == nil { 29 t.Errorf("Return() did not panic with exit code") 30 } 31 32 switch code := err.(type) { 33 case exitCode: 34 if code != 152 { 35 t.Errorf("got %v, want %v", code, 152) 36 } 37 default: 38 panic(err) 39 } 40 }() 41 42 Return(152) 43 } 44 45 func TestRecover(t *testing.T) { 46 var code int 47 48 exitFunc = func(c int) { 49 code = c 50 } 51 52 func() { 53 defer Recover() 54 Return(8235) 55 }() 56 57 if code != 8235 { 58 t.Errorf("got %v, want %v", code, 8235) 59 } 60 } 61 62 func TestRecoverRepanic(t *testing.T) { 63 defer func() { 64 err := recover() 65 66 if err == nil { 67 t.Errorf("Recover() didn't re-panic an error other than exitCode") 68 return 69 } 70 71 if _, ok := err.(repanicType); !ok { 72 panic(err) // something unexpected went wrong 73 } 74 }() 75 76 defer Recover() 77 78 panic(repanicType(1)) 79 } 80 81 func TestRecoverAll(t *testing.T) { 82 exitFunc = func(int) {} 83 84 defer func() { 85 err := recover() 86 87 if err != nil { 88 t.Errorf("RecoverAll() didn't absorb all panics") 89 } 90 }() 91 92 defer RecoverAll() 93 94 panic(repanicType(1)) 95 } 96 97 // TestRecoverNil checks that Recover() does nothing when there is no panic. 98 func TestRecoverNil(t *testing.T) { 99 defer Recover() 100 }