You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/threading/routines_test.go

88 lines
1.2 KiB
Go

4 years ago
package threading
import (
"bytes"
"context"
"io"
4 years ago
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/logx"
4 years ago
)
func TestRoutineId(t *testing.T) {
assert.True(t, RoutineId() > 0)
}
func TestRunSafe(t *testing.T) {
log.SetOutput(io.Discard)
4 years ago
i := 0
defer func() {
assert.Equal(t, 1, i)
}()
ch := make(chan lang.PlaceholderType)
go RunSafe(func() {
defer func() {
ch <- lang.Placeholder
}()
panic("panic")
})
<-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++
}