github.com/GuanceCloud/cliutils@v1.1.21/system/rtpanic/rtpanic_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package rtpanic 7 8 import ( 9 "fmt" 10 "log" 11 "testing" 12 ) 13 14 func TestRecover(t *testing.T) { 15 var f RecoverCallback 16 panicCnt := 0 17 18 f = func(trace []byte, err error) { 19 defer Recover(f, nil) 20 21 if trace != nil { 22 panicCnt++ 23 log.Printf("try panic(at %d), err: %v, trace: %s", panicCnt, err, string(trace)) 24 if panicCnt >= 1 { 25 return 26 } 27 } 28 29 panic(fmt.Errorf("panic error")) 30 } 31 32 f(nil, nil) 33 }