Skip to content

Commit

Permalink
Better quality connection setup tests (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Häusler authored Jan 23, 2018
1 parent 6d4ef44 commit 5f3a089
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 131 deletions.
6 changes: 1 addition & 5 deletions consumer/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,5 @@ func (c *rabbitMqConnection) queueArgs() amqp.Table {
args["x-max-priority"] = c.cfg.Priority()
}

if len(args) > 0 {
return args
}

return nil
return args
}
232 changes: 106 additions & 126 deletions consumer/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,112 +12,36 @@ import (
"github.com/stretchr/testify/mock"
)

const defaultConfig = `[rabbitmq]
host=localhost
username=ricbra
password=t3st
vhost=staging
port=123
queue=worker
[prefetch]
count=3
global=On
[queuesettings]
routingkey=foo
[exchange]
name=worker
autodelete=Off
type=test
durable=On
[logs]
error=a
info=b`

const ttlConfig = `[rabbitmq]
host=localhost
username=ricbra
password=t3st
vhost=staging
port=123
queue=worker
[prefetch]
count=3
global=On
[queuesettings]
routingkey=foo
messagettl=1200
[exchange]
name=worker
autodelete=Off
type=test
durable=On
[logs]
error=a
info=b`

const priorityConfig = `[rabbitmq]
queue=worker
[queuesettings]
priority=42
[exchange]
name=worker
type=test`

const multipleRoutingKeysConfig = `[rabbitmq]
queue=worker
[queuesettings]
routingkey=foo
routingkey=bar
[exchange]
name=worker
type=test`

const oneEmptyRoutingKeyConfig = `[rabbitmq]
queue=worker
[queuesettings]
routingkey="<empty>"
[exchange]
name=worker
type=test`

const noRoutingKeyConfig = `[rabbitmq]
queue=worker
[exchange]
name=worker
type=test`
const (
autodeleteExchangeConfig = "autodelete"
defaultConfig = "default"
durableExchangeConfig = "durable"
multipleRoutingKeysConfig = "multiple_routing"
noRoutingKeyConfig = "no_routing"
oneEmptyRoutingKeyConfig = "empty_routing"
priorityConfig = "priority"
qosConfig = "qos"
routingConfig = "routing"
simpleExchangeConfig = "exchange"
ttlConfig = "ttl"
)

var amqpTable amqp.Table
var nilAmqpTable amqp.Table
var emptyAmqpTable = amqp.Table{}

