task_test.go
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package task
import (
"context"
"errors"
"github.com/google/go-cmp/cmp"
"log"
"strings"
"testing"
"time"
)
func Test_OnSuccess(t *testing.T){
work :=func()error{
log.Println("do work in")
return errors.New("do work error")
}
afterwork:= func()error{
log.Println("after work")
return nil
}
f :=OnSuccess(work,afterwork)
err := f()
if err!=nil{
log.Println(err)
}
}
func Test_ExecuteParallel(t *testing.T){
err :=Run(context.Background(),
func() error {
time.Sleep(time.Microsecond*300)
return errors.New("T1")
},
func()error{
time.Sleep(time.Microsecond*500)
return errors.New("T2")
})
if r:=cmp.Diff(err.Error(),"T1");r!=""{
t.Error(r)
}
}
func Test_ExecuteParallelContextCancel(t *testing.T){
ctx,cancel :=context.WithCancel(context.Background())
err :=Run(ctx,
func() error {
time.Sleep(time.Microsecond*3000)
return errors.New("T1")
},
func()error{
time.Sleep(time.Microsecond*5000)
return errors.New("T2")
},
func()error{
time.Sleep(time.Microsecond*1000)
cancel()
return nil
})
errStr := err.Error()
if strings.Contains(errStr, "canceled") {
t.Error("expected error string to contain 'canceled', but actually not: ", errStr)
}
}
func BenchmarkExecuteOne(b *testing.B){
noop:=func()error{
return nil
}
for i:=0;i<b.N;i++{
Run(context.Background(),noop)
}
}
func BenchmarkExecuteTwo(b *testing.B){
noop:=func()error{
return nil
}
for i:=0;i<b.N;i++{
Run(context.Background(),noop,noop)
}
}