partitioner_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package sarama
  2. import (
  3. "crypto/rand"
  4. "testing"
  5. )
  6. func assertPartitioningConsistent(t *testing.T, partitioner Partitioner, key Encoder, numPartitions int32) {
  7. choice, err := partitioner.Partition(key, numPartitions)
  8. if err != nil {
  9. t.Error(partitioner, err)
  10. }
  11. if choice < 0 || choice >= numPartitions {
  12. t.Error(partitioner, "returned partition", choice, "outside of range for", key)
  13. }
  14. for i := 1; i < 50; i++ {
  15. newChoice, err := partitioner.Partition(key, numPartitions)
  16. if err != nil {
  17. t.Error(partitioner, err)
  18. }
  19. if newChoice != choice {
  20. t.Error(partitioner, "returned partition", newChoice, "inconsistent with", choice, ".")
  21. }
  22. }
  23. }
  24. func TestRandomPartitioner(t *testing.T) {
  25. partitioner := NewRandomPartitioner()
  26. choice, err := partitioner.Partition(nil, 1)
  27. if err != nil {
  28. t.Error(partitioner, err)
  29. }
  30. if choice != 0 {
  31. t.Error("Returned non-zero partition when only one available.")
  32. }
  33. for i := 1; i < 50; i++ {
  34. choice, err := partitioner.Partition(nil, 50)
  35. if err != nil {
  36. t.Error(partitioner, err)
  37. }
  38. if choice < 0 || choice >= 50 {
  39. t.Error("Returned partition", choice, "outside of range.")
  40. }
  41. }
  42. }
  43. func TestRoundRobinPartitioner(t *testing.T) {
  44. partitioner := RoundRobinPartitioner{}
  45. choice, err := partitioner.Partition(nil, 1)
  46. if err != nil {
  47. t.Error(partitioner, err)
  48. }
  49. if choice != 0 {
  50. t.Error("Returned non-zero partition when only one available.")
  51. }
  52. var i int32
  53. for i = 1; i < 50; i++ {
  54. choice, err := partitioner.Partition(nil, 7)
  55. if err != nil {
  56. t.Error(partitioner, err)
  57. }
  58. if choice != i%7 {
  59. t.Error("Returned partition", choice, "expecting", i%7)
  60. }
  61. }
  62. }
  63. func TestHashPartitioner(t *testing.T) {
  64. partitioner := NewHashPartitioner()
  65. choice, err := partitioner.Partition(nil, 1)
  66. if err != nil {
  67. t.Error(partitioner, err)
  68. }
  69. if choice != 0 {
  70. t.Error("Returned non-zero partition when only one available.")
  71. }
  72. for i := 1; i < 50; i++ {
  73. choice, err := partitioner.Partition(nil, 50)
  74. if err != nil {
  75. t.Error(partitioner, err)
  76. }
  77. if choice < 0 || choice >= 50 {
  78. t.Error("Returned partition", choice, "outside of range for nil key.")
  79. }
  80. }
  81. buf := make([]byte, 256)
  82. for i := 1; i < 50; i++ {
  83. rand.Read(buf)
  84. assertPartitioningConsistent(t, partitioner, ByteEncoder(buf), 50)
  85. }
  86. }
  87. func TestConstantPartitioner(t *testing.T) {
  88. var partitioner Partitioner
  89. partitioner = &ConstantPartitioner{Constant: 0}
  90. for i := 1; i < 50; i++ {
  91. choice, err := partitioner.Partition(nil, 50)
  92. if err != nil {
  93. t.Error(partitioner, err)
  94. }
  95. if choice != 0 {
  96. t.Error("Returned partition", choice, "instead of 0.")
  97. }
  98. }
  99. }