forked from turnage/graw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman_test.go
98 lines (79 loc) · 1.84 KB
/
foreman_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
package graw
import (
"fmt"
"io/ioutil"
"log"
"testing"
"time"
"github.com/turnage/graw/reddit"
)
type mockBot struct {
err error
setUpCalled bool
tearDownCalled bool
}
func (m *mockBot) SetUp() error {
m.setUpCalled = true
return m.err
}
func (m *mockBot) TearDown() {
m.tearDownCalled = true
}
func TestForemanControls(t *testing.T) {
b := &mockBot{}
result, stop := testForeman(b, nil, t)
stop()
waitForForeman(result, nil, t)
if !b.setUpCalled {
t.Errorf("SetUp() was not called on bot")
}
if !b.tearDownCalled {
t.Errorf("TearDown() was not called on bot")
}
}
func TestForemanError(t *testing.T) {
errs := make(chan error)
result, _ := testForeman(nil, errs, t)
// send errors that should be ignored and then a unique error to make
// sure only the unique error killed the foreman (verified by checking
// that it is the one that comes through the result channel, since the
// errors are read chronologically).
uniqueError := fmt.Errorf("an error")
go func() {
errs <- nil
errs <- reddit.BusyErr
errs <- reddit.GatewayErr
errs <- reddit.GatewayTimeoutErr
errs <- uniqueError
}()
waitForForeman(result, uniqueError, t)
}
func testForeman(handler interface{}, errs chan error, t *testing.T) (
<-chan error,
func(),
) {
kill := make(chan bool)
result := make(chan error)
logger := log.New(ioutil.Discard, "", 0)
if errs == nil {
errs = make(chan error)
}
stop, wait, err := launch(handler, kill, errs, logger)
if err != nil {
t.Fatalf("error launching the foreman: %v", err)
}
go func() {
result <- wait()
}()
return result, stop
}
func waitForForeman(result <-chan error, expected error, t *testing.T) {
select {
case err := <-result:
if err != expected {
t.Errorf("error from foreman run: %v", err)
}
case <-time.After(time.Second):
t.Errorf("foreman did not stop()")
}
}