Skip to content

Commit

Permalink
[#498] fix(test): Fix port conflict issue in UT (#499)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Change the `TestGravitonServer` to use random port instead of a default
one to avoid port conflicts.

### Why are the changes needed?

The port conflicts issue makes the test quite flaky, see #498

Fix: #498 

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Existing UTs.
  • Loading branch information
jerryshao authored Oct 12, 2023
1 parent c953f18 commit 140a0da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static org.mockito.Mockito.mock;

import com.datastrato.graviton.Config;
import com.datastrato.graviton.rest.RESTUtils;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -32,17 +34,19 @@ public void tearDown() {
}

@Test
public void testInitialize() {
public void testInitialize() throws IOException {
Config config = new Config(false) {};
config.set(JettyServerConfig.WEBSERVER_HTTP_PORT, RESTUtils.findAvailablePort(5000, 6000));
JettyServerConfig serverConfig = JettyServerConfig.fromConfig(config);
jettyServer.initialize(serverConfig, "test");

// TODO might be nice to have an isInitalised method or similar?
}

@Test
public void testStartAndStop() throws RuntimeException {
public void testStartAndStop() throws RuntimeException, IOException {
Config config = new Config(false) {};
config.set(JettyServerConfig.WEBSERVER_HTTP_PORT, RESTUtils.findAvailablePort(5000, 6000));
JettyServerConfig serverConfig = JettyServerConfig.fromConfig(config);
jettyServer.initialize(serverConfig, "test");
jettyServer.start();
Expand All @@ -51,8 +55,9 @@ public void testStartAndStop() throws RuntimeException {
}

@Test
public void testAddServletAndFilter() throws RuntimeException {
public void testAddServletAndFilter() throws RuntimeException, IOException {
Config config = new Config(false) {};
config.set(JettyServerConfig.WEBSERVER_HTTP_PORT, RESTUtils.findAvailablePort(5000, 6000));
JettyServerConfig serverConfig = JettyServerConfig.fromConfig(config);
jettyServer.initialize(serverConfig, "test");
jettyServer.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.datastrato.graviton.aux.AuxiliaryServiceManager;
import com.datastrato.graviton.rest.RESTUtils;
import com.datastrato.graviton.server.web.JettyServerConfig;
import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -30,17 +32,17 @@ public class TestGravitonServer {
private ServerConfig spyServerConfig;

@BeforeAll
void initConfig() {
String confPath =
System.getenv("GRAVITON_HOME")
+ File.separator
+ "conf"
+ File.separator
+ "graviton.conf.template";
ServerConfig serverConfig = GravitonServer.loadConfig(confPath);
void initConfig() throws IOException {
ServerConfig serverConfig = new ServerConfig();
serverConfig.loadFromMap(
ImmutableMap.of(
ENTRY_KV_ROCKSDB_BACKEND_PATH.getKey(),
ROCKS_DB_STORE_PATH,
GravitonServer.WEBSERVER_CONF_PREFIX + JettyServerConfig.WEBSERVER_HTTP_PORT.getKey(),
String.valueOf(RESTUtils.findAvailablePort(5000, 6000))),
t -> true);

spyServerConfig = Mockito.spy(serverConfig);
Mockito.when(spyServerConfig.get(ENTRY_KV_ROCKSDB_BACKEND_PATH))
.thenReturn(ROCKS_DB_STORE_PATH);

Mockito.when(
spyServerConfig.getConfigsWithPrefix(
Expand Down

0 comments on commit 140a0da

Please sign in to comment.