balancedpusher_test.go 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package queue
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestBalancedQueuePusher(t *testing.T) {
  9. const numPushers = 100
  10. var pushers []Pusher
  11. var mockedPushers []*mockedPusher
  12. for i := 0; i < numPushers; i++ {
  13. p := &mockedPusher{
  14. name: "pusher:" + strconv.Itoa(i),
  15. }
  16. pushers = append(pushers, p)
  17. mockedPushers = append(mockedPushers, p)
  18. }
  19. pusher := NewBalancedPusher(pushers)
  20. assert.True(t, len(pusher.Name()) > 0)
  21. for i := 0; i < numPushers*1000; i++ {
  22. assert.Nil(t, pusher.Push("item"))
  23. }
  24. var counts []int
  25. for _, p := range mockedPushers {
  26. counts = append(counts, p.count)
  27. }
  28. mean := calcMean(counts)
  29. variance := calcVariance(mean, counts)
  30. assert.True(t, variance < 100, fmt.Sprintf("too big variance - %.2f", variance))
  31. }
  32. func TestBalancedQueuePusher_NoAvailable(t *testing.T) {
  33. pusher := NewBalancedPusher(nil)
  34. assert.True(t, len(pusher.Name()) == 0)
  35. assert.Equal(t, ErrNoAvailablePusher, pusher.Push("item"))
  36. }