1
0
mirror of https://github.com/eventials/goevents.git synced 2025-04-24 13:48:53 +08:00
eventials.goevents/amqp/consumer_test.go

857 lines
17 KiB
Go
Raw Permalink Normal View History

2017-06-15 10:05:00 -03:00
package amqp
import (
"fmt"
2018-02-12 00:54:10 -02:00
"os"
2017-10-09 19:33:27 -03:00
"testing"
"time"
2018-02-10 14:38:29 -02:00
"github.com/eventials/goevents/messaging"
"github.com/stretchr/testify/assert"
2017-06-15 10:05:00 -03:00
)
2018-02-12 10:08:18 -02:00
const (
SleepSetupTopology = 300 * time.Millisecond
)
2018-02-12 00:54:10 -02:00
var conn *connection
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
func TestMain(m *testing.M) {
var err error
conn, err = NewConnection("amqp://guest:guest@broker:5672/")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if err != nil {
panic(err)
}
2017-06-15 10:05:00 -03:00
defer conn.Close()
2018-02-12 00:54:10 -02:00
os.Exit(m.Run())
}
func clearQueue(conn *connection, queueName string) error {
2018-02-10 14:38:29 -02:00
channel, err := conn.openChannel()
2018-02-12 00:54:10 -02:00
if err != nil {
return err
}
2018-02-10 14:38:29 -02:00
defer channel.Close()
2018-02-12 00:54:10 -02:00
// Clean all messages if any...
_, err = channel.QueuePurge(queueName, false)
return err
}
func TestSubscribeActions(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
2017-06-15 10:05:00 -03:00
c, err := NewConsumer(conn, false, "webhooks", "TestSubscribeActions")
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
c.Subscribe("my_action_1", func(e messaging.Event) error {
func1 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
c.Subscribe("my_action_2", func(e messaging.Event) error {
func2 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-10-10 17:49:53 -03:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
assert.Nil(t, err)
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
p.Publish("my_action_1", []byte(""))
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
select {
case <-func1:
case <-func2:
assert.Fail(t, "called wrong action")
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2017-10-10 17:49:53 -03:00
assert.Fail(t, "timed out")
}
2017-06-15 10:05:00 -03:00
}
}
func TestSubscribeActionsByBindAfterConsume(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
c, err := NewConsumer(conn, false, "webhooks", "TestSubscribeActionsByBindAfterConsume")
if assert.Nil(t, err) {
defer c.Close()
clearQueue(conn, c.queueName)
go c.Consume()
// take a time to setup topology
time.Sleep(SleepSetupTopology)
c.Subscribe("my_action_1", func(e messaging.Event) error {
func1 <- true
return nil
}, nil)
c.Subscribe("my_action_2", func(e messaging.Event) error {
func2 <- true
return nil
}, nil)
assert.NoError(t, c.BindActions("my_action_1", "my_action_2"))
p, err := NewProducer(conn, "webhooks")
assert.Nil(t, err)
p.Publish("my_action_1", []byte(""))
select {
case <-func1:
case <-func2:
assert.Fail(t, "called wrong action")
case <-time.After(3 * time.Second):
assert.Fail(t, "timed out")
}
}
}
func TestSubscribeActionsUnbindAfterConsume(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
c, err := NewConsumer(conn, false, "webhooks", "TestSubscribeActionsUnbindAfterConsume")
if assert.Nil(t, err) {
defer c.Close()
clearQueue(conn, c.queueName)
c.Subscribe("my_action_1", func(e messaging.Event) error {
func1 <- true
return nil
}, nil)
c.Subscribe("my_action_2", func(e messaging.Event) error {
func2 <- true
return nil
}, nil)
go c.Consume()
// take a time to setup topology
time.Sleep(SleepSetupTopology)
assert.NoError(t, c.UnbindActions("my_action_2"))
p, err := NewProducer(conn, "webhooks")
assert.Nil(t, err)
p.Publish("my_action_2", []byte(""))
select {
case <-func1:
assert.Fail(t, "called wrong action")
case <-func2:
assert.Fail(t, "called wrong action")
case <-time.After(3 * time.Second):
}
}
}
2017-06-15 10:05:00 -03:00
func TestSubscribeWildcardActions(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
2018-02-12 00:54:10 -02:00
c, err := NewConsumer(conn, false, "webhooks", "TestSubscribeWildcardActions")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
2018-02-12 00:54:10 -02:00
defer c.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("webinar.*", func(e messaging.Event) error {
func1 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("foobar.*", func(e messaging.Event) error {
func2 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2018-02-12 00:54:10 -02:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
assert.Nil(t, err)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("webinar.state_changed", []byte(""))
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
select {
case <-func1:
case <-func2:
assert.Fail(t, "called wrong action")
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2018-02-12 00:54:10 -02:00
assert.Fail(t, "timed out")
2017-10-10 17:49:53 -03:00
}
2017-06-15 10:05:00 -03:00
}
2018-02-12 00:54:10 -02:00
2017-06-15 10:05:00 -03:00
}
func TestSubscribeWildcardActionOrder1(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
2018-02-12 00:54:10 -02:00
c, err := NewConsumer(conn, false, "webhooks", "TestSubscribeWildcardActionOrder1")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
2018-02-12 00:54:10 -02:00
defer c.Close()
2018-02-10 14:38:29 -02:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("webinar.*", func(e messaging.Event) error {
func1 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("webinar.state_changed", func(e messaging.Event) error {
func2 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2018-02-12 00:54:10 -02:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
p.Publish("webinar.state_changed", []byte(""))
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
select {
case <-func1:
case <-func2:
assert.Fail(t, "called wrong action")
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2017-10-10 17:49:53 -03:00
assert.Fail(t, "timed out")
}
}
2017-06-15 10:05:00 -03:00
}
}
func TestSubscribeWildcardActionOrder2(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
2018-02-12 00:54:10 -02:00
c, err := NewConsumer(conn, false, "webhooks", "TestSubscribeWildcardActionOrder2")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
2018-02-12 00:54:10 -02:00
defer c.Close()
2018-02-10 14:38:29 -02:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("webinar.state_changed", func(e messaging.Event) error {
func1 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("webinar.*", func(e messaging.Event) error {
func2 <- true
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2018-02-12 00:54:10 -02:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
p.Publish("webinar.state_changed", []byte(""))
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
select {
case <-func1:
case <-func2:
assert.Fail(t, "called wrong action")
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2017-10-10 17:49:53 -03:00
assert.Fail(t, "timed out")
}
}
2017-06-15 10:05:00 -03:00
}
}
2017-06-22 16:33:59 -03:00
func TestDontRetryMessageIfFailsToProcess(t *testing.T) {
2017-06-15 10:05:00 -03:00
timesCalled := 0
2018-02-12 00:54:10 -02:00
c, err := NewConsumer(conn, false, "webhooks", "TestDontRetryMessageIfFailsToProcess")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
2018-02-12 00:54:10 -02:00
defer c.Close()
2018-02-10 14:38:29 -02:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
c.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled++ }()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if timesCalled == 0 {
return fmt.Errorf("Error")
}
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
return nil
}, nil)
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2018-02-12 00:54:10 -02:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
p.Publish("my_action", []byte(""))
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
select {
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2017-10-10 17:49:53 -03:00
assert.Equal(t, 1, timesCalled, "Consumer got wrong quantity of messages.")
}
}
2017-06-15 10:05:00 -03:00
}
}
2017-06-22 16:33:59 -03:00
func TestRetryMessageIfFailsToProcess(t *testing.T) {
2017-06-15 10:05:00 -03:00
timesCalled := 0
2017-06-22 16:33:59 -03:00
c, err := NewConsumer(conn, false, "webhooks", "TestRetryMessageIfFailsToProcess")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
c.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled++ }()
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if timesCalled == 0 {
return fmt.Errorf("Error")
}
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
return nil
}, &messaging.SubscribeOptions{
RetryDelay: 100 * time.Millisecond,
DelayedRetry: false,
MaxRetries: 5,
})
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-10-10 17:49:53 -03:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("my_action", []byte(""))
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
select {
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2018-02-12 00:54:10 -02:00
assert.True(t, timesCalled >= 1 || timesCalled <= 5, "Consumer got wrong quantity of messages.")
}
2017-10-10 17:49:53 -03:00
}
2017-06-15 10:05:00 -03:00
}
}
2017-06-22 16:33:59 -03:00
func TestRetryMessageIfPanicsToProcess(t *testing.T) {
2017-06-15 10:05:00 -03:00
timesCalled := 0
2017-06-22 16:33:59 -03:00
c, err := NewConsumer(conn, false, "webhooks", "TestRetryMessageIfPanicsToProcess")
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
c.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled++ }()
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
if timesCalled == 0 {
panic("this is a panic!")
}
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
return nil
}, &messaging.SubscribeOptions{
RetryDelay: 100 * time.Millisecond,
DelayedRetry: false,
MaxRetries: 5,
})
2017-06-15 10:05:00 -03:00
2017-10-10 17:49:53 -03:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-10-10 17:49:53 -03:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("my_action", []byte(""))
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
select {
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2018-02-12 00:54:10 -02:00
assert.Equal(t, 2, timesCalled, "Consumer got wrong quantity of messages.")
}
2017-10-10 17:49:53 -03:00
}
2017-06-15 10:05:00 -03:00
}
}
2017-06-22 16:33:59 -03:00
func TestRetryMessageToTheSameQueue(t *testing.T) {
2017-06-15 10:05:00 -03:00
timesCalled1 := 0
timesCalled2 := 0
2017-06-22 16:33:59 -03:00
c1, err := NewConsumer(conn, false, "webhooks", "TestRetryMessageToTheSameQueue_1")
2017-06-15 10:05:00 -03:00
assert.Nil(t, err)
2017-06-22 16:33:59 -03:00
c2, err := NewConsumer(conn, false, "webhooks", "TestRetryMessageToTheSameQueue_2")
2017-06-15 10:05:00 -03:00
assert.Nil(t, err)
defer c1.Close()
defer c2.Close()
2018-02-12 10:08:18 -02:00
clearQueue(conn, c1.queueName)
clearQueue(conn, c2.queueName)
2017-06-15 10:05:00 -03:00
2017-10-05 15:19:54 -03:00
c1.Subscribe("my_action", func(e messaging.Event) error {
2017-06-15 10:05:00 -03:00
timesCalled2++
return nil
2017-10-05 16:22:28 -03:00
}, nil)
c2.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled1++ }()
2017-06-15 10:05:00 -03:00
2017-10-05 16:22:28 -03:00
if timesCalled1 == 0 {
2020-10-21 19:41:11 -03:00
return fmt.Errorf("timescalled zero")
2017-10-05 16:22:28 -03:00
}
2020-10-21 19:41:11 -03:00
return nil
2017-10-05 16:22:28 -03:00
}, &messaging.SubscribeOptions{
2017-10-05 14:11:11 -03:00
RetryDelay: 100 * time.Millisecond,
DelayedRetry: false,
MaxRetries: 5,
})
2017-06-15 10:05:00 -03:00
go c1.Consume()
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-06-15 10:05:00 -03:00
go c2.Consume()
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-06-15 10:05:00 -03:00
p, err := NewProducer(conn, "webhooks")
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("my_action", []byte(""))
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
select {
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2018-02-12 00:54:10 -02:00
assert.Equal(t, 2, timesCalled1, "Consumer 1 got wrong quantity of messages.")
assert.Equal(t, 1, timesCalled2, "Consumer 2 got wrong quantity of messages.")
}
2017-06-15 10:05:00 -03:00
}
}
func TestActionExitsMaxRetries(t *testing.T) {
timesCalled := 0
c, err := NewConsumer(conn, false, "webhooks", "TestActionExitsMaxRetries")
assert.Nil(t, err)
defer c.Close()
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
// It runs once and get an error, it will try five times more until it stops.
2017-10-05 16:22:28 -03:00
c.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled++ }()
2020-10-21 19:41:11 -03:00
return fmt.Errorf("error")
2017-10-05 16:22:28 -03:00
}, &messaging.SubscribeOptions{
2017-10-05 14:11:11 -03:00
RetryDelay: 100 * time.Millisecond,
DelayedRetry: false,
MaxRetries: 5,
})
2017-06-15 10:05:00 -03:00
go c.Consume()
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-06-15 10:05:00 -03:00
p, err := NewProducer(conn, "webhooks")
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("my_action", []byte(""))
2017-06-15 10:05:00 -03:00
2018-02-12 00:54:10 -02:00
select {
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2018-02-12 00:54:10 -02:00
assert.True(t, timesCalled >= 4 || timesCalled <= 6, "Consumer got wrong quantity of messages.")
}
2017-06-15 10:05:00 -03:00
}
}
func TestActionExitsMaxRetriesWhenDelayed(t *testing.T) {
c, err := NewConsumer(conn, false, "webhooks", "TestActionExitsMaxRetriesWhenDelayed")
2018-02-12 10:08:18 -02:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
clearQueue(conn, c.queueName)
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
timesCalled := 0
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// It runs once and get an error, it will try three times more until it stops.
c.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled++ }()
2020-10-21 19:41:11 -03:00
return fmt.Errorf("error")
2018-02-12 10:08:18 -02:00
}, &messaging.SubscribeOptions{
RetryDelay: 100 * time.Millisecond,
DelayedRetry: true,
MaxRetries: 3,
})
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
go c.Consume()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 10:05:00 -03:00
2018-02-12 10:08:18 -02:00
p.Publish("my_action", []byte(""))
select {
case <-time.After(3 * time.Second):
assert.True(t, timesCalled > 1 || timesCalled <= 4, "Consumer got wrong quantity of messages.")
}
2018-02-12 00:54:10 -02:00
}
2017-06-15 10:05:00 -03:00
}
}
2017-06-15 12:07:50 -03:00
2017-06-22 16:33:59 -03:00
func TestActionExitsMaxRetriesWhenDelayedWindow(t *testing.T) {
timesCalled := 0
c, err := NewConsumer(conn, false, "webhooks", "TestActionExitsMaxRetriesWhenDelayed")
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-22 16:33:59 -03:00
2017-10-10 17:49:53 -03:00
// It runs once and get an error, it will try three times more until it stops.
c.Subscribe("my_action", func(e messaging.Event) error {
defer func() { timesCalled++ }()
2020-10-21 19:41:11 -03:00
return fmt.Errorf("error")
2017-10-10 17:49:53 -03:00
}, &messaging.SubscribeOptions{
RetryDelay: 100 * time.Millisecond,
DelayedRetry: true,
MaxRetries: 5,
})
2017-06-22 16:33:59 -03:00
2017-10-10 17:49:53 -03:00
go c.Consume()
2017-06-22 16:33:59 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-10-10 17:49:53 -03:00
p, err := NewProducer(conn, "webhooks")
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("my_action", []byte(""))
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
select {
2018-02-12 10:08:18 -02:00
case <-time.After(3 * time.Second):
2018-02-12 00:54:10 -02:00
assert.True(t, timesCalled > 1 || timesCalled <= 6, "Consumer got wrong quantity of messages.")
}
2017-10-10 17:49:53 -03:00
}
2017-06-22 16:33:59 -03:00
}
}
func TestActionRetryTimeout(t *testing.T) {
myActionTimesCalled := 0
myAction2TimesCalled := 0
c, err := NewConsumerConfig(conn, false, "webhooks", "TestActionRetryTimeout", ConsumerConfig{
2017-10-10 19:49:27 -03:00
ConsumeRetryInterval: 2 * time.Second,
PrefetchCount: 1,
2017-06-22 16:33:59 -03:00
})
2017-10-10 17:49:53 -03:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
clearQueue(conn, c.queueName)
2017-06-22 16:33:59 -03:00
2017-10-10 17:49:53 -03:00
c.Subscribe("test1", func(e messaging.Event) error {
defer func() {
myActionTimesCalled++
}()
2020-10-21 19:41:11 -03:00
return fmt.Errorf("error")
2017-10-10 17:49:53 -03:00
}, &messaging.SubscribeOptions{
RetryDelay: 300 * time.Millisecond,
DelayedRetry: true,
MaxRetries: 4,
})
c.Subscribe("test2", func(e messaging.Event) error {
defer func() {
myAction2TimesCalled++
}()
return nil
}, nil)
2017-06-22 16:33:59 -03:00
2017-10-10 17:49:53 -03:00
go c.Consume()
2017-06-22 16:33:59 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-10-10 17:49:53 -03:00
p, err := NewProducer(conn, "webhooks")
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
p.Publish("test1", []byte(""))
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
time.Sleep(200 * time.Millisecond)
p.Publish("test2", []byte(""))
2017-06-22 16:33:59 -03:00
2018-02-12 00:54:10 -02:00
select {
case <-time.After(1 * time.Second):
assert.True(t, myActionTimesCalled > 1 || myActionTimesCalled <= 4, "Consumer got wrong quantity of messages.")
assert.Equal(t, 1, myAction2TimesCalled, "Consumer got wrong quantity of messages.")
}
2017-10-10 17:49:53 -03:00
}
2017-06-22 16:33:59 -03:00
}
}
2017-06-15 16:28:08 -03:00
func TestConsumePrefetch(t *testing.T) {
2017-06-15 12:07:50 -03:00
timesCalled := 0
wait := make(chan bool)
2017-06-15 16:28:08 -03:00
c, err := NewConsumerConfig(conn, false, "webhooks", "TestConsumePrefetch", ConsumerConfig{
PrefetchCount: 5,
2017-06-15 12:07:50 -03:00
ConsumeRetryInterval: 15 * time.Second,
})
2018-02-12 10:08:18 -02:00
if assert.Nil(t, err) {
defer c.Close()
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
clearQueue(conn, c.queueName)
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
c.Subscribe("my_action", func(e messaging.Event) error {
timesCalled++
<-wait
return nil
}, nil)
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
go c.Consume()
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
p, err := NewProducer(conn, "webhooks")
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
for i := 0; i < 10; i++ {
p.Publish("my_action", []byte(""))
}
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
<-time.After(100 * time.Millisecond)
assert.Equal(t, 5, timesCalled, "Consumer got wrong quantity of messages.")
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
// release one
wait <- true
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
<-time.After(100 * time.Millisecond)
assert.Equal(t, 6, timesCalled, "Consumer got wrong quantity of messages.")
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
// release all
for i := 0; i < 5; i++ {
wait <- true
}
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
<-time.After(100 * time.Millisecond)
assert.Equal(t, 10, timesCalled, "Consumer got wrong quantity of messages.")
2017-06-15 12:07:50 -03:00
2018-02-12 10:08:18 -02:00
// release all
for i := 0; i < 4; i++ {
wait <- true
}
}
2017-06-15 12:07:50 -03:00
}
}
2017-12-19 12:02:09 -02:00
func TestBlankQueueWithPrefix(t *testing.T) {
c, err := NewConsumerConfig(conn, false, "webhooks", "", ConsumerConfig{
ConsumeRetryInterval: 2 * time.Second,
2018-02-12 00:54:10 -02:00
PrefetchCount: 0,
PrefixName: "teste@",
2017-12-19 12:02:09 -02:00
})
if assert.Nil(t, err) {
defer c.Close()
2018-02-12 00:54:10 -02:00
fmt.Println(c.queueName)
2017-12-19 12:02:09 -02:00
2018-02-12 00:54:10 -02:00
wait := make(chan bool)
myActionTimesCalled := 0
c.Subscribe("TestBlankQueueWithPrefix", func(e messaging.Event) error {
myActionTimesCalled++
wait <- true
2017-12-19 12:02:09 -02:00
return nil
2018-02-12 00:54:10 -02:00
}, &messaging.SubscribeOptions{
RetryDelay: 100 * time.Millisecond,
DelayedRetry: true,
MaxRetries: 3,
})
2017-12-19 12:02:09 -02:00
go c.Consume()
2018-02-12 10:08:18 -02:00
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2017-12-19 12:02:09 -02:00
p, err := NewProducer(conn, "webhooks")
2018-02-12 00:54:10 -02:00
if assert.Nil(t, err) {
defer p.Close()
2017-12-19 12:02:09 -02:00
2018-02-12 00:54:10 -02:00
p.Publish("TestBlankQueueWithPrefix", []byte(""))
2017-12-19 12:02:09 -02:00
2018-02-12 00:54:10 -02:00
<-wait
2017-12-19 12:02:09 -02:00
assert.Equal(t, 1, myActionTimesCalled, "Consumer got wrong quantity of messages.")
}
}
}
func TestCallEventAckMethod(t *testing.T) {
func1 := make(chan bool)
func2 := make(chan bool)
2019-06-24 18:56:34 -03:00
c, err := NewConsumer(conn, false, "multi", "TestCallEventAckMethod")
if assert.Nil(t, err) {
defer c.Close()
clearQueue(conn, c.queueName)
c.Subscribe("multi", func(e messaging.Event) error {
2019-06-24 18:56:34 -03:00
e.Manual()
e.Ack(false)
func1 <- true
return nil
}, nil)
2019-06-24 18:56:34 -03:00
c.Subscribe("multi_2", func(e messaging.Event) error {
e.Manual()
e.Ack(false)
func2 <- true
return nil
}, nil)
go c.Consume()
// take a time to setup topology
time.Sleep(SleepSetupTopology)
p, err := NewProducer(conn, "multi")
assert.Nil(t, err)
p.Publish("multi", []byte(""))
select {
case <-func1:
case <-func2:
assert.Fail(t, "called wrong action")
case <-time.After(3 * time.Second):
assert.Fail(t, "timed out")
}
}
}
func TestCallEventNackMethod(t *testing.T) {
2019-06-24 18:56:34 -03:00
c, err := NewConsumer(conn, false, "onlynack", "TestCallEventNackMethod")
if assert.Nil(t, err) {
count := 0
defer c.Close()
clearQueue(conn, c.queueName)
c.Subscribe("multi", func(e messaging.Event) error {
2019-06-24 18:56:34 -03:00
e.Manual()
2020-10-21 19:41:11 -03:00
count++
if count == 3 {
e.Ack(false)
} else {
e.Nack(false, true)
}
return nil
}, nil)
go c.Consume()
// take a time to setup topology
time.Sleep(SleepSetupTopology)
2019-06-24 18:56:34 -03:00
p, err := NewProducer(conn, "onlynack")
assert.Nil(t, err)
p.Publish("multi", []byte(""))
// Wait for requeue
time.Sleep(10 * time.Second)
assert.Equal(t, 3, count)
}
}