-
Notifications
You must be signed in to change notification settings - Fork 0
/
conman_test.go
104 lines (82 loc) · 1.96 KB
/
conman_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
// Author: Ilyess Bachiri
// Copyright (c) 2021-present Ilyess Bachiri
package conman
import (
"fmt"
"testing"
"time"
)
type doubler struct {
operand int
}
func (d *doubler) Execute() (interface{}, error) {
return d.operand * 2, nil
}
type errdoubler struct {
operand int
}
func (d *errdoubler) Execute() (interface{}, error) {
return nil, fmt.Errorf("Error calculating for %v", d.operand)
}
type slowdoubler struct {
operand int
}
func (d *slowdoubler) Execute() (interface{}, error) {
time.Sleep(time.Second)
return d.operand * 2, nil
}
func TestCaptureOutputs(t *testing.T) {
cm := New(5)
cm.Run(&doubler{operand: 299})
cm.Run(&doubler{operand: 532})
cm.Run(&doubler{operand: 203})
cm.Wait()
for _, o := range []int{598, 1064, 406} {
if !contains(cm.Outputs(), o) {
t.Errorf("Expected output %v is not part of the captured outputs", o)
}
}
}
func TestCaptureErrors(t *testing.T) {
cm := New(5)
cm.Run(&errdoubler{operand: 299})
cm.Run(&errdoubler{operand: 532})
cm.Run(&errdoubler{operand: 203})
cm.Wait()
for _, i := range []int{299, 532, 203} {
if !containsError(cm.Errors(), fmt.Errorf("Error calculating for %v", i)) {
t.Errorf("Expected error for %v but none was found", i)
}
}
}
func TestConcurrencyLimit(t *testing.T) {
cm := New(2)
cm.Run(&slowdoubler{operand: 299})
cm.Run(&slowdoubler{operand: 532})
cm.Run(&slowdoubler{operand: 203})
// Wait to make sure the first two tasks have completed
time.Sleep(100 * time.Millisecond)
if contains(cm.Outputs(), 406) {
t.Errorf("Didn't expect task for 406 to have been executed")
}
cm.Wait()
if !contains(cm.Outputs(), 406) {
t.Errorf("Expected task for 406 to have been exectued")
}
}
func containsError(items []error, item error) bool {
for _, i := range items {
if i.Error() == item.Error() {
return true
}
}
return false
}
func contains(items []interface{}, item interface{}) bool {
for _, i := range items {
if i == item {
return true
}
}
return false
}