123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package threading
- import (
- "bytes"
- "context"
- "io"
- "log"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/wuntsong-org/go-zero-plus/core/lang"
- "github.com/wuntsong-org/go-zero-plus/core/logx"
- )
- func TestRoutineId(t *testing.T) {
- assert.True(t, RoutineId() > 0)
- }
- func TestRunSafe(t *testing.T) {
- log.SetOutput(io.Discard)
- 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++
- }
|