mirror of
https://github.com/eventials/goevents.git
synced 2025-04-24 13:48:53 +08:00
Add mock package.
This commit is contained in:
parent
144d3870f6
commit
4709626f35
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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{
|
||||
|
@ -1,4 +1,4 @@
|
||||
package events
|
||||
package messaging
|
||||
|
||||
type Connection interface {
|
||||
Consumer(autoAck bool) (Consumer, error)
|
@ -1,4 +1,4 @@
|
||||
package events
|
||||
package messaging
|
||||
|
||||
type EventHandler func(body []byte) bool
|
||||
|
@ -1,4 +1,4 @@
|
||||
package events
|
||||
package messaging
|
||||
|
||||
type Producer interface {
|
||||
Publish(action string, data []byte) error
|
24
mock/connection.go
Normal file
24
mock/connection.go
Normal file
@ -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()
|
||||
}
|
30
mock/consumer.go
Normal file
30
mock/consumer.go
Normal file
@ -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)
|
||||
}
|
14
mock/producer.go
Normal file
14
mock/producer.go
Normal file
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user