6
0
mirror of https://git.mills.io/prologic/msgbus.git synced 2024-06-30 18:51:44 +00:00
prologic-msgbus/msgbus_test.go

90 lines
1.8 KiB
Go
Raw Normal View History

2017-06-03 15:16:17 +00:00
package msgbus
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMessageBusLen(t *testing.T) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
2017-06-03 15:16:17 +00:00
assert.Equal(t, mb.Len(), 0)
}
func TestMessage(t *testing.T) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
2017-06-03 15:16:17 +00:00
assert.Equal(t, mb.Len(), 0)
2017-08-20 01:26:15 +00:00
topic := mb.NewTopic("foo")
2017-08-10 07:30:45 +00:00
expected := Message{Topic: topic, Payload: []byte("bar")}
2017-08-20 01:26:15 +00:00
mb.Put(expected)
2017-06-03 15:16:17 +00:00
actual, ok := mb.Get(topic)
assert.True(t, ok)
assert.Equal(t, actual, expected)
}
func TestMessageGetEmpty(t *testing.T) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
2017-06-03 15:16:17 +00:00
assert.Equal(t, mb.Len(), 0)
2017-08-20 01:26:15 +00:00
topic := mb.NewTopic("foo")
2017-06-03 15:16:17 +00:00
msg, ok := mb.Get(topic)
assert.False(t, ok)
2017-08-10 07:30:45 +00:00
assert.Equal(t, msg, Message{})
2017-06-03 15:16:17 +00:00
}
2018-04-07 06:34:28 +00:00
func TestMessageBusPutGet(t *testing.T) {
mb := NewMessageBus(nil)
topic := mb.NewTopic("foo")
expected := Message{Topic: topic, Payload: []byte("foo")}
mb.Put(expected)
actual, ok := mb.Get(topic)
assert.True(t, ok)
assert.Equal(t, actual, expected)
}
2017-06-03 15:16:17 +00:00
func BenchmarkMessageBusPut(b *testing.B) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
topic := mb.NewTopic("foo")
msg := Message{Topic: topic, Payload: []byte("foo")}
2017-06-03 15:16:17 +00:00
b.ResetTimer()
for i := 0; i < b.N; i++ {
2017-08-20 01:26:15 +00:00
mb.Put(msg)
2017-06-03 15:16:17 +00:00
}
}
func BenchmarkMessageBusGet(b *testing.B) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
topic := mb.NewTopic("foo")
2018-03-03 19:42:50 +00:00
msg := Message{Topic: topic, Payload: []byte("foo")}
2017-06-03 15:16:17 +00:00
for i := 0; i < b.N; i++ {
2017-08-20 01:26:15 +00:00
mb.Put(msg)
2017-06-03 15:16:17 +00:00
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mb.Get(topic)
}
}
func BenchmarkMessageBusGetEmpty(b *testing.B) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
topic := mb.NewTopic("foo")
2017-06-03 15:16:17 +00:00
b.ResetTimer()
for i := 0; i < b.N; i++ {
mb.Get(topic)
}
}
func BenchmarkMessageBusPutGet(b *testing.B) {
2017-08-20 01:26:15 +00:00
mb := NewMessageBus(nil)
topic := mb.NewTopic("foo")
2018-03-03 19:42:50 +00:00
msg := Message{Topic: topic, Payload: []byte("foo")}
2017-06-03 15:16:17 +00:00
b.ResetTimer()
for i := 0; i < b.N; i++ {
2017-08-20 01:26:15 +00:00
mb.Put(msg)
2017-06-03 15:16:17 +00:00
mb.Get(topic)
}
}