recover_test.go 424 B

123456789101112131415161718192021222324252627
  1. package rescue
  2. import (
  3. "sync/atomic"
  4. "testing"
  5. "git.i2edu.net/i2/go-zero/core/logx"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func init() {
  9. logx.Disable()
  10. }
  11. func TestRescue(t *testing.T) {
  12. var count int32
  13. assert.NotPanics(t, func() {
  14. defer Recover(func() {
  15. atomic.AddInt32(&count, 2)
  16. }, func() {
  17. atomic.AddInt32(&count, 3)
  18. })
  19. panic("hello")
  20. })
  21. assert.Equal(t, int32(5), atomic.LoadInt32(&count))
  22. }