This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 8a88607be [Improve] add collectJonService unit test (#2351)
8a88607be is described below
commit 8a88607be90e6053be044e2862a155660b697e42
Author: YuLuo <[email protected]>
AuthorDate: Tue Jul 23 23:30:44 2024 +0800
[Improve] add collectJonService unit test (#2351)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../entrance/internal/CollectJobServiceTest.java | 119 +++++++++++++++++++--
1 file changed, 111 insertions(+), 8 deletions(-)
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/internal/CollectJobServiceTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/internal/CollectJobServiceTest.java
index e1cccffad..ca802b654 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/internal/CollectJobServiceTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/dispatch/entrance/internal/CollectJobServiceTest.java
@@ -17,36 +17,139 @@
package org.apache.hertzbeat.collector.dispatch.entrance.internal;
-import org.junit.jupiter.api.AfterEach;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import org.apache.hertzbeat.collector.dispatch.DispatchProperties;
+import org.apache.hertzbeat.collector.dispatch.WorkerPool;
+import org.apache.hertzbeat.collector.dispatch.entrance.CollectServer;
+import org.apache.hertzbeat.collector.dispatch.timer.TimerDispatch;
+import org.apache.hertzbeat.common.entity.job.Job;
+import org.apache.hertzbeat.common.entity.message.ClusterMsg;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
/**
* Test case for {@link CollectJobService}
*/
+
class CollectJobServiceTest {
+ @Mock
+ private TimerDispatch timerDispatch;
+
+ @Mock
+ private WorkerPool workerPool;
+
+ @Mock
+ private CollectServer collectServer;
+
+ @Mock
+ private DispatchProperties properties;
+
+ @InjectMocks
+ private CollectJobService collectJobService;
+
@BeforeEach
- void setUp() {
+ public void setUp() {
+
+ MockitoAnnotations.openMocks(this);
+
+ DispatchProperties.EntranceProperties entrance =
mock(DispatchProperties.EntranceProperties.class);
+ DispatchProperties.EntranceProperties.NettyProperties netty =
mock(DispatchProperties.EntranceProperties.NettyProperties.class);
+
+ when(properties.getEntrance()).thenReturn(entrance);
+ when(entrance.getNetty()).thenReturn(netty);
+ when(netty.isEnabled()).thenReturn(true);
+ when(netty.getMode()).thenReturn("test-mode");
+ when(netty.getIdentity()).thenReturn("test-collector");
+
+ collectJobService = spy(new CollectJobService(timerDispatch,
properties, workerPool));
+ collectJobService.setCollectServer(collectServer);
+ }
+
+ @Test
+ public void testCollectSyncJobData() {
+
+ Job job = mock(Job.class);
+ List<CollectRep.MetricsData> metricsDataList =
List.of(CollectRep.MetricsData.newBuilder().build());
+ CountDownLatch latch = new CountDownLatch(1);
+
+ doAnswer(invocation -> {
+ CollectResponseEventListener listener = invocation.getArgument(1);
+ listener.response(metricsDataList);
+ latch.countDown();
+ return null;
+ }).when(timerDispatch).addJob(any(Job.class),
any(CollectResponseEventListener.class));
+
+ List<CollectRep.MetricsData> result =
collectJobService.collectSyncJobData(job);
+
+ assertEquals(metricsDataList, result);
}
- @AfterEach
- void tearDown() {
+ @Test
+ public void testCollectSyncOneTimeJobData() {
+
+ Job job = mock(Job.class);
+ List<CollectRep.MetricsData> metricsDataList =
List.of(CollectRep.MetricsData.newBuilder().build());
+
+ doAnswer(invocation -> {
+ Runnable task = invocation.getArgument(0);
+ task.run();
+ return null;
+ }).when(workerPool).executeJob(any(Runnable.class));
+
+
doReturn(metricsDataList).when(collectJobService).collectSyncJobData(any(Job.class));
+
+ collectJobService.collectSyncOneTimeJobData(job);
+
+ verify(collectServer, times(1)).sendMsg(any(ClusterMsg.Message.class));
}
@Test
- void collectSyncJobData() {
+ public void testCancelAsyncCollectJob() {
+
+ Long jobId = 123L;
+ collectJobService.cancelAsyncCollectJob(jobId);
+
+ verify(timerDispatch, times(1)).deleteJob(eq(jobId), eq(true));
}
@Test
- void addAsyncCollectJob() {
+ public void testSendAsyncCollectData() {
+
+ CollectRep.MetricsData metricsData = CollectRep.MetricsData
+ .newBuilder()
+ .setMetrics("test")
+ .build();
+ collectJobService.sendAsyncCollectData(metricsData);
+
+ verify(collectServer, times(1)).sendMsg(any(ClusterMsg.Message.class));
}
@Test
- void updateAsyncCollectJob() {
+ public void testGetCollectorIdentity() {
+
+ assertEquals("test-collector",
collectJobService.getCollectorIdentity());
}
@Test
- void cancelAsyncCollectJob() {
+ public void testGetCollectorMode() {
+
+ assertEquals("test-mode", collectJobService.getCollectorMode());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]