Skip to content

Commit

Permalink
[Improve] add some unit test (#2334)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
yuluo-yx and tomsun28 authored Jul 22, 2024
1 parent 497338e commit aee8481
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,39 @@

package org.apache.hertzbeat.collector.dispatch;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;

/**
* Test case for {@link MetricsCollectorQueue}
*/
class MetricsCollectorQueueTest {

private MetricsCollectorQueue metricsCollectorQueue;

private MetricsCollect mockJob;

@BeforeEach
void setUp() {
}

@AfterEach
void tearDown() {
metricsCollectorQueue = new MetricsCollectorQueue();
mockJob = mock(MetricsCollect.class);
}

@Test
void addJob() {
void testAddJob() throws InterruptedException {

metricsCollectorQueue.addJob(mockJob);
assertEquals(mockJob, metricsCollectorQueue.getJob());
}

@Test
void getJob() {
void testGetJobTimeout() throws InterruptedException {

assertNull(metricsCollectorQueue.getJob());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,48 @@

package org.apache.hertzbeat.collector.dispatch;

import org.junit.jupiter.api.AfterEach;
import java.util.concurrent.RejectedExecutionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

/**
* Test case for {@link WorkerPool}
*/
class WorkerPoolTest {

private WorkerPool workerPool;

private Runnable mockTask;

@BeforeEach
void setUp() {

workerPool = new WorkerPool();
mockTask = mock(Runnable.class);
}

@AfterEach
void tearDown() {
@Test
void testExecuteJob() {

assertDoesNotThrow(() -> workerPool.executeJob(mockTask));
}

@Test
void executeJob() {
void testExecuteJobThrowsException() {

workerPool = mock(WorkerPool.class);
doThrow(new RejectedExecutionException()).when(workerPool).executeJob(mockTask);

assertThrows(RejectedExecutionException.class, () -> workerPool.executeJob(mockTask));
}

@Test
void destroy() {
void testDestroy() {
assertDoesNotThrow(() -> workerPool.destroy());
}

}

0 comments on commit aee8481

Please sign in to comment.