From 4709626f3575c2f6ad62680d36c88cc869b96ed0 Mon Sep 17 00:00:00 2001 From: Alexandre Vicenzi Date: Thu, 1 Dec 2016 11:40:58 -0200 Subject: [PATCH] Add mock package. --- amqp/connection.go | 9 +++---- amqp/consumer.go | 11 +++++---- amqp/producer.go | 5 ++-- connection.go => messaging/connection.go | 2 +- consumer.go => messaging/consumer.go | 2 +- producer.go => messaging/producer.go | 2 +- mock/connection.go | 24 +++++++++++++++++++ mock/consumer.go | 30 ++++++++++++++++++++++++ mock/producer.go | 14 +++++++++++ 9 files changed, 85 insertions(+), 14 deletions(-) rename connection.go => messaging/connection.go (86%) rename consumer.go => messaging/consumer.go (91%) rename producer.go => messaging/producer.go (80%) create mode 100644 mock/connection.go create mode 100644 mock/consumer.go create mode 100644 mock/producer.go diff --git a/amqp/connection.go b/amqp/connection.go index 0efd6a4..5a69e57 100644 --- a/amqp/connection.go +++ b/amqp/connection.go @@ -1,7 +1,8 @@ package amqp import ( - base "github.com/eventials/goevents" + "github.com/eventials/goevents/messaging" + amqplib "github.com/streadway/amqp" ) @@ -14,7 +15,7 @@ type Connection struct { queueName string } -func NewConnection(url, exchange, queue string) (base.Connection, error) { +func NewConnection(url, exchange, queue string) (messaging.Connection, error) { conn, err := amqplib.Dial(url) if err != nil { @@ -63,11 +64,11 @@ func NewConnection(url, exchange, queue string) (base.Connection, error) { }, nil } -func (c *Connection) Consumer(autoAck bool) (base.Consumer, error) { +func (c *Connection) Consumer(autoAck bool) (messaging.Consumer, error) { return NewConsumer(c, autoAck) } -func (c *Connection) Producer() (base.Producer, error) { +func (c *Connection) Producer() (messaging.Producer, error) { return NewProducer(c) } diff --git a/amqp/consumer.go b/amqp/consumer.go index 0c32897..3b41ba7 100644 --- a/amqp/consumer.go +++ b/amqp/consumer.go @@ -4,13 +4,14 @@ import ( "regexp" "strings" - base "github.com/eventials/goevents" + "github.com/eventials/goevents/messaging" + amqplib "github.com/streadway/amqp" ) type handler struct { action string - handler base.EventHandler + handler messaging.EventHandler re *regexp.Regexp } @@ -20,7 +21,7 @@ type Consumer struct { handlers []handler } -func NewConsumer(c base.Connection, autoAck bool) (base.Consumer, error) { +func NewConsumer(c messaging.Connection, autoAck bool) (messaging.Consumer, error) { amqpConn := c.(*Connection) return &Consumer{ @@ -56,7 +57,7 @@ func (c *Consumer) dispatch(msg amqplib.Delivery) { } } -func (c *Consumer) getHandler(action string) (base.EventHandler, bool) { +func (c *Consumer) getHandler(action string) (messaging.EventHandler, bool) { for _, h := range c.handlers { if h.re.MatchString(action) { return h.handler, true @@ -66,7 +67,7 @@ func (c *Consumer) getHandler(action string) (base.EventHandler, bool) { return nil, false } -func (c *Consumer) Subscribe(action string, handlerFn base.EventHandler) error { +func (c *Consumer) Subscribe(action string, handlerFn messaging.EventHandler) error { // TODO: Replace # pattern too. pattern := strings.Replace(action, "*", "(.*)", 0) re, err := regexp.Compile(pattern) diff --git a/amqp/producer.go b/amqp/producer.go index 58f7c1e..9e666ed 100644 --- a/amqp/producer.go +++ b/amqp/producer.go @@ -3,7 +3,8 @@ package amqp import ( "time" - base "github.com/eventials/goevents" + "github.com/eventials/goevents/messaging" + amqplib "github.com/streadway/amqp" ) @@ -11,7 +12,7 @@ type Producer struct { conn *Connection } -func NewProducer(c base.Connection) (base.Producer, error) { +func NewProducer(c messaging.Connection) (messaging.Producer, error) { amqpConn := c.(*Connection) return &Producer{ diff --git a/connection.go b/messaging/connection.go similarity index 86% rename from connection.go rename to messaging/connection.go index 1b48718..b714798 100644 --- a/connection.go +++ b/messaging/connection.go @@ -1,4 +1,4 @@ -package events +package messaging type Connection interface { Consumer(autoAck bool) (Consumer, error) diff --git a/consumer.go b/messaging/consumer.go similarity index 91% rename from consumer.go rename to messaging/consumer.go index 69c83e6..3fe79cb 100644 --- a/consumer.go +++ b/messaging/consumer.go @@ -1,4 +1,4 @@ -package events +package messaging type EventHandler func(body []byte) bool diff --git a/producer.go b/messaging/producer.go similarity index 80% rename from producer.go rename to messaging/producer.go index a041b48..c689ca8 100644 --- a/producer.go +++ b/messaging/producer.go @@ -1,4 +1,4 @@ -package events +package messaging type Producer interface { Publish(action string, data []byte) error diff --git a/mock/connection.go b/mock/connection.go new file mode 100644 index 0000000..7179216 --- /dev/null +++ b/mock/connection.go @@ -0,0 +1,24 @@ +package mock + +import ( + "github.com/eventials/goevents/messaging" + "github.com/stretchr/testify/mock" +) + +type Connection struct { + mock.Mock +} + +func (c *Connection) Consumer(autoAck bool) (messaging.Consumer, error) { + args := c.Called(autoAck) + return args.Get(0).(messaging.Consumer), args.Error(1) +} + +func (c *Connection) Producer() (messaging.Producer, error) { + args := c.Called() + return args.Get(0).(*Producer), args.Error(1) +} + +func (c *Connection) Close() { + c.Called() +} diff --git a/mock/consumer.go b/mock/consumer.go new file mode 100644 index 0000000..88780be --- /dev/null +++ b/mock/consumer.go @@ -0,0 +1,30 @@ +package mock + +import ( + "github.com/eventials/goevents/messaging" + "github.com/stretchr/testify/mock" +) + +type Consumer struct { + mock.Mock +} + +func (c *Consumer) Subscribe(action string, handler messaging.EventHandler) error { + args := c.Called(action, handler) + return args.Error(1) +} + +func (c *Consumer) Unsubscribe(action string) error { + args := c.Called(action) + return args.Error(1) +} + +func (c *Consumer) Listen() error { + args := c.Called() + return args.Error(1) +} + +func (c *Consumer) ListenForever() error { + args := c.Called() + return args.Error(1) +} diff --git a/mock/producer.go b/mock/producer.go new file mode 100644 index 0000000..f2ffd19 --- /dev/null +++ b/mock/producer.go @@ -0,0 +1,14 @@ +package mock + +import ( + "github.com/stretchr/testify/mock" +) + +type Producer struct { + mock.Mock +} + +func (p *Producer) Publish(action string, data []byte) error { + args := p.Called(action, data) + return args.Error(1) +}