-
Notifications
You must be signed in to change notification settings - Fork 1
/
coordinator_inner_test.go
134 lines (123 loc) · 2.54 KB
/
coordinator_inner_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
132
133
134
package choreograph
import (
"context"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var _ ProcessExecutioner = (*Coordinator)(nil)
func TestCoordinator_AddStep(t *testing.T) {
simpleJobOrPreCheckFunc := func(ctx context.Context) error { return nil }
tests := []struct {
name string
steps Steps
wantErr bool
}{
{
name: "add one step",
steps: Steps{
{
Name: "first",
Job: simpleJobOrPreCheckFunc,
PreCheck: simpleJobOrPreCheckFunc,
},
},
wantErr: false,
},
{
name: "add several steps",
steps: Steps{
{
Name: "first",
Job: simpleJobOrPreCheckFunc,
PreCheck: simpleJobOrPreCheckFunc,
},
{
Name: "second",
Job: simpleJobOrPreCheckFunc,
PreCheck: simpleJobOrPreCheckFunc,
},
{
Name: "third",
Job: simpleJobOrPreCheckFunc,
PreCheck: simpleJobOrPreCheckFunc,
},
},
wantErr: false,
},
{
name: "error when adding step",
steps: Steps{
{
Name: "first",
Job: simpleJobOrPreCheckFunc,
PreCheck: func() {},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewCoordinator()
for _, s := range tt.steps {
err := c.AddStep(s)
if tt.wantErr {
require.Error(t, err)
return
} else {
assert.NoError(t, err)
}
}
for wIdx := range c.workers {
if assert.Len(t, c.workers[wIdx].steps, len(tt.steps), "incorrect length of steps") {
assert.Equal(t, tt.steps, c.workers[wIdx].steps)
}
}
})
}
}
func TestNewCoordinator(t *testing.T) {
type args struct {
options []Option
}
tests := []struct {
name string
args args
want *Coordinator
}{
{
name: "successfully created",
args: args{},
want: &Coordinator{
workerCount: runtime.NumCPU(),
},
},
{
name: "successfully created with worker count as option",
args: args{
options: []Option{WithWorkerCount(10)},
},
want: &Coordinator{
workerCount: 10,
},
},
{
name: "successfully created with worker count lower than 1",
args: args{
options: []Option{WithWorkerCount(0)},
},
want: &Coordinator{
workerCount: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewCoordinator(tt.args.options...)
// Check workers count
assert.Equalf(t, tt.want.workerCount, got.workerCount, "expected count of workers is '%d', got '%d'", tt.want.workerCount, got.workerCount)
})
}
}