-
Notifications
You must be signed in to change notification settings - Fork 17
/
async_test.go
131 lines (107 loc) · 2.28 KB
/
async_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package async
import (
"errors"
"fmt"
"runtime"
"testing"
"time"
)
func fib(p, c int) (int, int) {
return c, p + c
}
func TestAsync(t *testing.T) {
var (
e error
multiRes Results
res []interface{}
keyRes = "two"
)
fmt.Println("Testing `Waterfall`")
res, e = Waterfall(Tasks{
fib, fib, fib,
func(p, c int) int {
return c
},
}, 0, 1)
if e != nil {
t.Errorf("Error executing a Waterfall (%s)", e.Error())
}
fmt.Println("Waterfall result :", res[0].(int))
fmt.Printf("\nTesting `Parallel` with `runtime.GOMAXPROCS(2)`\n")
runtime.GOMAXPROCS(2)
multiRes, e = Parallel(MapTasks{
"one": func() error {
for i := 'a'; i < 'a'+26; i++ {
fmt.Printf("%c ", i)
}
return nil //fmt.Errorf("Error in one function")
},
"two": func() (int, string, error) {
time.Sleep(2 * time.Microsecond)
for i := 0; i < 27; i++ {
fmt.Printf("%d ", i)
}
return 2, "test", nil
},
"three": func() int {
for i := 'z'; i >= 'a'; i-- {
fmt.Printf("%c ", i)
}
return 3
},
})
if e != nil {
t.Errorf("Error executing a Parallel (%s)", e.Error())
}
fmt.Printf("Parallel Result key %s: %+v\n", keyRes, multiRes.Key(keyRes))
runtime.GOMAXPROCS(runtime.NumCPU())
fmt.Printf("\nTesting `Concurrent`\n")
multiRes, e = Concurrent(Tasks{
func() int {
for i := 'a'; i < 'a'+26; i++ {
fmt.Printf("%c ", i)
}
return 1
},
func() bool {
time.Sleep(3 * time.Microsecond)
for i := 0; i < 27; i++ {
fmt.Printf("%d ", i)
}
return false
},
func() {
for i := 'z'; i >= 'a'; i-- {
fmt.Printf("%c ", i)
}
},
})
fmt.Println("Concurrent Result Index: 1", multiRes.Index(1))
if e != nil {
t.Errorf("Error executing a Concurrent (%s)", e.Error())
}
}
func TestAsyncError(t *testing.T) {
fmt.Printf("\nTesting `Waterfall` with error\n")
res, e := Waterfall(Tasks{
func() (int, error) {
return 1, nil
},
func(n int) error {
fmt.Printf("if %d > 0 then error\n", n)
if n > 0 {
return errors.New("Error on second function")
}
return nil
},
func() error {
fmt.Println("Function never reached")
return nil
},
})
if e != nil {
fmt.Printf("Error executing a Waterfall (%q)\n", e)
}
// should be empty
fmt.Printf("Waterfall result with error should be empty: %+v\n", res)
}