@Test public void testReigsterSlaveConsumer_thenMasterDown() throws Exception { this.testReigsterSlaveConsumer(); this.mocksControl.reset(); this.mockCommitOffsets( GROUP, ConsumerZooKeeperAccessor.getTopicPartitionRegInfos( this.slaveConsumerZooKeeper, this.fetchManager)); this.mockFetchManagerRestartAnyTimes(); this.mockConnectCloseAnyTimes("meta://localhost:0"); this.mocksControl.replay(); // master down ZkUtils.deletePath(this.client, this.metaZookeeper.brokerIdsPath + "/0/master"); // 这里topic的两次删除(挂掉或人工停掉),可能会引起两次负载均衡 ZkUtils.deletePath(this.client, this.metaZookeeper.brokerTopicsPath + "/topic1/0-m"); ZkUtils.deletePath(this.client, this.metaZookeeper.brokerTopicsPath + "/topic2/0-m"); Thread.sleep(5000); this.mocksControl.verify(); // master 挂掉或人工停掉,TopicPartitionRegInfo清空了 final SlaveZKLoadRebalanceListener listener = (SlaveZKLoadRebalanceListener) ConsumerZooKeeperAccessor.getBrokerConnectionListenerForTest( this.slaveConsumerZooKeeper, this.fetchManager); assertNotNull(listener); final ConcurrentHashMap<String /* topic */, ConcurrentHashMap<Partition, TopicPartitionRegInfo>> topicRegistry = ConsumerZooKeeperAccessor.getTopicRegistry(listener); assertNotNull(topicRegistry); assertTrue(topicRegistry.isEmpty()); // assertEquals(2, topicRegistry.size()); // // assertTrue(topicRegistry.containsKey("topic1")); // assertTrue(topicRegistry.containsKey("topic2")); // assertEquals(0, topicRegistry.get("topic1").size()); // assertEquals(0, topicRegistry.get("topic2").size()); final Set<Broker> brokerSet = ConsumerZooKeeperAccessor.getOldBrokerSet(listener); assertEquals(0, brokerSet.size()); assertFalse(brokerSet.contains(new Broker(0, "meta://localhost:0"))); assertFalse(brokerSet.contains(new Broker(1, "meta://localhost:1"))); }