6
0
mirror of https://git.mills.io/prologic/msgbus.git synced 2024-07-03 00:34:16 +00:00
prologic-msgbus/msgbus_test.go

79 lines
1.4 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) {
mb := NewMessageBus()
assert.Equal(t, mb.Len(), 0)
}
func TestMessage(t *testing.T) {
mb := NewMessageBus()
assert.Equal(t, mb.Len(), 0)
topic := "foo"
expected := &Message{Payload: []byte("bar")}
mb.Put(topic, expected)
actual, ok := mb.Get(topic)
assert.True(t, ok)
assert.Equal(t, actual, expected)
}
func TestMessageGetEmpty(t *testing.T) {
mb := NewMessageBus()
assert.Equal(t, mb.Len(), 0)
topic := "foo"
msg, ok := mb.Get(topic)
assert.False(t, ok)
assert.Equal(t, msg, &Message{})
}
func BenchmarkMessageBusPut(b *testing.B) {
mb := NewMessageBus()
topic := "foo"
msg := &Message{Payload: []byte("foo")}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mb.Put(topic, msg)
}
}
func BenchmarkMessageBusGet(b *testing.B) {
mb := NewMessageBus()
topic := "foo"
msg := &Message{Payload: []byte("foo")}
for i := 0; i < b.N; i++ {
mb.Put(topic, msg)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mb.Get(topic)
}
}
func BenchmarkMessageBusGetEmpty(b *testing.B) {
mb := NewMessageBus()
topic := "foo"
b.ResetTimer()
for i := 0; i < b.N; i++ {
mb.Get(topic)
}
}
func BenchmarkMessageBusPutGet(b *testing.B) {
mb := NewMessageBus()
topic := "foo"
msg := &Message{Payload: []byte("foo")}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mb.Put(topic, msg)
mb.Get(topic)
}
}