v2ray-core/common/task/task_test.go

64 lines
1.2 KiB
Go
Raw Normal View History

2018-05-27 11:02:29 +00:00
package task_test
import (
"context"
"errors"
"testing"
"time"
2018-11-14 20:00:51 +00:00
"v2ray.com/core/common"
2018-05-27 11:02:29 +00:00
. "v2ray.com/core/common/task"
. "v2ray.com/ext/assert"
)
func TestExecuteParallel(t *testing.T) {
assert := With(t)
2018-12-06 10:35:02 +00: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 11:02:29 +00:00
assert(err.Error(), Equals, "test")
}
func TestExecuteParallelContextCancel(t *testing.T) {
assert := With(t)
ctx, cancel := context.WithCancel(context.Background())
2018-12-06 10:35:02 +00:00
err := Run(ctx, func() error {
2018-05-27 11:02:29 +00: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 10:35:02 +00:00
})
2018-05-27 11:02:29 +00:00
assert(err.Error(), HasSubstring, "canceled")
}
2018-11-14 20:00:51 +00:00
func BenchmarkExecuteOne(b *testing.B) {
noop := func() error {
return nil
}
for i := 0; i < b.N; i++ {
2018-12-06 10:35:02 +00:00
common.Must(Run(context.Background(), noop))
2018-11-14 20:00:51 +00:00
}
}
func BenchmarkExecuteTwo(b *testing.B) {
noop := func() error {
return nil
}
for i := 0; i < b.N; i++ {
2018-12-06 10:35:02 +00:00
common.Must(Run(context.Background(), noop, noop))
2018-11-14 20:00:51 +00:00
}
}