def test_count_preferred_replica(self, partition): p1 = partition b1 = Broker('test-broker', partitions=set([p1])) p1.add_replica(b1) p2 = Mock(spec=Partition, topic=sentinel.t1, replicas=[sentinel.b2]) b1.add_partition(p2) assert b1.count_preferred_replica() == 1
def test_add_partition(self, create_partition): p10 = create_partition('t1', 0) p20 = create_partition('t2', 0) broker = Broker('test-broker', partitions=set([p10])) broker.add_partition(p20) assert broker.partitions == set([p10, p20]) assert p20.replicas == [broker]
def test_remove_partition(self, partition): p1 = partition b1 = Broker('test-broker', partitions=set([p1])) p1.add_replica(b1) # Remove partition b1.remove_partition(p1) assert p1 not in b1.partitions assert b1 not in p1.replicas
def test_count_partition(self): t1 = sentinel.t1 partitions = set([ Mock(spec=Partition, topic=t1, replicas=sentinel.r1), Mock(spec=Partition, topic=t1, replicas=sentinel.r1), Mock(spec=Partition, topic=sentinel.t2, replicas=sentinel.r1), ]) broker = Broker('test-broker', partitions=partitions) assert broker.count_partitions(t1) == 2 assert broker.count_partitions(sentinel.t3) == 0
def test_count_partition(self, create_partition): p10 = create_partition('t1', 0) p11 = create_partition('t1', 1) p20 = create_partition('t2', 0) p30 = create_partition('t3', 0) t1 = p10.topic t3 = p30.topic broker = Broker('test-broker', partitions=set([p10, p11, p20])) assert broker.count_partitions(t1) == 2 assert broker.count_partitions(t3) == 0
def test_move_partition(self, partition): victim_partition = partition source_broker = Broker('b1', partitions=set([victim_partition])) victim_partition.add_replica(source_broker) dest_broker = Broker('b2') # Move partition source_broker.move_partition(victim_partition, dest_broker) # Verify partition moved from source to destination broker assert victim_partition not in source_broker.partitions assert victim_partition in dest_broker.partitions # Verify that victim-partition-replicas has replaced # source-broker to dest-broker assert source_broker not in victim_partition.replicas assert dest_broker in victim_partition.replicas
def test_size(self): partitions = set([ Mock(spec=Partition, topic=sentinel.t1, size=1), Mock(spec=Partition, topic=sentinel.t1, size=2), Mock(spec=Partition, topic=sentinel.t2, size=3), ]) broker = Broker('test-broker', partitions=partitions) assert broker.size == 6
def test_rebalance_new_empty_broker(self, rg_balanced): broker = Broker('4') rg_balanced.add_broker(broker) orig_partitions = rg_balanced.partitions rg_balanced.rebalance_brokers() assert sorted(orig_partitions) == sorted(rg_balanced.partitions) assert_rg_balanced(rg_balanced)
def test_weight(self): partitions = set([ Mock(spec=Partition, topic=sentinel.t1, weight=1), Mock(spec=Partition, topic=sentinel.t1, weight=2), Mock(spec=Partition, topic=sentinel.t2, weight=3), ]) broker = Broker('test-broker', partitions=partitions) assert broker.weight == 6
def test_topics(self): partitions = set([ Mock(spec=Partition, topic=sentinel.t1), Mock(spec=Partition, topic=sentinel.t1), Mock(spec=Partition, topic=sentinel.t2), ]) broker = Broker('test-broker', partitions=partitions) assert broker.topics == set([sentinel.t1, sentinel.t2])
def test_not_empty(self, partition): broker = Broker('test-broker', partitions=set([partition])) assert not broker.empty()
def test_empty(self): broker = Broker('test-broker') # No partitions for this broker assert broker.empty()
def test_partitions(self): broker = Broker('test-broker', partitions=set([sentinel.p1, sentinel.p2])) assert broker.partitions == set([sentinel.p1, sentinel.p2])
def test_mark_decommissioned(self): broker = Broker('test-broker') broker.mark_decommissioned() assert broker.decommissioned
def test_partitions_empty(self): broker = Broker('test-broker') assert broker.partitions == set()
def create_broker(broker_id, partitions): b = Broker(broker_id, partitions=set(partitions)) for p in partitions: p.add_replica(b) return b
def test_mark_inactive(self): broker = Broker('test-broker') broker.mark_inactive() assert broker.inactive