@@ -12,112 +12,36 @@ import (
12
12
"github.com/stretchr/testify/mock"
13
13
)
14
14
15
- const defaultConfig = `[rabbitmq]
16
- host=localhost
17
- username=ricbra
18
- password=t3st
19
- vhost=staging
20
- port=123
21
- queue=worker
22
-
23
- [prefetch]
24
- count=3
25
- global=On
26
-
27
- [queuesettings]
28
- routingkey=foo
29
-
30
- [exchange]
31
- name=worker
32
- autodelete=Off
33
- type=test
34
- durable=On
35
-
36
- [logs]
37
- error=a
38
- info=b`
39
-
40
- const ttlConfig = `[rabbitmq]
41
- host=localhost
42
- username=ricbra
43
- password=t3st
44
- vhost=staging
45
- port=123
46
- queue=worker
47
-
48
- [prefetch]
49
- count=3
50
- global=On
51
-
52
- [queuesettings]
53
- routingkey=foo
54
- messagettl=1200
55
-
56
- [exchange]
57
- name=worker
58
- autodelete=Off
59
- type=test
60
- durable=On
61
-
62
- [logs]
63
- error=a
64
- info=b`
65
-
66
- const priorityConfig = `[rabbitmq]
67
- queue=worker
68
-
69
- [queuesettings]
70
- priority=42
71
-
72
- [exchange]
73
- name=worker
74
- type=test`
75
-
76
- const multipleRoutingKeysConfig = `[rabbitmq]
77
- queue=worker
78
-
79
- [queuesettings]
80
- routingkey=foo
81
- routingkey=bar
82
-
83
- [exchange]
84
- name=worker
85
- type=test`
86
-
87
- const oneEmptyRoutingKeyConfig = `[rabbitmq]
88
- queue=worker
89
-
90
- [queuesettings]
91
- routingkey="<empty>"
92
-
93
- [exchange]
94
- name=worker
95
- type=test`
96
-
97
- const noRoutingKeyConfig = `[rabbitmq]
98
- queue=worker
99
-
100
- [exchange]
101
- name=worker
102
- type=test`
15
+ const (
16
+ autodeleteExchangeConfig = "autodelete"
17
+ defaultConfig = "default"
18
+ durableExchangeConfig = "durable"
19
+ multipleRoutingKeysConfig = "multiple_routing"
20
+ noRoutingKeyConfig = "no_routing"
21
+ oneEmptyRoutingKeyConfig = "empty_routing"
22
+ priorityConfig = "priority"
23
+ qosConfig = "qos"
24
+ routingConfig = "routing"
25
+ simpleExchangeConfig = "exchange"
26
+ ttlConfig = "ttl"
27
+ )
103
28
104
- var amqpTable amqp.Table
29
+ var nilAmqpTable amqp.Table
30
+ var emptyAmqpTable = amqp.Table {}
105
31
106
32
var queueTests = []struct {
107
33
name string
108
34
config string
109
35
setup func (* TestChannel )
110
36
err error
111
37
}{
112
- // The happy path .
38
+ // Simple queue .
113
39
{
114
- "happyPath " ,
40
+ "simpleQueue " ,
115
41
defaultConfig ,
116
42
func (ch * TestChannel ) {
117
- ch .On ("Qos" , 3 , 0 , true ).Return (nil ).Once ()
118
- ch .On ("QueueDeclare" , "worker" , true , false , false , false , amqpTable ).Return (amqp.Queue {}, nil ).Once ()
119
- ch .On ("ExchangeDeclare" , "worker" , "test" , true , false , false , false , amqp.Table {}).Return (nil ).Once ()
120
- ch .On ("QueueBind" , "worker" , "foo" , "worker" , false , amqpTable ).Return (nil ).Once ()
43
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
44
+ ch .On ("QueueDeclare" , "defaultQueue" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
121
45
},
122
46
nil ,
123
47
},
@@ -126,10 +50,8 @@ var queueTests = []struct {
126
50
"queueWithTTL" ,
127
51
ttlConfig ,
128
52
func (ch * TestChannel ) {
129
- ch .On ("Qos" , 3 , 0 , true ).Return (nil ).Once ()
130
- ch .On ("QueueDeclare" , "worker" , true , false , false , false , amqp.Table {"x-message-ttl" : int32 (1200 )}).Return (amqp.Queue {}, nil ).Once ()
131
- ch .On ("ExchangeDeclare" , "worker" , "test" , true , false , false , false , amqp.Table {}).Return (nil ).Once ()
132
- ch .On ("QueueBind" , "worker" , "foo" , "worker" , false , amqpTable ).Return (nil ).Once ()
53
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
54
+ ch .On ("QueueDeclare" , "ttlQueue" , true , false , false , false , amqp.Table {"x-message-ttl" : int32 (1200 )}).Return (amqp.Queue {}, nil ).Once ()
133
55
},
134
56
nil ,
135
57
},
@@ -139,9 +61,9 @@ var queueTests = []struct {
139
61
priorityConfig ,
140
62
func (ch * TestChannel ) {
141
63
ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
142
- ch .On ("QueueDeclare" , "worker " , true , false , false , false , amqp.Table {"x-max-priority" : int32 (42 )}).Return (amqp.Queue {}, nil ).Once ()
143
- ch .On ("ExchangeDeclare" , "worker " , "test " , false , false , false , false , amqp. Table {} ).Return (nil ).Once ()
144
- ch .On ("QueueBind" , "worker " , "" , "worker " , false , amqpTable ).Return (nil ).Once ()
64
+ ch .On ("QueueDeclare" , "priorityWorker " , true , false , false , false , amqp.Table {"x-max-priority" : int32 (42 )}).Return (amqp.Queue {}, nil ).Once ()
65
+ ch .On ("ExchangeDeclare" , "priorityExchange " , "priorityType " , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
66
+ ch .On ("QueueBind" , "priorityWorker " , "" , "priorityExchange " , false , nilAmqpTable ).Return (nil ).Once ()
145
67
},
146
68
nil ,
147
69
},
@@ -151,10 +73,10 @@ var queueTests = []struct {
151
73
multipleRoutingKeysConfig ,
152
74
func (ch * TestChannel ) {
153
75
ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
154
- ch .On ("QueueDeclare" , "worker " , true , false , false , false , amqpTable ).Return (amqp.Queue {}, nil ).Once ()
155
- ch .On ("ExchangeDeclare" , "worker " , "test " , false , false , false , false , amqp. Table {} ).Return (nil ).Once ()
156
- ch .On ("QueueBind" , "worker " , "foo" , "worker " , false , amqpTable ).Return (nil ).Once ()
157
- ch .On ("QueueBind" , "worker " , "bar" , "worker " , false , amqpTable ).Return (nil ).Once ()
76
+ ch .On ("QueueDeclare" , "multiRoutingQueue " , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
77
+ ch .On ("ExchangeDeclare" , "multiRoutingExchange " , "multiRoutingType " , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
78
+ ch .On ("QueueBind" , "multiRoutingQueue " , "foo" , "multiRoutingExchange " , false , nilAmqpTable ).Return (nil ).Once ()
79
+ ch .On ("QueueBind" , "multiRoutingQueue " , "bar" , "multiRoutingExchange " , false , nilAmqpTable ).Return (nil ).Once ()
158
80
},
159
81
nil ,
160
82
},
@@ -164,9 +86,9 @@ var queueTests = []struct {
164
86
oneEmptyRoutingKeyConfig ,
165
87
func (ch * TestChannel ) {
166
88
ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
167
- ch .On ("QueueDeclare" , "worker " , true , false , false , false , amqpTable ).Return (amqp.Queue {}, nil ).Once ()
168
- ch .On ("ExchangeDeclare" , "worker " , "test " , false , false , false , false , amqp. Table {} ).Return (nil ).Once ()
169
- ch .On ("QueueBind" , "worker " , "" , "worker " , false , amqpTable ).Return (nil ).Once ()
89
+ ch .On ("QueueDeclare" , "emptyRoutingQueue " , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
90
+ ch .On ("ExchangeDeclare" , "emptyRoutingExchange " , "emptyRoutingType " , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
91
+ ch .On ("QueueBind" , "emptyRoutingQueue " , "" , "emptyRoutingExchange " , false , nilAmqpTable ).Return (nil ).Once ()
170
92
},
171
93
nil ,
172
94
},
@@ -176,18 +98,28 @@ var queueTests = []struct {
176
98
noRoutingKeyConfig ,
177
99
func (ch * TestChannel ) {
178
100
ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
179
- ch .On ("QueueDeclare" , "worker" , true , false , false , false , amqpTable ).Return (amqp.Queue {}, nil ).Once ()
180
- ch .On ("ExchangeDeclare" , "worker" , "test" , false , false , false , false , amqp.Table {}).Return (nil ).Once ()
181
- ch .On ("QueueBind" , "worker" , "" , "worker" , false , amqpTable ).Return (nil ).Once ()
101
+ ch .On ("QueueDeclare" , "noRoutingQueue" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
102
+ ch .On ("ExchangeDeclare" , "noRoutingExchange" , "noRoutingType" , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
103
+ ch .On ("QueueBind" , "noRoutingQueue" , "" , "noRoutingExchange" , false , nilAmqpTable ).Return (nil ).Once ()
104
+ },
105
+ nil ,
106
+ },
107
+ // Set QoS.
108
+ {
109
+ "setQos" ,
110
+ qosConfig ,
111
+ func (ch * TestChannel ) {
112
+ ch .On ("Qos" , 42 , 0 , true ).Return (nil ).Once ()
113
+ ch .On ("QueueDeclare" , "qosQueue" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
182
114
},
183
115
nil ,
184
116
},
185
117
// Set QoS fails.
186
118
{
187
119
"setQosFail" ,
188
- defaultConfig ,
120
+ qosConfig ,
189
121
func (ch * TestChannel ) {
190
- ch .On ("Qos" , 3 , 0 , true ).Return (fmt .Errorf ("QoS error" )).Once ()
122
+ ch .On ("Qos" , 42 , 0 , true ).Return (fmt .Errorf ("QoS error" )).Once ()
191
123
},
192
124
fmt .Errorf ("failed to set QoS: QoS error" ),
193
125
},
@@ -196,31 +128,79 @@ var queueTests = []struct {
196
128
"declareQueueFail" ,
197
129
defaultConfig ,
198
130
func (ch * TestChannel ) {
199
- ch .On ("Qos" , 3 , 0 , true ).Return (nil ).Once ()
200
- ch .On ("QueueDeclare" , "worker " , true , false , false , false , amqpTable ).Return (amqp.Queue {}, fmt .Errorf ("queue error" )).Once ()
131
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
132
+ ch .On ("QueueDeclare" , "defaultQueue " , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, fmt .Errorf ("queue error" )).Once ()
201
133
},
202
134
fmt .Errorf ("failed to declare queue: queue error" ),
203
135
},
136
+ // Declare exchange.
137
+ {
138
+ "declareExchange" ,
139
+ simpleExchangeConfig ,
140
+ func (ch * TestChannel ) {
141
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
142
+ ch .On ("QueueDeclare" , "queueName" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
143
+ ch .On ("ExchangeDeclare" , "exchangeName" , "exchangeType" , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
144
+ ch .On ("QueueBind" , "queueName" , "" , "exchangeName" , false , nilAmqpTable ).Return (nil ).Once ()
145
+ },
146
+ nil ,
147
+ },
148
+ // Declare durable exchange.
149
+ {
150
+ "declareDurableExchange" ,
151
+ durableExchangeConfig ,
152
+ func (ch * TestChannel ) {
153
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
154
+ ch .On ("QueueDeclare" , "queueName" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
155
+ ch .On ("ExchangeDeclare" , "exchangeName" , "exchangeType" , true , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
156
+ ch .On ("QueueBind" , "queueName" , "" , "exchangeName" , false , nilAmqpTable ).Return (nil ).Once ()
157
+ },
158
+ nil ,
159
+ },
160
+ // Declare auto delete exchange.
161
+ {
162
+ "declareAutoDeleteExchange" ,
163
+ autodeleteExchangeConfig ,
164
+ func (ch * TestChannel ) {
165
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
166
+ ch .On ("QueueDeclare" , "queueName" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
167
+ ch .On ("ExchangeDeclare" , "exchangeName" , "exchangeType" , false , true , false , false , emptyAmqpTable ).Return (nil ).Once ()
168
+ ch .On ("QueueBind" , "queueName" , "" , "exchangeName" , false , nilAmqpTable ).Return (nil ).Once ()
169
+ },
170
+ nil ,
171
+ },
204
172
// Declare exchange fails.
205
173
{
206
174
"declareExchangeFail" ,
207
- defaultConfig ,
175
+ simpleExchangeConfig ,
208
176
func (ch * TestChannel ) {
209
- ch .On ("Qos" , 3 , 0 , true ).Return (nil ).Once ()
210
- ch .On ("QueueDeclare" , "worker " , true , false , false , false , amqpTable ).Return (amqp.Queue {}, nil ).Once ()
211
- ch .On ("ExchangeDeclare" , "worker " , "test " , true , false , false , false , amqp. Table {} ).Return (fmt .Errorf ("declare exchagne error" )).Once ()
177
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
178
+ ch .On ("QueueDeclare" , "queueName " , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
179
+ ch .On ("ExchangeDeclare" , "exchangeName " , "exchangeType " , false , false , false , false , emptyAmqpTable ).Return (fmt .Errorf ("declare exchagne error" )).Once ()
212
180
},
213
181
fmt .Errorf ("failed to declare exchange: declare exchagne error" ),
214
182
},
183
+ // Bind queue.
184
+ {
185
+ "bindQueue" ,
186
+ routingConfig ,
187
+ func (ch * TestChannel ) {
188
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
189
+ ch .On ("QueueDeclare" , "routingQueue" , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
190
+ ch .On ("ExchangeDeclare" , "routingExchange" , "routingType" , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
191
+ ch .On ("QueueBind" , "routingQueue" , "routingKey" , "routingExchange" , false , nilAmqpTable ).Return (nil ).Once ()
192
+ },
193
+ nil ,
194
+ },
215
195
// Bind queue fails.
216
196
{
217
197
"bindQueueFail" ,
218
- defaultConfig ,
198
+ routingConfig ,
219
199
func (ch * TestChannel ) {
220
- ch .On ("Qos" , 3 , 0 , true ).Return (nil ).Once ()
221
- ch .On ("QueueDeclare" , "worker " , true , false , false , false , amqpTable ).Return (amqp.Queue {}, nil ).Once ()
222
- ch .On ("ExchangeDeclare" , "worker " , "test " , true , false , false , false , amqp. Table {} ).Return (nil ).Once ()
223
- ch .On ("QueueBind" , "worker " , "foo " , "worker " , false , amqpTable ).Return (fmt .Errorf ("queue bind error" )).Once ()
200
+ ch .On ("Qos" , 3 , 0 , false ).Return (nil ).Once ()
201
+ ch .On ("QueueDeclare" , "routingQueue " , true , false , false , false , emptyAmqpTable ).Return (amqp.Queue {}, nil ).Once ()
202
+ ch .On ("ExchangeDeclare" , "routingExchange " , "routingType " , false , false , false , false , emptyAmqpTable ).Return (nil ).Once ()
203
+ ch .On ("QueueBind" , "routingQueue " , "routingKey " , "routingExchange " , false , nilAmqpTable ).Return (fmt .Errorf ("queue bind error" )).Once ()
224
204
},
225
205
fmt .Errorf ("failed to bind queue to exchange: queue bind error" ),
226
206
},
@@ -229,7 +209,7 @@ var queueTests = []struct {
229
209
func TestQueueSettings (t * testing.T ) {
230
210
for _ , test := range queueTests {
231
211
t .Run (test .name , func (t * testing.T ) {
232
- cfg , _ := config .CreateFromString ( test .config )
212
+ cfg , _ := config .LoadAndParse ( fmt . Sprintf ( "fixtures/%s.conf" , test .config ) )
233
213
234
214
var b bytes.Buffer
235
215
infLogger := log .New (& b , "" , 0 )
0 commit comments