-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainloop_test.go
236 lines (195 loc) · 6.25 KB
/
mainloop_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"bytes"
"errors"
"github.com/stretchr/testify/assert"
"strings"
"syscall"
"testing"
"time"
)
func TestMainLoop(t *testing.T) {
t.Run("ThreeSuccessIterations", func(t *testing.T) {
config := Config{
secondaryDekanatDbDSN: "dummy",
storageFile: "dummy",
pauseAfterSuccess: 0,
pauseAfterError: 0,
errorCountToBreak: 3,
}
wantExecutedCount := 3
functionExecutedCount := 0
executeIteration := func() error {
functionExecutedCount++
if functionExecutedCount >= wantExecutedCount {
return BreakLoopError
}
return nil
}
var out bytes.Buffer
err := runMainLoop(config, &out, executeIteration)
output := out.String()
assert.Contains(t, output, "iteration done success", "output not contains iteration done success")
assert.Equalf(
t, wantExecutedCount, functionExecutedCount,
"Iteration Function is not executed expected amount times: %q, want %q",
functionExecutedCount, wantExecutedCount,
)
assert.ErrorIsf(
t, err, BreakLoopError,
"Expected for BreakLoop, got %s.", err,
)
})
t.Run("ThreeErrorIterationsAndBreak", func(t *testing.T) {
config := Config{
secondaryDekanatDbDSN: "dummy",
storageFile: "dummy",
pauseAfterSuccess: 0,
pauseAfterError: 0,
errorCountToBreak: 3,
}
maxExecutedCount := 5
expectedExecutedCount := config.errorCountToBreak
functionExecutedCount := 0
executeIteration := func() error {
functionExecutedCount++
if functionExecutedCount >= maxExecutedCount {
return BreakLoopError
}
return errors.New("dummy error")
}
var out bytes.Buffer
err := runMainLoop(config, &out, executeIteration)
output := out.String()
assert.Equalf(
t, expectedExecutedCount, functionExecutedCount,
"Iteration Function is not executed expected amount times: %q, want %q",
functionExecutedCount, expectedExecutedCount,
)
assert.ErrorIs(
t, err, TooManyError,
"Expected forTooManyError, got %d.", err,
)
assert.Contains(t, output, "dummy error", "No dummy error in output")
dummyErrorCount := strings.Count(output, "dummy error")
assert.Equalf(
t, functionExecutedCount+1, dummyErrorCount,
"Not enough amount of dummy error in output. Expected: %d, actual: %d",
functionExecutedCount+1, dummyErrorCount,
)
assert.Contains(t, output, "Too many mistakes", "No Too many mistakes in output")
})
t.Run("PauseOnSuccess", func(t *testing.T) {
config := Config{
secondaryDekanatDbDSN: "dummy",
storageFile: "dummy",
pauseAfterSuccess: 100 * time.Microsecond,
pauseAfterError: 0,
errorCountToBreak: 3,
}
maxExecutedCount := 3
expectedExecutedCount := maxExecutedCount
functionExecutedCount := 0
executeIteration := func() error {
functionExecutedCount++
if functionExecutedCount >= maxExecutedCount {
return BreakLoopError
}
return nil
}
var out bytes.Buffer
start := time.Now()
err := runMainLoop(config, &out, executeIteration)
executionTime := time.Since(start)
assert.Equalf(
t, expectedExecutedCount, functionExecutedCount,
"Iteration Function is not executed expected amount times: %q, want %q",
functionExecutedCount, expectedExecutedCount,
)
assert.ErrorIs(t, err, BreakLoopError, "Expected for BreakLoopError, got %s.", err)
expectedExecutionTime := config.pauseAfterSuccess * time.Duration(maxExecutedCount-1)
assert.Greaterf(
t, executionTime, expectedExecutionTime,
"Success pause is not applied: Expect for execution greater that %s, actual exectution time: %s",
expectedExecutionTime, executionTime,
)
})
t.Run("PauseOnError", func(t *testing.T) {
config := Config{
secondaryDekanatDbDSN: "dummy",
storageFile: "dummy",
pauseAfterSuccess: 0,
pauseAfterError: 100 * time.Microsecond,
errorCountToBreak: 3,
}
maxExecutedCount := 3
expectedExecutedCount := maxExecutedCount
functionExecutedCount := 0
executeIteration := func() error {
functionExecutedCount++
if functionExecutedCount >= maxExecutedCount {
return BreakLoopError
}
return errors.New("dummy error")
}
var out bytes.Buffer
start := time.Now()
err := runMainLoop(config, &out, executeIteration)
executionTime := time.Since(start)
assert.Equalf(
t, expectedExecutedCount, functionExecutedCount,
"Iteration Function is not executed expected amount times: %q, want %q",
functionExecutedCount, expectedExecutedCount,
)
assert.ErrorIs(t, err, BreakLoopError, "Expected for ExitCodeLoopIsBroken, got %d.", err)
expectedExecutionTime := config.pauseAfterError * time.Duration(maxExecutedCount-1)
assert.Greaterf(
t, executionTime, expectedExecutionTime,
"Success pause is not applied: Expect for execution greater that %s, actual exectution time: %s",
expectedExecutionTime, executionTime,
)
})
t.Run("Sigterm", func(t *testing.T) {
config := Config{
secondaryDekanatDbDSN: "dummy",
storageFile: "dummy",
pauseAfterSuccess: 3 * time.Second,
pauseAfterError: 0,
errorCountToBreak: 3,
}
maxExecutedCount := 2
expectedExecutedCount := 1
functionExecutedCount := 0
executeIteration := func() error {
functionExecutedCount++
if functionExecutedCount >= maxExecutedCount {
return BreakLoopError
}
return nil
}
var out bytes.Buffer
go func() {
time.Sleep(time.Microsecond * 50)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
err := runMainLoop(config, &out, executeIteration)
assert.Equalf(
t, expectedExecutedCount, functionExecutedCount,
"Iteration Function is not executed expected amount times: %q, want %q",
functionExecutedCount, expectedExecutedCount,
)
assert.NoError(t, err, "Not expected error %s.", err)
assert.Contains(t, out.String(), "cancelled", "No `canceled` string in output")
})
}
func TestGetCurrentDatetime(t *testing.T) {
expected := time.Now()
expected = time.Date(
expected.Year(), expected.Month(), expected.Day(),
expected.Hour(), expected.Minute(), expected.Second(),
0, time.UTC,
)
actual, err := time.Parse("2006-01-02 15:04:05", getCurrentDatetime())
assert.NoErrorf(t, err, "Failed to parse string: %s", err)
assert.Equalf(t, expected, actual, "Expected current time %s, acutal %s", expected, actual)
}