forked from rueian/cony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeclaration_test.go
159 lines (124 loc) · 3.04 KB
/
declaration_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cony
import (
"testing"
amqp "github.com/rabbitmq/amqp091-go"
)
type testDeclarer struct {
_QueueDeclare func(string) (amqp.Queue, error)
_ExchangeDeclare func() error
_QueueBind func() error
_QueueDeclarePassive func() (amqp.Queue, error)
_ExchangeDeclarePassive func() error
}
func (td *testDeclarer) QueueDeclare(name string, durable, autoDelete,
exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) {
return td._QueueDeclare(name)
}
func (td *testDeclarer) ExchangeDeclare(name, kind string, durable, autoDelete,
internal, noWait bool, args amqp.Table) error {
return td._ExchangeDeclare()
}
func (td *testDeclarer) QueueBind(name, key, exchange string, noWait bool,
args amqp.Table) error {
return td._QueueBind()
}
func (td *testDeclarer) QueueDeclarePassive(name string, durable, autoDelete,
exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) {
return td._QueueDeclarePassive()
}
func (td *testDeclarer) ExchangeDeclarePassive(name, kind string, durable,
autoDelete, internal, noWait bool, args amqp.Table) error {
return td._ExchangeDeclarePassive()
}
func TestDeclareQueue(t *testing.T) {
var (
callOK, nameOK bool
)
q := &Queue{
Name: "Q1",
}
td := &testDeclarer{
_QueueDeclare: func(name string) (amqp.Queue, error) {
callOK = true
if name == "Q1" {
nameOK = true
}
return amqp.Queue{Name: "Q1_REAL"}, nil
},
}
testDec := DeclareQueue(q)
testDec(td)
if !callOK {
t.Error("DeclareQueue() should call declarer.QueueDeclare()")
}
if q.Name != "Q1_REAL" {
t.Error("DeclareQueue() should update queue name from AMQP reply")
}
// call it another time (like reconnect event happened)
testDec(td)
if !nameOK {
t.Error("queue name should be preserved")
}
}
func TestDeclareExchange(t *testing.T) {
var ok bool
e := Exchange{Name: "ex1"}
td := &testDeclarer{
_ExchangeDeclare: func() error {
ok = true
return nil
},
}
DeclareExchange(e)(td)
if !ok {
t.Error("DeclareExchange() should call declarer.ExchangeDeclare()")
}
}
func TestDeclareBinding(t *testing.T) {
var ok bool
b := Binding{
Queue: &Queue{Name: "lol1"},
Exchange: Exchange{Name: "lol2"},
Key: "ololoev",
}
td := &testDeclarer{
_QueueBind: func() error {
ok = true
return nil
},
}
DeclareBinding(b)(td)
if !ok {
t.Error("DeclareBinding() should call declarer.QueueBind()")
}
}
func TestDeclareQueuePassive(t *testing.T) {
var ok bool
q := &Queue{
Name: "Q1",
}
td := &testDeclarer{
_QueueDeclarePassive: func() (amqp.Queue, error) {
ok = true
return amqp.Queue{Name: q.Name}, nil
},
}
DeclareQueuePassive(q)(td)
if !ok {
t.Error("DeclareQueuePassive() should call declarer.QueueDeclarePassive()")
}
}
func TestDeclareExchangePassive(t *testing.T) {
var ok bool
e := Exchange{Name: "ex1"}
td := &testDeclarer{
_ExchangeDeclarePassive: func() error {
ok = true
return nil
},
}
DeclareExchangePassive(e)(td)
if !ok {
t.Error("DeclareExchangePassive() should call declarer.ExchangeDeclarePassive()")
}
}