|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | describe Kafka::Partitioner, "#call" do
|
4 |
| - let(:partitioner) { Kafka::Partitioner.new } |
5 | 4 | let(:message) { double(:message, key: nil, partition_key: "yolo") }
|
6 | 5 |
|
7 |
| - it "deterministically returns a partition number for a partition key and partition count" do |
8 |
| - partition = partitioner.call(3, message) |
9 |
| - expect(partition).to eq 0 |
10 |
| - end |
| 6 | + describe "default partitioner" do |
| 7 | + let(:partitioner) { Kafka::Partitioner.new } |
| 8 | + |
| 9 | + it "deterministically returns a partition number for a partition key and partition count" do |
| 10 | + partition = partitioner.call(3, message) |
| 11 | + expect(partition).to eq 0 |
| 12 | + end |
| 13 | + |
| 14 | + it "falls back to the message key if no partition key is available" do |
| 15 | + allow(message).to receive(:partition_key) { nil } |
| 16 | + allow(message).to receive(:key) { "hey" } |
11 | 17 |
|
12 |
| - it "falls back to the message key if no partition key is available" do |
13 |
| - allow(message).to receive(:partition_key) { nil } |
14 |
| - allow(message).to receive(:key) { "hey" } |
| 18 | + partition = partitioner.call(3, message) |
15 | 19 |
|
16 |
| - partition = partitioner.call(3, message) |
| 20 | + expect(partition).to eq 2 |
| 21 | + end |
17 | 22 |
|
18 |
| - expect(partition).to eq 2 |
| 23 | + it "randomly picks a partition if the key is nil" do |
| 24 | + allow(message).to receive(:key) { nil } |
| 25 | + allow(message).to receive(:partition_key) { nil } |
| 26 | + |
| 27 | + partitions = 30.times.map { partitioner.call(3, message) } |
| 28 | + |
| 29 | + expect(partitions.uniq).to contain_exactly(0, 1, 2) |
| 30 | + end |
19 | 31 | end
|
20 | 32 |
|
21 |
| - it "randomly picks a partition if the key is nil" do |
22 |
| - allow(message).to receive(:key) { nil } |
23 |
| - allow(message).to receive(:partition_key) { nil } |
| 33 | + describe "murmur2 partitioner" do |
| 34 | + let(:partitioner) { Kafka::Partitioner.new(hash_function: :murmur2) } |
| 35 | + let(:message) { double(:message, key: nil, partition_key: "yolo") } |
| 36 | + |
| 37 | + it "deterministically returns a partition number for a partition key and partition count" do |
| 38 | + partition = partitioner.call(3, message) |
| 39 | + expect(partition).to eq 0 |
| 40 | + end |
| 41 | + |
| 42 | + it "falls back to the message key if no partition key is available" do |
| 43 | + allow(message).to receive(:partition_key) { nil } |
| 44 | + allow(message).to receive(:key) { "hey" } |
| 45 | + |
| 46 | + partition = partitioner.call(3, message) |
| 47 | + |
| 48 | + expect(partition).to eq 1 |
| 49 | + end |
| 50 | + |
| 51 | + it "randomly picks a partition if the key is nil" do |
| 52 | + allow(message).to receive(:key) { nil } |
| 53 | + allow(message).to receive(:partition_key) { nil } |
| 54 | + |
| 55 | + partitions = 30.times.map { partitioner.call(3, message) } |
24 | 56 |
|
25 |
| - partitions = 30.times.map { partitioner.call(3, message) } |
| 57 | + expect(partitions.uniq).to contain_exactly(0, 1, 2) |
| 58 | + end |
26 | 59 |
|
27 |
| - expect(partitions.uniq).to contain_exactly(0, 1, 2) |
| 60 | + it "picks a Java Kafka compatible partition" do |
| 61 | + partition_count = 100 |
| 62 | + { |
| 63 | + # librdkafka test cases taken from tests/0048-partitioner.c |
| 64 | + "" => 0x106e08d9 % partition_count, |
| 65 | + "this is another string with more length to it perhaps" => 0x4f7703da % partition_count, |
| 66 | + "hejsan" => 0x5ec19395 % partition_count, |
| 67 | + # Java Kafka test cases taken from UtilsTest.java. |
| 68 | + # The Java tests check the result of murmur2 directly, |
| 69 | + # so have been ANDd with 0x7fffffff to work here |
| 70 | + "21" => (-973932308 & 0x7fffffff) % partition_count, |
| 71 | + "foobar" => (-790332482 & 0x7fffffff) % partition_count, |
| 72 | + "a-little-bit-long-string" => (-985981536 & 0x7fffffff) % partition_count, |
| 73 | + "a-little-bit-longer-string" => (-1486304829 & 0x7fffffff) % partition_count, |
| 74 | + "lkjh234lh9fiuh90y23oiuhsafujhadof229phr9h19h89h8" => (-58897971 & 0x7fffffff) % partition_count |
| 75 | + }.each do |key, partition| |
| 76 | + allow(message).to receive(:partition_key) { key } |
| 77 | + expect(partitioner.call(partition_count, message)).to eq partition |
| 78 | + end |
| 79 | + end |
28 | 80 | end
|
29 | 81 | end
|
0 commit comments