-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_test.go
138 lines (118 loc) · 3.16 KB
/
loop_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
package webwatch
import (
"fmt"
"testing"
"time"
)
// ------------------------------------------
var _ Sleeper = &fakeSleeper{}
type fakeSleeper struct {
calls []string
}
func (fake *fakeSleeper) Sleep(duration time.Duration) {
call := fmt.Sprintf("Sleep(%s)", duration)
fake.calls = append(fake.calls, call)
}
func (fake *fakeSleeper) reset() {
fake.calls = []string{}
}
// ------------------------------------------
var _ Checker = &fakeChecker{}
type fakeChecker struct {
result CheckResult
}
func (fake *fakeChecker) Check() CheckResult {
return fake.result
}
// ------------------------------------------
var _ Mailer = &fakeMailer{}
type fakeMailer struct {
calls []string
}
func (fake *fakeMailer) SendRestarted(now time.Time) {
call := fmt.Sprintf("SendRestarted()")
fake.calls = append(fake.calls, call)
}
func (fake *fakeMailer) SendReport(now time.Time, ok bool, text string) {
call := fmt.Sprintf("SendReport(%t, %s)", ok, text)
fake.calls = append(fake.calls, call)
}
func (fake *fakeMailer) reset() {
fake.calls = []string{}
}
// ------------------------------------------
func TestLoop(t *testing.T) {
sleeper := &fakeSleeper{[]string{}}
checker := &fakeChecker{CheckResult{true, "ok"}}
mailer := &fakeMailer{[]string{}}
loop := NewLoop(
sleeper,
checker,
mailer,
mustParseDuration("5m"), // checks,
mustParseDuration("12h"), // reports,
mustParseDuration("1h"), // limit
)
now := time.Date(2017, 1, 1, 10, 0, 0, 0, time.UTC)
// first tick
// -> send report
// -> sleep limit
loop.Tick(now)
assertEqInt(t, 1, len(mailer.calls))
assertEqStr(t, "SendReport(true, ok)", mailer.calls[0])
mailer.reset()
assertEqInt(t, 1, len(sleeper.calls))
assertEqStr(t, "Sleep(1h0m0s)", sleeper.calls[0])
sleeper.reset()
// after 1h: new check
// -> no report
// -> sleep checks (5m)
now = now.Add(1 * time.Hour)
loop.Tick(now)
assertEqInt(t, 0, len(mailer.calls))
mailer.reset()
assertEqInt(t, 1, len(sleeper.calls))
assertEqStr(t, "Sleep(5m0s)", sleeper.calls[0])
sleeper.reset()
// after 5m: new check, url error
// -> send report
// -> sleep limit (1h)
now = now.Add(5 * time.Minute)
checker.result = CheckResult{false, "err"}
loop.Tick(now)
assertEqInt(t, 1, len(mailer.calls))
assertEqStr(t, "SendReport(false, err)", mailer.calls[0])
mailer.reset()
assertEqInt(t, 1, len(sleeper.calls))
assertEqStr(t, "Sleep(1h0m0s)", sleeper.calls[0])
sleeper.reset()
// after 1h: new check
// -> no report
// -> sleep checks (5m)
now = now.Add(1 * time.Hour)
loop.Tick(now)
assertEqInt(t, 0, len(mailer.calls))
mailer.reset()
assertEqInt(t, 1, len(sleeper.calls))
assertEqStr(t, "Sleep(5m0s)", sleeper.calls[0])
sleeper.reset()
// after 12h: new check
// -> report after 12h
// -> sleep checks (5m)
now = now.Add(12 * time.Hour)
loop.Tick(now)
assertEqInt(t, 1, len(mailer.calls))
assertEqStr(t, "SendReport(false, err)", mailer.calls[0])
mailer.reset()
assertEqInt(t, 1, len(sleeper.calls))
assertEqStr(t, "Sleep(1h0m0s)", sleeper.calls[0])
sleeper.reset()
}
// ------------------------------------------
func mustParseDuration(s string) time.Duration {
d, err := time.ParseDuration(s)
if err != nil {
panic(err)
}
return d
}