github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/recover_test.go (about) 1 package easy 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "log" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 var _ = log.Println 15 16 var wantPanicLoc string 17 18 func willPanic() { 19 wantPanicLoc = "gopkg/v2/easy.willPanic:20" 20 panic("oops...") 21 } 22 23 func willPanicCaller() { 24 willPanic() 25 } 26 27 func TestSafe(t *testing.T) { 28 var err error 29 assert.NotPanics(t, func() { 30 err = Safe(func() { 31 willPanic() 32 })() 33 }) 34 assert.NotNil(t, err) 35 assert.IsType(t, &PanicError{}, err) 36 37 got := fmt.Sprintf("%+v", err) 38 t.Log(got) 39 assert.Contains(t, got, "panic: oops..., location: github.com/jxskiss/"+wantPanicLoc) 40 } 41 42 func TestSafe1(t *testing.T) { 43 var err error 44 assert.NotPanics(t, func() { 45 err = Safe1(func() error { 46 willPanic() 47 return errors.New("test error") 48 })() 49 }) 50 assert.NotNil(t, err) 51 assert.IsType(t, &PanicError{}, err) 52 53 got := fmt.Sprintf("%+v", err) 54 t.Log(got) 55 assert.Contains(t, got, "panic: oops..., location: github.com/jxskiss/"+wantPanicLoc) 56 } 57 58 func TestRecover(t *testing.T) { 59 var err error 60 assert.NotPanics(t, func() { 61 func() { 62 defer Recover(&err) 63 willPanic() 64 }() 65 }) 66 assert.NotNil(t, err) 67 68 got := fmt.Sprintf("%+v", err) 69 assert.Contains(t, got, "panic: oops..., location: github.com/jxskiss/"+wantPanicLoc) 70 } 71 72 func TestNewRecoverFunc(t *testing.T) { 73 var got string 74 recoverfn := NewRecoverFunc(func(ctx context.Context, panicErr *PanicError) { 75 got = fmt.Sprintf("%+v", panicErr) 76 }) 77 func() { 78 defer recoverfn(context.Background(), nil) 79 willPanic() 80 }() 81 t.Log(got) 82 assert.Contains(t, got, "panic: oops..., location: github.com/jxskiss/"+wantPanicLoc) 83 assert.Contains(t, got, "gopkg/easy/recover_test.go:20 (github.com/jxskiss/gopkg/v2/easy.willPanic)") 84 assert.Contains(t, got, "gopkg/easy/recover_test.go:79 (github.com/jxskiss/gopkg/v2/easy.TestNewRecoverFunc.func2)") 85 assert.Contains(t, got, "gopkg/easy/recover_test.go:80 (github.com/jxskiss/gopkg/v2/easy.TestNewRecoverFunc)") 86 } 87 88 func TestIdentifyPanicLoc(t *testing.T) { 89 var panicLoc1 string 90 func() { 91 defer func() { 92 recover() 93 panicLoc1 = IdentifyPanic() 94 }() 95 willPanic() 96 }() 97 t.Log(panicLoc1) 98 assert.True(t, strings.HasSuffix(panicLoc1, wantPanicLoc)) 99 100 var panicLoc2 string 101 func() { 102 defer func() { 103 recover() 104 panicLoc2 = IdentifyPanic() 105 }() 106 willPanicCaller() 107 }() 108 t.Log(panicLoc2) 109 assert.True(t, strings.HasSuffix(panicLoc2, wantPanicLoc)) 110 } 111 112 func TestPanicOnError(t *testing.T) { 113 panicErr := errors.New("dummy panic error") 114 willPanic := func() (int, error) { 115 return 123, panicErr 116 } 117 118 x, gotErr := willPanic() 119 assert.PanicsWithValue(t, panicErr, func() { 120 PanicOnError(x, gotErr) 121 }) 122 123 assert.PanicsWithValue(t, panicErr, func() { 124 PanicOnError(willPanic()) 125 }) 126 }