Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] add CollectServer unit test #2379

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<parent>/a
yuluo-yx marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
Expand Down