|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | describe Kafka::ConsumerGroup::Assignor do |
4 | | - let(:cluster) { double(:cluster) } |
5 | | - let(:assignor) { described_class.new(cluster: cluster, strategy: strategy) } |
6 | | - let(:strategy) do |
7 | | - klass = Class.new do |
8 | | - def protocol_name |
9 | | - "test" |
| 4 | + describe ".register_strategy" do |
| 5 | + context "when another strategy is already registered with the same name" do |
| 6 | + around do |example| |
| 7 | + described_class.register_strategy(:custom) { |**kwargs| [] } |
| 8 | + example.run |
| 9 | + described_class.strategy_classes.delete("custom") |
10 | 10 | end |
11 | 11 |
|
12 | | - def user_data |
13 | | - nil |
| 12 | + it do |
| 13 | + expect { described_class.register_strategy(:custom) { |**kwargs| [] } }.to raise_error(ArgumentError) |
14 | 14 | end |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + describe "#assign" do |
| 19 | + let(:cluster) { double(:cluster) } |
| 20 | + let(:assignor) { described_class.new(cluster: cluster, strategy: strategy) } |
| 21 | + |
| 22 | + let(:members) { Hash[(0...10).map {|i| ["member#{i}", nil] }] } |
| 23 | + let(:topics) { ["greetings"] } |
| 24 | + let(:partitions) { (0...30).map {|i| double(:"partition#{i}", partition_id: i) } } |
| 25 | + |
| 26 | + before do |
| 27 | + allow(cluster).to receive(:partitions_for) { partitions } |
| 28 | + end |
| 29 | + after do |
| 30 | + described_class.strategy_classes.delete("custom") |
| 31 | + end |
| 32 | + |
| 33 | + context "when the strategy is an object" do |
| 34 | + let(:strategy) do |
| 35 | + klass = Class.new do |
| 36 | + def assign(cluster:, members:, partitions:) |
| 37 | + assignment = {} |
| 38 | + partition_count_per_member = (partitions.count.to_f / members.count).ceil |
| 39 | + partitions.each_slice(partition_count_per_member).with_index do |chunk, index| |
| 40 | + assignment[members.keys[index]] = chunk |
| 41 | + end |
15 | 42 |
|
16 | | - def assign(members:, partitions:) |
17 | | - assignment = {} |
18 | | - partition_count_per_member = (partitions.count.to_f / members.count).ceil |
19 | | - partitions.each_slice(partition_count_per_member).with_index do |chunk, index| |
20 | | - assignment[members.keys[index]] = chunk |
| 43 | + assignment |
| 44 | + end |
21 | 45 | end |
| 46 | + described_class.register_strategy(:custom, klass) |
22 | 47 |
|
23 | | - assignment |
| 48 | + klass.new |
| 49 | + end |
| 50 | + |
| 51 | + it "assigns all partitions" do |
| 52 | + assignments = assignor.assign(members: members, topics: topics) |
| 53 | + |
| 54 | + partitions.each do |partition| |
| 55 | + member = assignments.values.find {|assignment| |
| 56 | + assignment.topics.find {|topic, partitions| |
| 57 | + partitions.include?(partition.partition_id) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + expect(member).to_not be_nil |
| 62 | + end |
24 | 63 | end |
25 | 64 | end |
26 | | - klass.new |
27 | | - end |
28 | 65 |
|
29 | | - it "assigns all partitions" do |
30 | | - members = Hash[(0...10).map {|i| ["member#{i}", nil] }] |
31 | | - topics = ["greetings"] |
32 | | - partitions = (0...30).map {|i| double(:"partition#{i}", partition_id: i) } |
| 66 | + context "when the strategy is a string" do |
| 67 | + let(:strategy) { :custom } |
33 | 68 |
|
34 | | - allow(cluster).to receive(:partitions_for) { partitions } |
| 69 | + before do |
| 70 | + described_class.register_strategy(:custom) do |cluster:, members:, partitions:| |
| 71 | + assignment = {} |
| 72 | + partition_count_per_member = (partitions.count.to_f / members.count).ceil |
| 73 | + partitions.each_slice(partition_count_per_member).with_index do |chunk, index| |
| 74 | + assignment[members.keys[index]] = chunk |
| 75 | + end |
35 | 76 |
|
36 | | - assignments = assignor.assign(members: members, topics: topics) |
| 77 | + assignment |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + it "assigns all partitions" do |
| 82 | + assignments = assignor.assign(members: members, topics: topics) |
37 | 83 |
|
38 | | - partitions.each do |partition| |
39 | | - member = assignments.values.find {|assignment| |
40 | | - assignment.topics.find {|topic, partitions| |
41 | | - partitions.include?(partition.partition_id) |
42 | | - } |
43 | | - } |
| 84 | + partitions.each do |partition| |
| 85 | + member = assignments.values.find {|assignment| |
| 86 | + assignment.topics.find {|topic, partitions| |
| 87 | + partitions.include?(partition.partition_id) |
| 88 | + } |
| 89 | + } |
44 | 90 |
|
45 | | - expect(member).to_not be_nil |
| 91 | + expect(member).to_not be_nil |
| 92 | + end |
| 93 | + end |
46 | 94 | end |
47 | 95 | end |
48 | 96 | end |
0 commit comments