mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-04-28 17:10:14 +08:00
test: add a panic catcher test
This commit is contained in:
parent
ef3e50c494
commit
8e16a4a4d7
28
internal/catch/catch_test.go
Normal file
28
internal/catch/catch_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package catch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCatch(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
oldPanicWriter := panicWriter
|
||||
t.Cleanup(func() { panicWriter = oldPanicWriter })
|
||||
panicWriter = buf
|
||||
|
||||
panicAndCatch := func() (err error) {
|
||||
defer func() { HandlePanic(recover(), &err, "somewhere") }()
|
||||
|
||||
panic("here")
|
||||
}
|
||||
|
||||
err := panicAndCatch()
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "panic in somewhere: here")
|
||||
|
||||
require.Contains(t, buf.String(), "caught panic: here")
|
||||
}
|
Loading…
Reference in New Issue
Block a user