| 8 | ) |
| 9 | |
| 10 | func TestHashBalancer(t *testing.T) { |
| 11 | testCases := map[string]struct { |
| 12 | Key []byte |
| 13 | Hasher hash.Hash32 |
| 14 | Partitions []int |
| 15 | Partition int |
| 16 | }{ |
| 17 | "nil": { |
| 18 | Key: nil, |
| 19 | Partitions: []int{0, 1, 2}, |
| 20 | Partition: 0, |
| 21 | }, |
| 22 | "partition-0": { |
| 23 | Key: []byte("blah"), |
| 24 | Partitions: []int{0, 1}, |
| 25 | Partition: 0, |
| 26 | }, |
| 27 | "partition-1": { |
| 28 | Key: []byte("blah"), |
| 29 | Partitions: []int{0, 1, 2}, |
| 30 | Partition: 1, |
| 31 | }, |
| 32 | "partition-2": { |
| 33 | Key: []byte("boop"), |
| 34 | Partitions: []int{0, 1, 2}, |
| 35 | Partition: 2, |
| 36 | }, |
| 37 | "custom hash": { |
| 38 | Key: []byte("boop"), |
| 39 | Hasher: crc32.NewIEEE(), |
| 40 | Partitions: []int{0, 1, 2}, |
| 41 | Partition: 1, |
| 42 | }, |
| 43 | // in a previous version, this test would select a different partition |
| 44 | // than sarama's hash partitioner. |
| 45 | "hash code with MSB set": { |
| 46 | Key: []byte("20"), |
| 47 | Partitions: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
| 48 | Partition: 1, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for label, test := range testCases { |
| 53 | t.Run(label, func(t *testing.T) { |
| 54 | msg := Message{Key: test.Key} |
| 55 | h := Hash{ |
| 56 | Hasher: test.Hasher, |
| 57 | } |
| 58 | partition := h.Balance(msg, test.Partitions...) |
| 59 | if partition != test.Partition { |
| 60 | t.Errorf("expected %v; got %v", test.Partition, partition) |
| 61 | } |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestReferenceHashBalancer(t *testing.T) { |
| 67 | testCases := map[string]struct { |