You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1009 B
Go
44 lines
1009 B
Go
package queue
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBalancedQueuePusher(t *testing.T) {
|
|
const numPushers = 100
|
|
var pushers []QueuePusher
|
|
var mockedPushers []*mockedPusher
|
|
for i := 0; i < numPushers; i++ {
|
|
p := &mockedPusher{
|
|
name: "pusher:" + strconv.Itoa(i),
|
|
}
|
|
pushers = append(pushers, p)
|
|
mockedPushers = append(mockedPushers, p)
|
|
}
|
|
|
|
pusher := NewBalancedQueuePusher(pushers)
|
|
assert.True(t, len(pusher.Name()) > 0)
|
|
|
|
for i := 0; i < numPushers*1000; i++ {
|
|
assert.Nil(t, pusher.Push("item"))
|
|
}
|
|
|
|
var counts []int
|
|
for _, p := range mockedPushers {
|
|
counts = append(counts, p.count)
|
|
}
|
|
mean := calcMean(counts)
|
|
variance := calcVariance(mean, counts)
|
|
assert.True(t, variance < 100, fmt.Sprintf("too big variance - %.2f", variance))
|
|
}
|
|
|
|
func TestBalancedQueuePusher_NoAvailable(t *testing.T) {
|
|
pusher := NewBalancedQueuePusher(nil)
|
|
assert.True(t, len(pusher.Name()) == 0)
|
|
assert.Equal(t, ErrNoAvailablePusher, pusher.Push("item"))
|
|
}
|