1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 02:35:23 +00:00
v2fly/common/task/task_test.go

67 lines
1.3 KiB
Go
Raw Normal View History

2018-05-27 11:02:29 +00:00
package task_test
import (
"context"
"errors"
2019-02-02 21:19:30 +00:00
"strings"
2018-05-27 11:02:29 +00:00
"testing"
"time"
2019-02-02 21:19:30 +00:00
"github.com/google/go-cmp/cmp"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
. "github.com/v2fly/v2ray-core/v4/common/task"
2018-05-27 11:02:29 +00:00
)
func TestExecuteParallel(t *testing.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
2019-02-02 21:19:30 +00:00
if r := cmp.Diff(err.Error(), "test"); r != "" {
t.Error(r)
}
2018-05-27 11:02:29 +00:00
}
func TestExecuteParallelContextCancel(t *testing.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
2019-02-02 21:19:30 +00:00
errStr := err.Error()
if !strings.Contains(errStr, "canceled") {
t.Error("expected error string to contain 'canceled', but actually not: ", errStr)
}
2018-05-27 11:02:29 +00:00
}
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
}
}