mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: add more tests (#3229)
This commit is contained in:
parent
851a72f1cc
commit
93124329ac
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logc"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,12 +21,13 @@ func Recover(cleanups ...func()) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RecoverCtx is used with defer to do cleanup on panics.
|
||||||
func RecoverCtx(ctx context.Context, cleanups ...func()) {
|
func RecoverCtx(ctx context.Context, cleanups ...func()) {
|
||||||
for _, cleanup := range cleanups {
|
for _, cleanup := range cleanups {
|
||||||
cleanup()
|
cleanup()
|
||||||
}
|
}
|
||||||
|
|
||||||
if p := recover(); p != nil {
|
if p := recover(); p != nil {
|
||||||
logc.Errorf(ctx, "%+v\n\n%s", p, debug.Stack())
|
logx.WithContext(ctx).Errorf("%+v\n%s", p, debug.Stack())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package rescue
|
package rescue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -25,3 +26,17 @@ func TestRescue(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Equal(t, int32(5), atomic.LoadInt32(&count))
|
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))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user