1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/common/task/task_test.go

64 lines
1.2 KiB
Go
Raw Normal View History

2018-05-27 07:02:29 -04:00
package task_test
import (
"context"
"errors"
"testing"
"time"
2018-11-14 15:00:51 -05:00
"v2ray.com/core/common"
2018-05-27 07:02:29 -04:00
. "v2ray.com/core/common/task"
. "v2ray.com/ext/assert"
)
func TestExecuteParallel(t *testing.T) {
assert := With(t)
2018-12-06 05:35:02 -05:00
err := Run(context.Background(),
func() error {
time.Sleep(time.Millisecond * 200)
return errors.New("test")
}, func() error {
time.Sleep(time.Millisecond * 500)
return errors.New("test2")
})
2018-05-27 07:02:29 -04:00
assert(err.Error(), Equals, "test")
}
func TestExecuteParallelContextCancel(t *testing.T) {
assert := With(t)
ctx, cancel := context.WithCancel(context.Background())
2018-12-06 05:35:02 -05:00
err := Run(ctx, func() error {
2018-05-27 07:02:29 -04:00
time.Sleep(time.Millisecond * 2000)
return errors.New("test")
}, func() error {
time.Sleep(time.Millisecond * 5000)
return errors.New("test2")
}, func() error {
cancel()
return nil
2018-12-06 05:35:02 -05:00
})
2018-05-27 07:02:29 -04:00
assert(err.Error(), HasSubstring, "canceled")
}
2018-11-14 15:00:51 -05:00
func BenchmarkExecuteOne(b *testing.B) {
noop := func() error {
return nil
}
for i := 0; i < b.N; i++ {
2018-12-06 05:35:02 -05:00
common.Must(Run(context.Background(), noop))
2018-11-14 15:00:51 -05:00
}
}
func BenchmarkExecuteTwo(b *testing.B) {
noop := func() error {
return nil
}
for i := 0; i < b.N; i++ {
2018-12-06 05:35:02 -05:00
common.Must(Run(context.Background(), noop, noop))
2018-11-14 15:00:51 -05:00
}
}