Add RunSafe with context (#3224)
Co-authored-by: yangtao <mrynag8614@163.com> Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package threading
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
func TestRoutineId(t *testing.T) {
|
||||
@@ -34,3 +37,51 @@ func TestRunSafe(t *testing.T) {
|
||||
<-ch
|
||||
i++
|
||||
}
|
||||
|
||||
func TestRunSafeCtx(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
logx.SetWriter(logx.NewWriter(&buf))
|
||||
ctx := context.Background()
|
||||
ch := make(chan lang.PlaceholderType)
|
||||
|
||||
i := 0
|
||||
|
||||
defer func() {
|
||||
assert.Equal(t, 1, i)
|
||||
}()
|
||||
|
||||
go RunSafeCtx(ctx, func() {
|
||||
defer func() {
|
||||
ch <- lang.Placeholder
|
||||
}()
|
||||
|
||||
panic("panic")
|
||||
})
|
||||
|
||||
<-ch
|
||||
i++
|
||||
}
|
||||
|
||||
func TestGoSafeCtx(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
logx.SetWriter(logx.NewWriter(&buf))
|
||||
ctx := context.Background()
|
||||
ch := make(chan lang.PlaceholderType)
|
||||
|
||||
i := 0
|
||||
|
||||
defer func() {
|
||||
assert.Equal(t, 1, i)
|
||||
}()
|
||||
|
||||
GoSafeCtx(ctx, func() {
|
||||
defer func() {
|
||||
ch <- lang.Placeholder
|
||||
}()
|
||||
|
||||
panic("panic")
|
||||
})
|
||||
|
||||
<-ch
|
||||
i++
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user