/
simple_test.go
90 lines (74 loc) · 1.79 KB
/
simple_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
package workers
import (
"errors"
"testing"
"time"
"github.com/sirupsen/logrus"
)
type simpleTestConfig struct {
errOnStart error
opts *SimpleOptions
mustBeForced bool
iterations int
}
func TestSimpleWorkerByTimeout(t *testing.T) {
test := &simpleTestConfig{
opts: &SimpleOptions{
SleepTimeout: 2 * time.Second,
},
}
testSimpleWorker(test, t)
}
func TestSimpleWorkerByTimeoutIteratively(t *testing.T) {
test := &simpleTestConfig{
opts: &SimpleOptions{
SleepTimeout: 1 * time.Second,
},
iterations: 3,
}
testSimpleWorker(test, t)
}
func TestSimpleWorkerOnLoad(t *testing.T) {
test := &simpleTestConfig{
opts: &SimpleOptions{
SleepTimeout: 2 * time.Second,
RunOnLoad: true,
},
}
testSimpleWorker(test, t)
}
func TestSimpleWorkerForced(t *testing.T) {
test := &simpleTestConfig{
opts: &SimpleOptions{
SleepTimeout: 2 * time.Hour,
RunOnLoad: false,
},
mustBeForced: true,
}
testSimpleWorker(test, t)
}
func TestSimpleWorkerStartError(t *testing.T) {
test := &simpleTestConfig{
opts: &SimpleOptions{
SleepTimeout: 2 * time.Second,
RunOnLoad: false,
},
errOnStart: errors.New("some start error"),
}
testSimpleWorker(test, t)
}
func testSimpleWorker(test *simpleTestConfig, t *testing.T) {
buf := 3 //старт, runner и стоп
ast := newWorkerTester(buf, test.errOnStart)
logger := logrus.StandardLogger()
logger.SetLevel(logrus.DebugLevel)
w := newSimpleWorker(logger, ast, test.opts)
if test.iterations <= 1 {
testWorker(t, w, ast, 1, test.mustBeForced, test.opts.RunOnLoad, test.opts.SleepTimeout, test.errOnStart)
return
}
for i := 0; i < test.iterations; i++ {
t.Logf("Running iteration %d", i+1)
testWorker(t, w, ast, 1, test.mustBeForced, test.opts.RunOnLoad, test.opts.SleepTimeout, test.errOnStart)
}
}