balance_strategy_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package sarama
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestBalanceStrategyRange(t *testing.T) {
  7. tests := []struct {
  8. members map[string][]string
  9. topics map[string][]int32
  10. expected BalanceStrategyPlan
  11. }{
  12. {
  13. members: map[string][]string{"M1": {"T1", "T2"}, "M2": {"T1", "T2"}},
  14. topics: map[string][]int32{"T1": {0, 1, 2, 3}, "T2": {0, 1, 2, 3}},
  15. expected: BalanceStrategyPlan{
  16. "M1": map[string][]int32{"T1": {0, 1}, "T2": {2, 3}},
  17. "M2": map[string][]int32{"T1": {2, 3}, "T2": {0, 1}},
  18. },
  19. },
  20. {
  21. members: map[string][]string{"M1": {"T1", "T2"}, "M2": {"T1", "T2"}},
  22. topics: map[string][]int32{"T1": {0, 1, 2}, "T2": {0, 1, 2}},
  23. expected: BalanceStrategyPlan{
  24. "M1": map[string][]int32{"T1": {0, 1}, "T2": {2}},
  25. "M2": map[string][]int32{"T1": {2}, "T2": {0, 1}},
  26. },
  27. },
  28. {
  29. members: map[string][]string{"M1": {"T1"}, "M2": {"T1", "T2"}},
  30. topics: map[string][]int32{"T1": {0, 1}, "T2": {0, 1}},
  31. expected: BalanceStrategyPlan{
  32. "M1": map[string][]int32{"T1": {0}},
  33. "M2": map[string][]int32{"T1": {1}, "T2": {0, 1}},
  34. },
  35. },
  36. }
  37. strategy := BalanceStrategyRange
  38. if strategy.Name() != "range" {
  39. t.Errorf("Unexpected stategy name\nexpected: range\nactual: %v", strategy.Name())
  40. }
  41. for _, test := range tests {
  42. members := make(map[string]ConsumerGroupMemberMetadata)
  43. for memberID, topics := range test.members {
  44. members[memberID] = ConsumerGroupMemberMetadata{Topics: topics}
  45. }
  46. actual, err := strategy.Plan(members, test.topics)
  47. if err != nil {
  48. t.Errorf("Unexpected error %v", err)
  49. } else if !reflect.DeepEqual(actual, test.expected) {
  50. t.Errorf("Plan does not match expectation\nexpected: %#v\nactual: %#v", test.expected, actual)
  51. }
  52. }
  53. }
  54. func TestBalanceStrategyRoundRobin(t *testing.T) {
  55. tests := []struct {
  56. members map[string][]string
  57. topics map[string][]int32
  58. expected BalanceStrategyPlan
  59. }{
  60. {
  61. members: map[string][]string{"M1": {"T1", "T2"}, "M2": {"T1", "T2"}},
  62. topics: map[string][]int32{"T1": {0, 1, 2, 3}, "T2": {0, 1, 2, 3}},
  63. expected: BalanceStrategyPlan{
  64. "M1": map[string][]int32{"T1": {0, 2}, "T2": {1, 3}},
  65. "M2": map[string][]int32{"T1": {1, 3}, "T2": {0, 2}},
  66. },
  67. },
  68. {
  69. members: map[string][]string{"M1": {"T1", "T2"}, "M2": {"T1", "T2"}},
  70. topics: map[string][]int32{"T1": {0, 1, 2}, "T2": {0, 1, 2}},
  71. expected: BalanceStrategyPlan{
  72. "M1": map[string][]int32{"T1": {0, 2}, "T2": {1}},
  73. "M2": map[string][]int32{"T1": {1}, "T2": {0, 2}},
  74. },
  75. },
  76. }
  77. strategy := BalanceStrategyRoundRobin
  78. if strategy.Name() != "roundrobin" {
  79. t.Errorf("Unexpected stategy name\nexpected: range\nactual: %v", strategy.Name())
  80. }
  81. for _, test := range tests {
  82. members := make(map[string]ConsumerGroupMemberMetadata)
  83. for memberID, topics := range test.members {
  84. members[memberID] = ConsumerGroupMemberMetadata{Topics: topics}
  85. }
  86. actual, err := strategy.Plan(members, test.topics)
  87. if err != nil {
  88. t.Errorf("Unexpected error %v", err)
  89. } else if !reflect.DeepEqual(actual, test.expected) {
  90. t.Errorf("Plan does not match expectation\nexpected: %#v\nactual: %#v", test.expected, actual)
  91. }
  92. }
  93. }