go-zero/core/fx/parallel_test.go

77 lines
1.4 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package fx
import (
2024-04-29 00:18:30 +08:00
"errors"
2020-07-26 17:09:05 +08:00
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParallel(t *testing.T) {
var count int32
Parallel(func() {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 1)
}, func() {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 2)
}, func() {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 3)
})
assert.Equal(t, int32(6), count)
}
2024-04-29 00:18:30 +08:00
func TestParallelErr(t *testing.T) {
var count int32
err := ParallelErr(
func() error {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 1)
return errors.New("failed to exec #1")
},
func() error {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 2)
return errors.New("failed to exec #2")
},
func() error {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 3)
return nil
},
)
assert.Equal(t, int32(6), count)
assert.Error(t, err)
assert.ErrorContains(t, err, "failed to exec #1", "failed to exec #2")
}
func TestParallelErrErrorNil(t *testing.T) {
var count int32
err := ParallelErr(
func() error {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 1)
return nil
},
func() error {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 2)
return nil
},
func() error {
time.Sleep(time.Millisecond * 100)
atomic.AddInt32(&count, 3)
return nil
},
)
assert.Equal(t, int32(6), count)
assert.NoError(t, err)
}