2020-07-26 17:09:05 +08:00
|
|
|
package threading
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-11-30 21:44:53 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/logx/logtest"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRoutineGroupRun(t *testing.T) {
|
|
|
|
|
var count int32
|
|
|
|
|
group := NewRoutineGroup()
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
|
group.Run(func() {
|
|
|
|
|
atomic.AddInt32(&count, 1)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.Wait()
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, int32(3), count)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRoutingGroupRunSafe(t *testing.T) {
|
2025-11-30 21:44:53 +08:00
|
|
|
logtest.Discard(t)
|
2025-12-07 11:37:56 +08:00
|
|
|
|
2020-07-26 17:09:05 +08:00
|
|
|
var count int32
|
|
|
|
|
group := NewRoutineGroup()
|
|
|
|
|
var once sync.Once
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
|
group.RunSafe(func() {
|
|
|
|
|
once.Do(func() {
|
|
|
|
|
panic("")
|
|
|
|
|
})
|
|
|
|
|
atomic.AddInt32(&count, 1)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.Wait()
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, int32(2), count)
|
|
|
|
|
}
|