-
Notifications
You must be signed in to change notification settings - Fork 36
/
middleware_test.go
112 lines (91 loc) · 2.25 KB
/
middleware_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
package workers
import (
"testing"
"github.com/stretchr/testify/assert"
)
func arrayCompare(a1, a2 []string) bool {
if len(a1) != len(a2) {
return false
}
for i := 0; i < len(a1); i++ {
if a1[i] != a2[i] {
return false
}
}
return true
}
type orderMiddleware struct {
name string
order *[]string
}
func (m *orderMiddleware) f() MiddlewareFunc {
return func(queue string, mgr *Manager, next JobFunc) JobFunc {
return func(message *Msg) (result error) {
*m.order = append(*m.order, m.name+" enter")
result = next(message)
*m.order = append(*m.order, m.name+" leave")
return
}
}
}
func TestNewMiddlewares(t *testing.T) {
//no middleware
middlewares := NewMiddlewares()
assert.Equal(t, 0, len(middlewares))
//middleware set when initializing
order := make([]string, 0)
first := orderMiddleware{"m1", &order}
second := orderMiddleware{"m2", &order}
middlewares = NewMiddlewares(first.f(), second.f())
message, _ := NewMsg("{\"foo\":\"bar\"}")
middlewares.build("myqueue", nil, func(message *Msg) error {
order = append(order, "job")
return nil
})(message)
expectedOrder := []string{
"m1 enter",
"m2 enter",
"job",
"m2 leave",
"m1 leave",
}
assert.Equal(t, expectedOrder, order)
}
func TestAppendMiddleware(t *testing.T) {
order := make([]string, 0)
first := orderMiddleware{"m1", &order}
second := orderMiddleware{"m2", &order}
middleware := NewMiddlewares().Append(first.f()).Append(second.f())
message, _ := NewMsg("{\"foo\":\"bar\"}")
middleware.build("myqueue", nil, func(message *Msg) error {
order = append(order, "job")
return nil
})(message)
expectedOrder := []string{
"m1 enter",
"m2 enter",
"job",
"m2 leave",
"m1 leave",
}
assert.Equal(t, expectedOrder, order)
}
func TestPrependMiddleware(t *testing.T) {
order := make([]string, 0)
first := orderMiddleware{"m1", &order}
second := orderMiddleware{"m2", &order}
middleware := NewMiddlewares().Prepend(first.f()).Prepend(second.f())
message, _ := NewMsg("{\"foo\":\"bar\"}")
middleware.build("myqueue", nil, func(message *Msg) error {
order = append(order, "job")
return nil
})(message)
expectedOrder := []string{
"m2 enter",
"m1 enter",
"job",
"m1 leave",
"m2 leave",
}
assert.Equal(t, expectedOrder, order)
}