From 44b5e625345cf9011c4a84ff97a6d87ee7f94c30 Mon Sep 17 00:00:00 2001 From: Artiom Diomin Date: Wed, 23 Nov 2016 09:40:49 +0200 Subject: [PATCH] Preserve declared name between reconnections fixes #18 --- .travis.yml | 2 +- declaration.go | 2 ++ declaration_test.go | 35 +++++++++++++++++++++++++---------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9a2d8eb..ace529d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: go go: - - 1.4 - 1.5 - 1.6 + - 1.7 services: - rabbitmq diff --git a/declaration.go b/declaration.go index cc0bfde..ea5784a 100644 --- a/declaration.go +++ b/declaration.go @@ -14,7 +14,9 @@ type Declarer interface { // DeclareQueue is a way to declare AMQP queue func DeclareQueue(q *Queue) Declaration { + name := q.Name return func(c Declarer) error { + q.Name = name realQ, err := c.QueueDeclare(q.Name, q.Durable, q.AutoDelete, diff --git a/declaration_test.go b/declaration_test.go index 6ec6501..776d120 100644 --- a/declaration_test.go +++ b/declaration_test.go @@ -7,46 +7,61 @@ import ( ) type testDeclarer struct { - _QueueDeclare func() (amqp.Queue, error) + _QueueDeclare func(string) (amqp.Queue, error) _ExchangeDeclare func() error _QueueBind func() error } -func (td *testDeclarer) QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) { - return td._QueueDeclare() +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 { +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 { +func (td *testDeclarer) QueueBind(name, key, exchange string, noWait bool, + args amqp.Table) error { return td._QueueBind() } func TestDeclareQueue(t *testing.T) { - var ok bool + var ( + callOK, nameOK bool + ) q := &Queue{ Name: "Q1", } td := &testDeclarer{ - _QueueDeclare: func() (amqp.Queue, error) { - ok = true + _QueueDeclare: func(name string) (amqp.Queue, error) { + callOK = true + if name == "Q1" { + nameOK = true + } return amqp.Queue{Name: "Q1_REAL"}, nil }, } - DeclareQueue(q)(td) + testDec := DeclareQueue(q) + testDec(td) - if !ok { + 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) {