Skip to content

Commit 189fe13

Browse files
kron4egspeedywizard
authored andcommitted
Preserve declared name between reconnections (#19)
fixes #18
1 parent fb14870 commit 189fe13

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: go
22

33
go:
4-
- 1.4
54
- 1.5
65
- 1.6
6+
- 1.7
77

88
services:
99
- rabbitmq

declaration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ type Declarer interface {
1414

1515
// DeclareQueue is a way to declare AMQP queue
1616
func DeclareQueue(q *Queue) Declaration {
17+
name := q.Name
1718
return func(c Declarer) error {
19+
q.Name = name
1820
realQ, err := c.QueueDeclare(q.Name,
1921
q.Durable,
2022
q.AutoDelete,

declaration_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,61 @@ import (
77
)
88

99
type testDeclarer struct {
10-
_QueueDeclare func() (amqp.Queue, error)
10+
_QueueDeclare func(string) (amqp.Queue, error)
1111
_ExchangeDeclare func() error
1212
_QueueBind func() error
1313
}
1414

15-
func (td *testDeclarer) QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) {
16-
return td._QueueDeclare()
15+
func (td *testDeclarer) QueueDeclare(name string, durable, autoDelete,
16+
exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) {
17+
return td._QueueDeclare(name)
1718
}
1819

19-
func (td *testDeclarer) ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error {
20+
func (td *testDeclarer) ExchangeDeclare(name, kind string, durable, autoDelete,
21+
internal, noWait bool, args amqp.Table) error {
2022
return td._ExchangeDeclare()
2123
}
2224

23-
func (td *testDeclarer) QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error {
25+
func (td *testDeclarer) QueueBind(name, key, exchange string, noWait bool,
26+
args amqp.Table) error {
2427
return td._QueueBind()
2528
}
2629

2730
func TestDeclareQueue(t *testing.T) {
28-
var ok bool
31+
var (
32+
callOK, nameOK bool
33+
)
2934

3035
q := &Queue{
3136
Name: "Q1",
3237
}
3338

3439
td := &testDeclarer{
35-
_QueueDeclare: func() (amqp.Queue, error) {
36-
ok = true
40+
_QueueDeclare: func(name string) (amqp.Queue, error) {
41+
callOK = true
42+
if name == "Q1" {
43+
nameOK = true
44+
}
3745
return amqp.Queue{Name: "Q1_REAL"}, nil
3846
},
3947
}
4048

41-
DeclareQueue(q)(td)
49+
testDec := DeclareQueue(q)
50+
testDec(td)
4251

43-
if !ok {
52+
if !callOK {
4453
t.Error("DeclareQueue() should call declarer.QueueDeclare()")
4554
}
4655

4756
if q.Name != "Q1_REAL" {
4857
t.Error("DeclareQueue() should update queue name from AMQP reply")
4958
}
59+
60+
// call it another time (like reconnect event happened)
61+
testDec(td)
62+
if !nameOK {
63+
t.Error("queue name should be preserved")
64+
}
5065
}
5166

5267
func TestDeclareExchange(t *testing.T) {

0 commit comments

Comments
 (0)