| 64 | } |
| 65 | |
| 66 | func TestReferenceHashBalancer(t *testing.T) { |
| 67 | testCases := map[string]struct { |
| 68 | Key []byte |
| 69 | Hasher hash.Hash32 |
| 70 | Partitions []int |
| 71 | Partition int |
| 72 | RndBalancerResult int |
| 73 | }{ |
| 74 | "nil": { |
| 75 | Key: nil, // nil key means random partition |
| 76 | Partitions: []int{0, 1, 2}, |
| 77 | Partition: 123, |
| 78 | RndBalancerResult: 123, |
| 79 | }, |
| 80 | "partition-0": { |
| 81 | Key: []byte("blah"), |
| 82 | Partitions: []int{0, 1}, |
| 83 | Partition: 0, |
| 84 | }, |
| 85 | "partition-1": { |
| 86 | Key: []byte("blah"), |
| 87 | Partitions: []int{0, 1, 2}, |
| 88 | Partition: 1, |
| 89 | }, |
| 90 | "partition-2": { |
| 91 | Key: []byte("castle"), |
| 92 | Partitions: []int{0, 1, 2}, |
| 93 | Partition: 2, |
| 94 | }, |
| 95 | "custom hash": { |
| 96 | Key: []byte("boop"), |
| 97 | Hasher: crc32.NewIEEE(), |
| 98 | Partitions: []int{0, 1, 2}, |
| 99 | Partition: 1, |
| 100 | }, |
| 101 | "hash code with MSB set": { |
| 102 | Key: []byte("20"), |
| 103 | Partitions: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
| 104 | Partition: 15, |
| 105 | }, |
| 106 | } |
| 107 | |
| 108 | for label, test := range testCases { |
| 109 | t.Run(label, func(t *testing.T) { |
| 110 | var rr randomBalancer |
| 111 | if test.Key == nil { |
| 112 | rr.mock = test.RndBalancerResult |
| 113 | } |
| 114 | |
| 115 | msg := Message{Key: test.Key} |
| 116 | h := ReferenceHash{Hasher: test.Hasher, rr: rr} |
| 117 | partition := h.Balance(msg, test.Partitions...) |
| 118 | if partition != test.Partition { |
| 119 | t.Errorf("expected %v; got %v", test.Partition, partition) |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | } |