-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaction_await_test.go
executable file
·90 lines (74 loc) · 1.98 KB
/
action_await_test.go
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
83
84
85
86
87
88
89
90
//go:build !race
package co_test
import (
"fmt"
"runtime"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
"go.tempura.ink/co"
)
func TestAwaitAll(t *testing.T) {
convey.Convey("given a sequential tasks", t, func() {
handlers := make([]func() (int, error), 0)
for i := 0; i < 1000; i++ {
i := i
handlers = append(handlers, func() (int, error) {
return i + 1, nil
})
}
convey.Convey("Run AwaitAll", func() {
responses := co.AwaitAll(handlers...)
convey.Convey("The responded value should be valid", func() {
expected, actuals := []int{}, []int{}
for i := 0; i < 1000; i++ {
expected = append(expected, i+1)
actuals = append(actuals, responses[i].GetValue())
}
convey.So(expected, convey.ShouldResemble, actuals)
})
})
})
}
func TestAwaitRace(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
convey.Convey("given a sequential tasks", t, func() {
handlers := make([]func() (int, error), 0)
for i := 0; i < 100; i++ {
i := i
handlers = append(handlers, func() (int, error) {
time.Sleep(time.Second * time.Duration(i+1))
return i + 1, nil
})
}
convey.Convey("Run AwaitAll", func() {
responses := co.AwaitRace(handlers...)
convey.Convey("The responded value should be valid", func() {
convey.So(responses, convey.ShouldEqual, 1)
})
})
})
}
func TestAwaitAny(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
convey.Convey("given a sequential tasks", t, func() {
handlers := make([]func() (int, error), 0)
for i := 0; i < 100; i++ {
i := i
err := fmt.Errorf("Determined value")
if i > 3 {
err = nil
}
handlers = append(handlers, func() (int, error) {
time.Sleep(time.Second * time.Duration(i+1))
return i + 1, err
})
}
convey.Convey("Run AwaitAll", func() {
responses := co.AwaitAny(handlers...)
convey.Convey("The responded value should be valid", func() {
convey.So(responses.GetValue(), convey.ShouldEqual, 5)
})
})
})
}