Skip to content

Commit

Permalink
[ISSUE #8285] Add more test coverage for BrokerPreOnlineService (#8286)
Browse files Browse the repository at this point in the history
  • Loading branch information
yx9o authored Jun 12, 2024
1 parent 3ecc73b commit b96d6b9
Showing 1 changed file with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

package org.apache.rocketmq.container;

import java.lang.reflect.Field;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.apache.rocketmq.broker.BrokerController;
import org.apache.rocketmq.broker.BrokerPreOnlineService;
import org.apache.rocketmq.broker.out.BrokerOuterAPI;
import org.apache.rocketmq.broker.transaction.TransactionalMessageCheckService;
import org.apache.rocketmq.common.BrokerConfig;
import org.apache.rocketmq.remoting.protocol.BrokerSyncInfo;
import org.apache.rocketmq.remoting.protocol.body.BrokerMemberGroup;
import org.apache.rocketmq.store.DefaultMessageStore;
import org.apache.rocketmq.store.config.MessageStoreConfig;
Expand All @@ -34,12 +31,21 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class BrokerPreOnlineTest {
public class BrokerPreOnlineServiceTest {
@Mock
private BrokerContainer brokerContainer;

Expand All @@ -48,25 +54,27 @@ public class BrokerPreOnlineTest {
@Mock
private BrokerOuterAPI brokerOuterAPI;

public void init() throws Exception {
public void init(final long brokerId) throws Exception {
when(brokerContainer.getBrokerOuterAPI()).thenReturn(brokerOuterAPI);

BrokerMemberGroup brokerMemberGroup1 = new BrokerMemberGroup();
Map<Long, String> brokerAddrMap = new HashMap<>();
brokerAddrMap.put(1L, "127.0.0.1:20911");
brokerAddrMap.put(0L, "127.0.0.1:10911");
brokerMemberGroup1.setBrokerAddrs(brokerAddrMap);

BrokerMemberGroup brokerMemberGroup2 = new BrokerMemberGroup();
brokerMemberGroup2.setBrokerAddrs(new HashMap<>());

// when(brokerOuterAPI.syncBrokerMemberGroup(anyString(), anyString()))
// .thenReturn(brokerMemberGroup1)
// .thenReturn(brokerMemberGroup2);
// doNothing().when(brokerOuterAPI).sendBrokerHaInfo(anyString(), anyString(), anyLong(), anyString());
when(brokerOuterAPI.syncBrokerMemberGroup(anyString(), anyString(), anyBoolean()))
.thenReturn(brokerMemberGroup1)
.thenReturn(brokerMemberGroup2);
BrokerSyncInfo brokerSyncInfo = mock(BrokerSyncInfo.class);
when(brokerOuterAPI.retrieveBrokerHaInfo(anyString())).thenReturn(brokerSyncInfo);

DefaultMessageStore defaultMessageStore = mock(DefaultMessageStore.class);
when(defaultMessageStore.getMessageStoreConfig()).thenReturn(new MessageStoreConfig());
when(defaultMessageStore.getBrokerConfig()).thenReturn(new BrokerConfig());
BrokerConfig brokerConfig = new BrokerConfig();
brokerConfig.setBrokerId(brokerId);
when(defaultMessageStore.getBrokerConfig()).thenReturn(brokerConfig);

// HAService haService = new DefaultHAService();
// haService.init(defaultMessageStore);
Expand All @@ -91,11 +99,38 @@ public void init() throws Exception {

@Test
public void testMasterOnlineConnTimeout() throws Exception {
init();
init(0);
BrokerPreOnlineService brokerPreOnlineService = new BrokerPreOnlineService(innerBrokerController);

brokerPreOnlineService.start();

await().atMost(Duration.ofSeconds(30)).until(() -> !innerBrokerController.isIsolated());
}

@Test
public void testMasterOnlineNormal() throws Exception {
init(0);
BrokerPreOnlineService brokerPreOnlineService = new BrokerPreOnlineService(innerBrokerController);
brokerPreOnlineService.start();
TimeUnit.SECONDS.sleep(1);
brokerPreOnlineService.shutdown();
await().atMost(Duration.ofSeconds(30)).until(brokerPreOnlineService::isStopped);
}

@Test
public void testSlaveOnlineNormal() throws Exception {
init(1);
BrokerPreOnlineService brokerPreOnlineService = new BrokerPreOnlineService(innerBrokerController);
brokerPreOnlineService.start();
TimeUnit.SECONDS.sleep(1);
brokerPreOnlineService.shutdown();
await().atMost(Duration.ofSeconds(30)).until(brokerPreOnlineService::isStopped);
}

@Test
public void testGetServiceName() throws Exception {
init(1);
BrokerPreOnlineService brokerPreOnlineService = new BrokerPreOnlineService(innerBrokerController);
assertEquals(BrokerPreOnlineService.class.getSimpleName(), brokerPreOnlineService.getServiceName());
}
}

0 comments on commit b96d6b9

Please sign in to comment.