Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 5f3a089

Browse files
author
Christian Häusler
authored
Better quality connection setup tests (#18)
1 parent 6d4ef44 commit 5f3a089

13 files changed

+183
-131
lines changed

consumer/connection.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,5 @@ func (c *rabbitMqConnection) queueArgs() amqp.Table {
163163
args["x-max-priority"] = c.cfg.Priority()
164164
}
165165

166-
if len(args) > 0 {
167-
return args
168-
}
169-
170-
return nil
166+
return args
171167
}

consumer/connection_test.go

+106-126
Original file line numberDiff line numberDiff line change
@@ -12,112 +12,36 @@ import (
1212
"github.com/stretchr/testify/mock"
1313
)
1414

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+
)
10328

104-
var amqpTable amqp.Table
29+
var nilAmqpTable amqp.Table
30+
var emptyAmqpTable = amqp.Table{}
10531

10632
var queueTests = []struct {
10733
name string
10834
config string
10935
setup func(*TestChannel)
11036
err error
11137
}{
112-
// The happy path.
38+
// Simple queue.
11339
{
114-
"happyPath",
40+
"simpleQueue",
11541
defaultConfig,
11642
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()
12145
},
12246
nil,
12347
},
@@ -126,10 +50,8 @@ var queueTests = []struct {
12650
"queueWithTTL",
12751
ttlConfig,
12852
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()
13355
},
13456
nil,
13557
},
@@ -139,9 +61,9 @@ var queueTests = []struct {
13961
priorityConfig,
14062
func(ch *TestChannel) {
14163
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()
14567
},
14668
nil,
14769
},
@@ -151,10 +73,10 @@ var queueTests = []struct {
15173
multipleRoutingKeysConfig,
15274
func(ch *TestChannel) {
15375
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()
15880
},
15981
nil,
16082
},
@@ -164,9 +86,9 @@ var queueTests = []struct {
16486
oneEmptyRoutingKeyConfig,
16587
func(ch *TestChannel) {
16688
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()
17092
},
17193
nil,
17294
},
@@ -176,18 +98,28 @@ var queueTests = []struct {
17698
noRoutingKeyConfig,
17799
func(ch *TestChannel) {
178100
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()
182114
},
183115
nil,
184116
},
185117
// Set QoS fails.
186118
{
187119
"setQosFail",
188-
defaultConfig,
120+
qosConfig,
189121
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()
191123
},
192124
fmt.Errorf("failed to set QoS: QoS error"),
193125
},
@@ -196,31 +128,79 @@ var queueTests = []struct {
196128
"declareQueueFail",
197129
defaultConfig,
198130
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()
201133
},
202134
fmt.Errorf("failed to declare queue: queue error"),
203135
},
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+
},
204172
// Declare exchange fails.
205173
{
206174
"declareExchangeFail",
207-
defaultConfig,
175+
simpleExchangeConfig,
208176
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()
212180
},
213181
fmt.Errorf("failed to declare exchange: declare exchagne error"),
214182
},
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+
},
215195
// Bind queue fails.
216196
{
217197
"bindQueueFail",
218-
defaultConfig,
198+
routingConfig,
219199
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()
224204
},
225205
fmt.Errorf("failed to bind queue to exchange: queue bind error"),
226206
},
@@ -229,7 +209,7 @@ var queueTests = []struct {
229209
func TestQueueSettings(t *testing.T) {
230210
for _, test := range queueTests {
231211
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))
233213

234214
var b bytes.Buffer
235215
infLogger := log.New(&b, "", 0)

consumer/fixtures/autodelete.conf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[rabbitmq]
2+
queue = queueName
3+
4+
[exchange]
5+
autodelete = On
6+
name = exchangeName
7+
type = exchangeType

consumer/fixtures/default.conf

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[rabbitmq]
2+
queue = defaultQueue

consumer/fixtures/durable.conf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[rabbitmq]
2+
queue = queueName
3+
4+
[exchange]
5+
durable = On
6+
name = exchangeName
7+
type = exchangeType

consumer/fixtures/empty_routing.conf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[rabbitmq]
2+
queue = emptyRoutingQueue
3+
4+
[queuesettings]
5+
routingkey = "<empty>"
6+
7+
[exchange]
8+
name = emptyRoutingExchange
9+
type = emptyRoutingType

consumer/fixtures/exchange.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[rabbitmq]
2+
queue = queueName
3+
4+
[exchange]
5+
name = exchangeName
6+
type = exchangeType
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[rabbitmq]
2+
queue = multiRoutingQueue
3+
4+
[queuesettings]
5+
routingkey = foo
6+
routingkey = bar
7+
8+
[exchange]
9+
name = multiRoutingExchange
10+
type = multiRoutingType

consumer/fixtures/no_routing.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[rabbitmq]
2+
queue = noRoutingQueue
3+
4+
[exchange]
5+
name = noRoutingExchange
6+
type = noRoutingType

consumer/fixtures/priority.conf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[rabbitmq]
2+
queue = priorityWorker
3+
4+
[queuesettings]
5+
priority = 42
6+
7+
[exchange]
8+
name = priorityExchange
9+
type = priorityType

0 commit comments

Comments
 (0)