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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|