This is an automated email from the ASF dual-hosted git repository.

jinrongtong pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new b96d6b98e8 [ISSUE #8285] Add more test coverage for 
BrokerPreOnlineService (#8286)
b96d6b98e8 is described below

commit b96d6b98e836b4604378b8358d58d8a9a1d2037c
Author: yx9o <[email protected]>
AuthorDate: Wed Jun 12 09:08:49 2024 +0800

    [ISSUE #8285] Add more test coverage for BrokerPreOnlineService (#8286)
---
 ...neTest.java => BrokerPreOnlineServiceTest.java} | 63 +++++++++++++++++-----
 1 file changed, 49 insertions(+), 14 deletions(-)

diff --git 
a/container/src/test/java/org/apache/rocketmq/container/BrokerPreOnlineTest.java
 
b/container/src/test/java/org/apache/rocketmq/container/BrokerPreOnlineServiceTest.java
similarity index 63%
rename from 
container/src/test/java/org/apache/rocketmq/container/BrokerPreOnlineTest.java
rename to 
container/src/test/java/org/apache/rocketmq/container/BrokerPreOnlineServiceTest.java
index 2158b8d9ac..6a46ed1f6f 100644
--- 
a/container/src/test/java/org/apache/rocketmq/container/BrokerPreOnlineTest.java
+++ 
b/container/src/test/java/org/apache/rocketmq/container/BrokerPreOnlineServiceTest.java
@@ -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;
@@ -34,12 +31,21 @@ import org.junit.runner.RunWith;
 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;
 
@@ -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);
@@ -91,11 +99,38 @@ public class BrokerPreOnlineTest {
 
     @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());
+    }
 }

Reply via email to