6
0
mirror of https://git.mills.io/prologic/msgbus.git synced 2024-06-16 11:59:03 +00:00
prologic-msgbus/queue_test.go
James Mills dd992ab5d8
Added configurable bounded queues with a deque data structure with added metrics (#6)
Adds configurable bounded queues with a deque data structure with added metrics
2018-05-09 23:25:13 -07:00

96 lines
1.4 KiB
Go

package msgbus
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmpty(t *testing.T) {
q := Queue{}
assert.Zero(t, q.Len())
assert.True(t, q.Empty())
}
func TestSimple(t *testing.T) {
assert := assert.New(t)
q := Queue{}
for i := 0; i < minCapacity; i++ {
q.Push(i)
}
for i := 0; i < minCapacity; i++ {
assert.Equal(q.Peek(), i)
assert.Equal(q.Pop(), i)
}
}
func TestMaxLen(t *testing.T) {
q := Queue{maxlen: minCapacity}
assert.Equal(t, q.MaxLen(), minCapacity)
}
func TestFull(t *testing.T) {
q := Queue{maxlen: minCapacity}
for i := 0; i < minCapacity; i++ {
q.Push(i)
}
assert.True(t, q.Full())
}
func TestBufferWrap(t *testing.T) {
q := Queue{}
for i := 0; i < minCapacity; i++ {
q.Push(i)
}
for i := 0; i < 3; i++ {
q.Pop()
q.Push(minCapacity + i)
}
for i := 0; i < minCapacity; i++ {
assert.Equal(t, q.Peek().(int), i+3)
q.Pop()
}
}
func TestLen(t *testing.T) {
assert := assert.New(t)
q := Queue{}
assert.Zero(q.Len())
for i := 0; i < 1000; i++ {
q.Push(i)
assert.Equal(q.Len(), i+1)
}
for i := 0; i < 1000; i++ {
q.Pop()
assert.Equal(q.Len(), 1000-i-1)
}
}
func BenchmarkPush(b *testing.B) {
q := Queue{}
for i := 0; i < b.N; i++ {
q.Push(i)
}
}
func BenchmarkPushPop(b *testing.B) {
q := Queue{}
for i := 0; i < b.N; i++ {
q.Push(i)
}
for i := 0; i < b.N; i++ {
q.Pop()
}
}