JAVA-10238: Use random port in the integration test

This commit is contained in:
Krzysiek 2022-03-18 08:00:13 +01:00
parent 8568a2b222
commit c6a8b181b8
1 changed files with 18 additions and 2 deletions

View File

@ -10,16 +10,23 @@ import com.twitter.util.Future;
import org.junit.Test; import org.junit.Test;
import scala.runtime.BoxedUnit; import scala.runtime.BoxedUnit;
import java.io.IOException;
import java.net.ServerSocket;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class FinagleIntegrationTest { public class FinagleIntegrationTest {
private static final int DEFAULT_PORT = 8079;
@Test @Test
public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception { public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception {
// given // given
int port = randomPort();
Service serverService = new LogFilter().andThen(new GreetingService()); Service serverService = new LogFilter().andThen(new GreetingService());
Http.serve(":8080", serverService); Http.serve(":" + port, serverService);
Service<Request, Response> clientService = new LogFilter().andThen(Http.newService(":8080")); Service<Request, Response> clientService = new LogFilter().andThen(Http.newService(":" + port));
// when // when
Request request = Request.apply(Method.Get(), "/?name=John"); Request request = Request.apply(Method.Get(), "/?name=John");
@ -37,4 +44,13 @@ public class FinagleIntegrationTest {
}) })
); );
} }
private int randomPort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
return DEFAULT_PORT;
}
}
} }