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

[TEST] fix log server related test #15659

Merged
merged 3 commits into from Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -49,6 +49,8 @@
import org.apache.dolphinscheduler.extract.common.transportor.TaskInstanceLogPageQueryRequest;
import org.apache.dolphinscheduler.extract.common.transportor.TaskInstanceLogPageQueryResponse;

import java.io.IOException;
import java.net.ServerSocket;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -89,9 +91,17 @@ public class LoggerServiceTest {

private NettyRemotingServer nettyRemotingServer;

private int nettyServerPort = 18080;

@BeforeEach
public void setUp() {
nettyRemotingServer = new NettyRemotingServer(NettyServerConfig.builder().listenPort(8080).build());
try (ServerSocket s = new ServerSocket(0)) {
nettyServerPort = s.getLocalPort();
} catch (IOException e) {
return;
}

nettyRemotingServer = new NettyRemotingServer(NettyServerConfig.builder().listenPort(nettyServerPort).build());
nettyRemotingServer.start();
SpringServerMethodInvokerDiscovery springServerMethodInvokerDiscovery =
new SpringServerMethodInvokerDiscovery(nettyRemotingServer);
Expand Down Expand Up @@ -148,7 +158,7 @@ public void testQueryLog() {
Assertions.assertEquals(Status.TASK_INSTANCE_HOST_IS_NULL.getCode(), result.getCode().intValue());

// PROJECT_NOT_EXIST
taskInstance.setHost("127.0.0.1:8080");
taskInstance.setHost("127.0.0.1:" + nettyServerPort);
taskInstance.setLogPath("/temp/log");
doThrow(new ServiceException(Status.PROJECT_NOT_EXIST)).when(projectService)
.checkProjectAndAuthThrowException(loginUser, taskInstance.getProjectCode(), VIEW_LOG);
Expand Down Expand Up @@ -198,7 +208,7 @@ public void testGetLogBytes() {
}

// PROJECT_NOT_EXIST
taskInstance.setHost("127.0.0.1:8080");
taskInstance.setHost("127.0.0.1:" + nettyServerPort);
taskInstance.setLogPath("/temp/log");
doThrow(new ServiceException(Status.PROJECT_NOT_EXIST)).when(projectService)
.checkProjectAndAuthThrowException(loginUser, taskInstance.getProjectCode(), VIEW_LOG);
Expand All @@ -215,7 +225,7 @@ public void testGetLogBytes() {
doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, taskInstance.getProjectCode(),
DOWNLOAD_LOG);
byte[] logBytes = loggerService.getLogBytes(loginUser, 1);
Assertions.assertEquals(47, logBytes.length);
Assertions.assertEquals(43, logBytes.length - String.valueOf(nettyServerPort).length());
}

@Test
Expand All @@ -233,7 +243,7 @@ public void testQueryLogInSpecifiedProject() {
// SUCCESS
taskInstance.setTaskCode(1L);
taskInstance.setId(1);
taskInstance.setHost("127.0.0.1:8080");
taskInstance.setHost("127.0.0.1:" + nettyServerPort);
taskInstance.setLogPath("/temp/log");
doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, VIEW_LOG);
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance);
Expand All @@ -259,7 +269,7 @@ public void testGetLogBytesInSpecifiedProject() {
// SUCCESS
taskInstance.setTaskCode(1L);
taskInstance.setId(1);
taskInstance.setHost("127.0.0.1:8080");
taskInstance.setHost("127.0.0.1:" + nettyServerPort);
taskInstance.setLogPath("/temp/log");
doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, DOWNLOAD_LOG);
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance);
Expand Down