-
Notifications
You must be signed in to change notification settings - Fork 7
/
mux_test.go
113 lines (101 loc) · 2.3 KB
/
mux_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
package workers
import (
"testing"
)
// WorkMux is defined and can be instanciated
func TestNewWorkMux(t *testing.T) {
var mux *WorkMux
mux = NewWorkMux()
if mux == nil {
t.Fail()
}
}
func TestHandleInvalidTube(t *testing.T) {
defer func() {
if err := recover(); err == nil || err != "invalid tube" {
t.Fail()
}
}()
mux := NewWorkMux()
mux.Handle("", HandlerFunc(func(*Job) {}))
}
func TestHandleNilHandler(t *testing.T) {
defer func() {
if err := recover(); err == nil || err != "nil handler" {
t.Fail()
}
}()
mux := NewWorkMux()
mux.Handle("tube1", nil)
}
func TestHandleDuplicate(t *testing.T) {
defer func() {
if err := recover(); err == nil || err != "multiple registrations for tube1" {
t.Fail()
}
}()
mux := NewWorkMux()
mux.Handle("tube1", HandlerFunc(func(*Job) {}))
mux.Handle("tube1", HandlerFunc(func(*Job) {}))
}
// Checks for unknown registered handler
func TestHandlerUnknown(t *testing.T) {
mux := NewWorkMux()
if mux.Handler("tube1") != nil {
t.Fail()
}
}
// Checks for a known registered handler
func TestHandlerRegistered(t *testing.T) {
mux := NewWorkMux()
handled := false
mux.Handle("tube1", HandlerFunc(func(*Job) { handled = true }))
mux.Handler("tube1").Work(nil)
if !handled {
t.Fail()
}
}
func TestEmptyTubes(t *testing.T) {
mux := NewWorkMux()
if len(mux.Tubes()) != 0 {
t.Fail()
}
}
func TestTubes(t *testing.T) {
contains := func(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
mux := NewWorkMux()
mux.Handle("tube1", HandlerFunc(func(*Job) {}))
mux.Handle("tube2", HandlerFunc(func(*Job) {}))
mux.Handle("tubeN", HandlerFunc(func(*Job) {}))
tubes := mux.Tubes()
if len(tubes) != 3 || !contains(tubes, "tube1") || !contains(tubes, "tube2") || !contains(tubes, "tubeN") {
t.Fail()
}
}
func TestWork(t *testing.T) {
handled := false
mux := NewWorkMux()
mux.Handle("tube1", HandlerFunc(func(*Job) {}))
mux.Handle("tube2", HandlerFunc(func(*Job) { handled = true }))
mux.Handle("tubeN", HandlerFunc(func(*Job) {}))
mux.Work(&Job{Tube: "tube2"})
if !handled {
t.Fail()
}
}
func TestWorkUnhandledTube(t *testing.T) {
defer func() {
if err := recover(); err == nil || err != "no handler for tube tubeX" {
t.Fail()
}
}()
mux := NewWorkMux()
mux.Work(&Job{Tube: "tubeX"})
}