var queueTests = []struct {
name string
config string
setup func(*TestChannel)
err error
}{
// The happy path.
// Simple queue.
{
"happyPath",
"simpleQueue",
defaultConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, true).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", true, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "foo", "worker", false, amqpTable).Return(nil).Once()
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "defaultQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
},
nil,
},
Expand All @@ -126,10 +50,8 @@ var queueTests = []struct {
"queueWithTTL",
ttlConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, true).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqp.Table{"x-message-ttl": int32(1200)}).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", true, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "foo", "worker", false, amqpTable).Return(nil).Once()
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "ttlQueue", true, false, false, false, amqp.Table{"x-message-ttl": int32(1200)}).Return(amqp.Queue{}, nil).Once()
},
nil,
},
Expand All @@ -139,9 +61,9 @@ var queueTests = []struct {
priorityConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqp.Table{"x-max-priority": int32(42)}).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", false, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "", "worker", false, amqpTable).Return(nil).Once()
ch.On("QueueDeclare", "priorityWorker", true, false, false, false, amqp.Table{"x-max-priority": int32(42)}).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "priorityExchange", "priorityType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "priorityWorker", "", "priorityExchange", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
Expand All @@ -151,10 +73,10 @@ var queueTests = []struct {
multipleRoutingKeysConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", false, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "foo", "worker", false, amqpTable).Return(nil).Once()
ch.On("QueueBind", "worker", "bar", "worker", false, amqpTable).Return(nil).Once()
ch.On("QueueDeclare", "multiRoutingQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "multiRoutingExchange", "multiRoutingType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "multiRoutingQueue", "foo", "multiRoutingExchange", false, nilAmqpTable).Return(nil).Once()
ch.On("QueueBind", "multiRoutingQueue", "bar", "multiRoutingExchange", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
Expand All @@ -164,9 +86,9 @@ var queueTests = []struct {
oneEmptyRoutingKeyConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", false, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "", "worker", false, amqpTable).Return(nil).Once()
ch.On("QueueDeclare", "emptyRoutingQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "emptyRoutingExchange", "emptyRoutingType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "emptyRoutingQueue", "", "emptyRoutingExchange", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
Expand All @@ -176,18 +98,28 @@ var queueTests = []struct {
noRoutingKeyConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", false, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "", "worker", false, amqpTable).Return(nil).Once()
ch.On("QueueDeclare", "noRoutingQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "noRoutingExchange", "noRoutingType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "noRoutingQueue", "", "noRoutingExchange", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
// Set QoS.
{
"setQos",
qosConfig,
func(ch *TestChannel) {
ch.On("Qos", 42, 0, true).Return(nil).Once()
ch.On("QueueDeclare", "qosQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
},
nil,
},
// Set QoS fails.
{
"setQosFail",
defaultConfig,
qosConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, true).Return(fmt.Errorf("QoS error")).Once()
ch.On("Qos", 42, 0, true).Return(fmt.Errorf("QoS error")).Once()
},
fmt.Errorf("failed to set QoS: QoS error"),
},
Expand All @@ -196,31 +128,79 @@ var queueTests = []struct {
"declareQueueFail",
defaultConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, true).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, fmt.Errorf("queue error")).Once()
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "defaultQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, fmt.Errorf("queue error")).Once()
},
fmt.Errorf("failed to declare queue: queue error"),
},
// Declare exchange.
{
"declareExchange",
simpleExchangeConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "queueName", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "exchangeName", "exchangeType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "queueName", "", "exchangeName", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
// Declare durable exchange.
{
"declareDurableExchange",
durableExchangeConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "queueName", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "exchangeName", "exchangeType", true, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "queueName", "", "exchangeName", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
// Declare auto delete exchange.
{
"declareAutoDeleteExchange",
autodeleteExchangeConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "queueName", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "exchangeName", "exchangeType", false, true, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "queueName", "", "exchangeName", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
// Declare exchange fails.
{
"declareExchangeFail",
defaultConfig,
simpleExchangeConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, true).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", true, false, false, false, amqp.Table{}).Return(fmt.Errorf("declare exchagne error")).Once()
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "queueName", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "exchangeName", "exchangeType", false, false, false, false, emptyAmqpTable).Return(fmt.Errorf("declare exchagne error")).Once()
},
fmt.Errorf("failed to declare exchange: declare exchagne error"),
},
// Bind queue.
{
"bindQueue",
routingConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "routingQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "routingExchange", "routingType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "routingQueue", "routingKey", "routingExchange", false, nilAmqpTable).Return(nil).Once()
},
nil,
},
// Bind queue fails.
{
"bindQueueFail",
defaultConfig,
routingConfig,
func(ch *TestChannel) {
ch.On("Qos", 3, 0, true).Return(nil).Once()
ch.On("QueueDeclare", "worker", true, false, false, false, amqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "worker", "test", true, false, false, false, amqp.Table{}).Return(nil).Once()
ch.On("QueueBind", "worker", "foo", "worker", false, amqpTable).Return(fmt.Errorf("queue bind error")).Once()
ch.On("Qos", 3, 0, false).Return(nil).Once()
ch.On("QueueDeclare", "routingQueue", true, false, false, false, emptyAmqpTable).Return(amqp.Queue{}, nil).Once()
ch.On("ExchangeDeclare", "routingExchange", "routingType", false, false, false, false, emptyAmqpTable).Return(nil).Once()
ch.On("QueueBind", "routingQueue", "routingKey", "routingExchange", false, nilAmqpTable).Return(fmt.Errorf("queue bind error")).Once()
},
fmt.Errorf("failed to bind queue to exchange: queue bind error"),
},
Expand All @@ -229,7 +209,7 @@ var queueTests = []struct {
func TestQueueSettings(t *testing.T) {
for _, test := range queueTests {
t.Run(test.name, func(t *testing.T) {
cfg, _ := config.CreateFromString(test.config)
cfg, _ := config.LoadAndParse(fmt.Sprintf("fixtures/%s.conf", test.config))

var b bytes.Buffer
infLogger := log.New(&b, "", 0)
Expand Down
7 changes: 7 additions & 0 deletions consumer/fixtures/autodelete.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[rabbitmq]
queue = queueName

[exchange]
autodelete = On
name = exchangeName
type = exchangeType
2 changes: 2 additions & 0 deletions consumer/fixtures/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[rabbitmq]
queue = defaultQueue
7 changes: 7 additions & 0 deletions consumer/fixtures/durable.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[rabbitmq]
queue = queueName

[exchange]
durable = On
name = exchangeName
type = exchangeType
9 changes: 9 additions & 0 deletions consumer/fixtures/empty_routing.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[rabbitmq]
queue = emptyRoutingQueue

[queuesettings]
routingkey = "<empty>"

[exchange]
name = emptyRoutingExchange
type = emptyRoutingType
6 changes: 6 additions & 0 deletions consumer/fixtures/exchange.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[rabbitmq]
queue = queueName

[exchange]
name = exchangeName
type = exchangeType
10 changes: 10 additions & 0 deletions consumer/fixtures/multiple_routing.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[rabbitmq]
queue = multiRoutingQueue

[queuesettings]
routingkey = foo
routingkey = bar

[exchange]
name = multiRoutingExchange
type = multiRoutingType
6 changes: 6 additions & 0 deletions consumer/fixtures/no_routing.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[rabbitmq]
queue = noRoutingQueue

[exchange]
name = noRoutingExchange
type = noRoutingType
9 changes: 9 additions & 0 deletions consumer/fixtures/priority.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[rabbitmq]
queue = priorityWorker

[queuesettings]
priority = 42

[exchange]
name = priorityExchange
type = priorityType
Loading

0 comments on commit 5f3a089

Please sign in to comment.