recover_test.go 405 B

12345678910111213141516171819202122232425262728
  1. package rescue
  2. import (
  3. "sync/atomic"
  4. "testing"
  5. "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. }