-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
187 lines (153 loc) · 3.87 KB
/
service_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package flex_test
import (
"context"
"errors"
"strings"
"testing"
"time"
"github.com/go-flexible/flex"
)
const unnamed = "un-named"
type mockWorker struct {
name string
t *testing.T
}
func (m *mockWorker) Run(context.Context) error {
if m.name == "" {
m.name = unnamed
}
m.t.Logf("mock worker (%s) running", m.name)
return nil
}
func (m *mockWorker) Halt(context.Context) error {
if m.name == "" {
m.name = unnamed
}
m.t.Logf("mock worker (%s) halting", m.name)
return nil
}
type failingMockWorker struct{ mockWorker }
func (f *failingMockWorker) Run(context.Context) error {
if f.name == "" {
f.name = unnamed
}
f.t.Logf("mock worker (%s) failing to run", f.name)
return errors.New("run failed")
}
func defaultCtx() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 2*time.Second)
}
func TestStart(t *testing.T) {
t.Run("nil worker must not panic", func(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r != nil {
t.Error("TestStart must not panic")
}
}()
ctx, cancel := defaultCtx()
defer cancel()
err := flex.Start(ctx, nil)
if err == nil {
t.Error("expected an error but did not get one")
}
})
t.Run("zero workers must return an error", func(t *testing.T) {
t.Parallel()
ctx, cancel := defaultCtx()
defer cancel()
err := flex.Start(ctx)
if err == nil {
t.Error("expected an error but did not get one")
}
})
t.Run("one worker must run and halt successfully", func(t *testing.T) {
t.Parallel()
ctx, cancel := defaultCtx()
defer cancel()
err := flex.Start(ctx, &mockWorker{t: t, name: "foo"})
if err != nil {
t.Error(err)
}
})
t.Run("multiple workers must run and halt successfully", func(t *testing.T) {
t.Parallel()
ctx, cancel := defaultCtx()
defer cancel()
workers := []flex.Worker{
&mockWorker{t: t, name: "foo"},
&mockWorker{t: t, name: "bar"},
&mockWorker{t: t, name: "baz"},
}
err := flex.Start(ctx, workers...)
if err != nil {
t.Error(err)
}
})
t.Run("one worker failing to run must cancel and error", func(t *testing.T) {
t.Parallel()
ctx, cancel := defaultCtx()
defer cancel()
workers := []flex.Worker{
&failingMockWorker{mockWorker{t: t, name: "foo"}},
}
err := flex.Start(ctx, workers...)
if err == nil {
t.Error("expected an error but did not get one")
}
if _, ok := err.(flex.MultiError); !ok {
t.Errorf("expected an error of type %T, but got: %T", flex.MultiError{}, err)
}
})
t.Run("one of multiple workers failing to run must cancel", func(t *testing.T) {
t.Parallel()
ctx, cancel := defaultCtx()
defer cancel()
workers := []flex.Worker{
&mockWorker{t: t, name: "foo"},
&failingMockWorker{mockWorker{t: t, name: "bar"}},
&mockWorker{t: t, name: "baz"},
}
err := flex.Start(ctx, workers...)
if err == nil {
t.Error("expected an error but did not get one")
}
if _, ok := err.(flex.MultiError); !ok {
t.Errorf("expected an error of type %T, but got: %T", flex.MultiError{}, err)
}
})
}
func TestMultiError(t *testing.T) {
t.Run("nil must not panic", func(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r != nil {
t.Error("TestMultiError must not panic")
}
}()
err := flex.MultiError{}
_ = err.Error()
})
t.Run("one error must contain the error", func(t *testing.T) {
t.Parallel()
errs := []error{
errors.New("foo"),
}
err := flex.MultiError{errs}
if err.Error() != errs[0].Error() {
t.Errorf("expected %q but got %q", errs[0].Error(), err.Error())
}
})
t.Run("multiple errors must display first error", func(t *testing.T) {
t.Parallel()
errs := []error{
errors.New("foo"),
errors.New("bar"),
errors.New("baz"),
}
err := flex.MultiError{errs}
if !strings.Contains(err.Error(), errs[0].Error()) {
t.Errorf("expected error to contain %q, but got %q", errs[0].Error(), err.Error())
}
})
}