From e9963d109a6cae00d41c4c922ab4d76ca19165f4 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Thu, 25 Jul 2024 15:35:23 +0800 Subject: [PATCH 1/2] [Improve] add CollectServer unit test Signed-off-by: yuluo-yx --- .../dispatch/entrance/CollectServerTest.java | 147 ++++++++++++++++++ pom.xml | 2 +- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServerTest.java diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServerTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServerTest.java new file mode 100644 index 00000000000..6e1367923fa --- /dev/null +++ b/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServerTest.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.collector.dispatch.entrance; + +import java.util.concurrent.ScheduledExecutorService; + +import io.netty.channel.Channel; +import org.apache.hertzbeat.collector.dispatch.CollectorInfoProperties; +import org.apache.hertzbeat.collector.dispatch.DispatchProperties; +import org.apache.hertzbeat.collector.dispatch.entrance.internal.CollectJobService; +import org.apache.hertzbeat.collector.dispatch.timer.TimerDispatch; +import org.apache.hertzbeat.common.entity.message.ClusterMsg; +import org.apache.hertzbeat.common.support.CommonThreadPool; +import org.apache.hertzbeat.remoting.RemotingClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.util.ReflectionTestUtils; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * test case for {@link CollectServer} + */ + +@ExtendWith(MockitoExtension.class) +class CollectServerTest { + + @Mock + private CollectJobService collectJobService; + + @Mock + private TimerDispatch timerDispatch; + + @Mock + private DispatchProperties properties; + + @Mock + private DispatchProperties.EntranceProperties entranceProperties; + + @Mock + private DispatchProperties.EntranceProperties.NettyProperties nettyProperties; + + @Mock + private CommonThreadPool threadPool; + + @Mock + private CollectorInfoProperties infoProperties; + + private CollectServer collectServer; + + private CollectServer.CollectNettyEventListener collectNettyEventListener; + + @BeforeEach + void setUp() { + + when(nettyProperties.getManagerHost()).thenReturn("127.0.0.1"); + when(nettyProperties.getManagerPort()).thenReturn(8080); + when(entranceProperties.getNetty()).thenReturn(nettyProperties); + when(properties.getEntrance()).thenReturn(entranceProperties); + + collectServer = new CollectServer(collectJobService, timerDispatch, properties, threadPool, infoProperties); + collectNettyEventListener = collectServer.new CollectNettyEventListener(); + } + + @Test + void testRun() throws Exception { + + RemotingClient remotingClient = mock(RemotingClient.class); + ReflectionTestUtils.setField(collectServer, "remotingClient", remotingClient); + + collectServer.run(); + + verify(remotingClient, times(1)).start(); + } + + @Test + void testShutdown() { + + RemotingClient remotingClient = mock(RemotingClient.class); + ReflectionTestUtils.setField(collectServer, "remotingClient", remotingClient); + ReflectionTestUtils.setField(collectServer, "scheduledExecutor", mock(ScheduledExecutorService.class)); + + collectServer.shutdown(); + + ScheduledExecutorService scheduledExecutor = (ScheduledExecutorService) ReflectionTestUtils.getField(collectServer, "scheduledExecutor"); + verify((scheduledExecutor), times(1)).shutdownNow(); + verify(remotingClient, times(1)).shutdown(); + } + + @Test + void testSendMsg() { + + RemotingClient remotingClient = mock(RemotingClient.class); + ReflectionTestUtils.setField(collectServer, "remotingClient", remotingClient); + ClusterMsg.Message message = mock(ClusterMsg.Message.class); + + collectServer.sendMsg(message); + + verify(remotingClient, times(1)).sendMsg(message); + } + + @Test + void testOnChannelActive() { + + RemotingClient remotingClient = mock(RemotingClient.class); + ReflectionTestUtils.setField(collectServer, "remotingClient", remotingClient); + + Channel channel = mock(Channel.class); + when(collectJobService.getCollectorIdentity()).thenReturn("collector1"); + when(collectJobService.getCollectorMode()).thenReturn("mode1"); + when(infoProperties.getIp()).thenReturn("127.0.0.1"); + when(infoProperties.getVersion()).thenReturn("1.0"); + + collectNettyEventListener.onChannelActive(channel); + + verify(timerDispatch, times(1)).goOnline(); + verify(remotingClient, times(1)).sendMsg(any(ClusterMsg.Message.class)); + + ScheduledExecutorService scheduledExecutor = + (ScheduledExecutorService) ReflectionTestUtils.getField(collectServer, "scheduledExecutor"); + assertNotNull(scheduledExecutor); + } + +} diff --git a/pom.xml b/pom.xml index 449c1d20e46..6ca3441284f 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + /a org.springframework.boot spring-boot-starter-parent 3.2.3 From aa75f53723d5402b1871c4dbbd7208a9071f2001 Mon Sep 17 00:00:00 2001 From: YuLuo Date: Thu, 25 Jul 2024 21:17:48 +0800 Subject: [PATCH 2/2] fix Signed-off-by: YuLuo --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6ca3441284f..449c1d20e46 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - /a + org.springframework.boot spring-boot-starter-parent 3.2.3