forked from turnage/graw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman_test.go
92 lines (74 loc) · 1.76 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
package graw
import (
"fmt"
"io"
"log"
"testing"
"time"
"github.com/mix/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{}
errs := make(chan error)
kill := make(chan bool)
testLogger := log.New(io.Discard, "", 0)
stop, _, err := launch(b, kill, errs, testLogger)
if err != nil {
t.Fatalf("error launching the foreman: %v", err)
}
stop()
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)
kill := make(chan bool)
result := make(chan error)
testLogger := log.New(io.Discard, "", 0)
_, wait, err := launch(nil, kill, errs, testLogger)
if err != nil {
t.Fatalf("error launching the foreman: %v", err)
}
go func() {
result <- wait()
}()
// 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 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()")
}
}