mirror of
https://github.com/eventials/goevents.git
synced 2025-04-26 13:48:59 +08:00
Add tests.
This commit is contained in:
parent
3bae7283b0
commit
9736a24867
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package events
|
||||
|
||||
import (
|
||||
"github.com/streadway/amqp"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package events
|
||||
|
||||
import (
|
||||
"github.com/streadway/amqp"
|
||||
|
256
integration_test.go
Normal file
256
integration_test.go
Normal file
@ -0,0 +1,256 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPublishConsume(t *testing.T) {
|
||||
func1 := make(chan bool)
|
||||
func2 := make(chan bool)
|
||||
|
||||
conn, err := NewConnection("amqp://guest:guest@broker:5672/", "event_PublishConsumeer", "webhooks")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
// Clean all messages if any...
|
||||
conn.channel.QueuePurge(conn.queueName, false)
|
||||
|
||||
c := NewConsumer(conn, false)
|
||||
|
||||
c.Subscribe("my_action_1", func(body []byte) bool {
|
||||
func1 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Subscribe("my_action_2", func(body []byte) bool {
|
||||
func2 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Listen()
|
||||
|
||||
p := NewProducer(conn)
|
||||
|
||||
err = p.Publish("my_action_1", []byte(""))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
select {
|
||||
case <-func1:
|
||||
case <-func2:
|
||||
assert.Fail(t, "called wrong action")
|
||||
case <-time.After(5 * time.Second):
|
||||
assert.Fail(t, "timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishConsumeWildcardAction(t *testing.T) {
|
||||
func1 := make(chan bool)
|
||||
func2 := make(chan bool)
|
||||
|
||||
conn, err := NewConnection("amqp://guest:guest@broker:5672/", "event_PublishConsumeer", "webhooks")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
// Clean all messages if any...
|
||||
conn.channel.QueuePurge(conn.queueName, false)
|
||||
|
||||
c := NewConsumer(conn, false)
|
||||
|
||||
c.Subscribe("webinar.*", func(body []byte) bool {
|
||||
func1 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Subscribe("foobar.*", func(body []byte) bool {
|
||||
func2 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Listen()
|
||||
|
||||
p := NewProducer(conn)
|
||||
|
||||
err = p.Publish("webinar.state_changed", []byte(""))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
select {
|
||||
case <-func1:
|
||||
case <-func2:
|
||||
assert.Fail(t, "called wrong action")
|
||||
case <-time.After(5 * time.Second):
|
||||
assert.Fail(t, "timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishConsumeWildcardActionOrderMatters1(t *testing.T) {
|
||||
func1 := make(chan bool)
|
||||
func2 := make(chan bool)
|
||||
|
||||
conn, err := NewConnection("amqp://guest:guest@broker:5672/", "event_PublishConsumeer", "webhooks")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
// Clean all messages if any...
|
||||
conn.channel.QueuePurge(conn.queueName, false)
|
||||
|
||||
c := NewConsumer(conn, false)
|
||||
|
||||
c.Subscribe("webinar.*", func(body []byte) bool {
|
||||
func1 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Subscribe("webinar.state_changed", func(body []byte) bool {
|
||||
func2 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Listen()
|
||||
|
||||
p := NewProducer(conn)
|
||||
|
||||
err = p.Publish("webinar.state_changed", []byte(""))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
select {
|
||||
case <-func1:
|
||||
case <-func2:
|
||||
assert.Fail(t, "called wrong action")
|
||||
case <-time.After(5 * time.Second):
|
||||
assert.Fail(t, "timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishConsumeWildcardActionOrderMatters2(t *testing.T) {
|
||||
func1 := make(chan bool)
|
||||
func2 := make(chan bool)
|
||||
|
||||
conn, err := NewConnection("amqp://guest:guest@broker:5672/", "event_PublishConsumeer", "webhooks")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
// Clean all messages if any...
|
||||
conn.channel.QueuePurge(conn.queueName, false)
|
||||
|
||||
c := NewConsumer(conn, false)
|
||||
|
||||
c.Subscribe("webinar.state_changed", func(body []byte) bool {
|
||||
func1 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Subscribe("webinar.*", func(body []byte) bool {
|
||||
func2 <- true
|
||||
return true
|
||||
})
|
||||
|
||||
c.Listen()
|
||||
|
||||
p := NewProducer(conn)
|
||||
|
||||
err = p.Publish("webinar.state_changed", []byte(""))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
select {
|
||||
case <-func1:
|
||||
case <-func2:
|
||||
assert.Fail(t, "called wrong action")
|
||||
case <-time.After(5 * time.Second):
|
||||
assert.Fail(t, "timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishConsumeRequeueIfFail(t *testing.T) {
|
||||
calledOnce := false
|
||||
called := make(chan bool)
|
||||
|
||||
conn, err := NewConnection("amqp://guest:guest@broker:5672/", "event_PublishConsumeer", "webhooks")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
// Clean all messages if any...
|
||||
conn.channel.QueuePurge(conn.queueName, false)
|
||||
|
||||
c := NewConsumer(conn, false)
|
||||
|
||||
c.Subscribe("my_action", func(body []byte) bool {
|
||||
if calledOnce {
|
||||
called <- true
|
||||
return true
|
||||
} else {
|
||||
calledOnce = true
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
c.Listen()
|
||||
|
||||
p := NewProducer(conn)
|
||||
|
||||
err = p.Publish("my_action", []byte(""))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
select {
|
||||
case <-called:
|
||||
case <-time.After(5 * time.Second):
|
||||
assert.Fail(t, "timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishConsumeRequeueIfPanic(t *testing.T) {
|
||||
calledOnce := false
|
||||
called := make(chan bool)
|
||||
|
||||
conn, err := NewConnection("amqp://guest:guest@broker:5672/", "event_PublishConsumeer", "webhooks")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
// Clean all messages if any...
|
||||
conn.channel.QueuePurge(conn.queueName, false)
|
||||
|
||||
c := NewConsumer(conn, false)
|
||||
|
||||
c.Subscribe("my_action", func(body []byte) bool {
|
||||
if calledOnce {
|
||||
called <- true
|
||||
return true
|
||||
} else {
|
||||
calledOnce = true
|
||||
panic("this is a panic!")
|
||||
}
|
||||
})
|
||||
|
||||
c.Listen()
|
||||
|
||||
p := NewProducer(conn)
|
||||
|
||||
err = p.Publish("my_action", []byte(""))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
select {
|
||||
case <-called:
|
||||
case <-time.After(5 * time.Second):
|
||||
assert.Fail(t, "timed out")
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package events
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
Loading…
x
Reference in New Issue
Block a user