hi-server/pkg/rescue/recover_test.go
2025-10-10 07:13:36 -07:00

41 lines
670 B
Go

package rescue
import (
"context"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func init() {
}
func TestRescue(t *testing.T) {
var count int32
assert.NotPanics(t, func() {
defer Recover(func() {
atomic.AddInt32(&count, 2)
}, func() {
atomic.AddInt32(&count, 3)
})
panic("hello")
})
assert.Equal(t, int32(5), atomic.LoadInt32(&count))
}
func TestRescueCtx(t *testing.T) {
var count int32
assert.NotPanics(t, func() {
defer RecoverCtx(context.Background(), func() {
atomic.AddInt32(&count, 2)
}, func() {
atomic.AddInt32(&count, 3)
})
panic("hello")
})
assert.Equal(t, int32(5), atomic.LoadInt32(&count))
}