Skip to content

Commit

Permalink
test (jkube-kit/remote-dev) : Fix intermittent failures of RemoteDeve…
Browse files Browse the repository at this point in the history
…lopmentServiceTest

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jan 25, 2024
1 parent be0ce2e commit dc5a2f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import org.eclipse.jkube.kit.common.KitLogger;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -65,7 +66,7 @@ public void stop() {

private void checkEnvironment() {
for (RemoteService remoteService : context.getRemoteDevelopmentConfig().getRemoteServices()) {
try (ServerSocket ignore = new ServerSocket(remoteService.getLocalPort())) {
try (ServerSocket ignore = new ServerSocket(remoteService.getLocalPort(), 50, InetAddress.getLoopbackAddress())) {
logger.debug("Local port '%s' for remote service '%s:%s' is available",
remoteService.getLocalPort(), remoteService.getHostname(), remoteService.getPort());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -109,20 +115,47 @@ void startInitsIfLocalPortAvailable() {
.debug("Creating or replacing Kubernetes services for exposed ports from local environment");
}

@Test
void nestedServerSocketsShouldThrowError() throws IOException {
try (ServerSocket ss1 = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
int port = ss1.getLocalPort();
assertThatExceptionOfType(BindException.class)
.isThrownBy(() -> {
try (ServerSocket ss2 = new ServerSocket(port, 50, InetAddress.getLoopbackAddress())) {
// Should be unreachable
assertThat(ss2.getLocalPort()).isEqualTo(port);
}
}).withMessageContaining("already in use");
}
}

@Test
@DisplayName("start fails if LocalPort for remote service is in use")
void startFailsIfLocalPortInUse() {
void startFailsIfLocalPortInUse() throws IOException {
final int localPort = IoUtil.getFreeRandomPort();
RemoteService remoteService = RemoteService.builder()
.hostname("remote-host").localPort(localPort).port(1234).build();
remoteDevelopmentService = new RemoteDevelopmentService(
logger, kubernetesClient, RemoteDevelopmentConfig.builder().remoteService(remoteService).build());
assertThatThrownBy(() -> {
try (ServerSocket localPortInUse = new ServerSocket(localPort)) {

try (ServerSocket localPortInUse = new ServerSocket(localPort, 1, InetAddress.getLoopbackAddress())) {
// When
remoteDevelopmentService.start();
assertThat(localPortInUse.getInetAddress().getHostAddress()).isEqualTo("127.0.0.1");
assertThat(localPortInUse.getLocalPort()).isEqualTo(localPort);
assertThat(localPortInUse.isBound()).isTrue();
assertThat(localPortInUse.isClosed()).isFalse();
try (Socket socket = new Socket("localhost", localPort)) {
assertThat(socket.getInetAddress().getHostAddress()).isEqualTo("127.0.0.1");
assertThat(socket.getPort()).isEqualTo(localPort);
assertThat(socket.isBound()).isTrue();
assertThat(socket.isClosed()).isFalse();
try (ServerSocket ss2 = new ServerSocket(remoteService.getLocalPort(), 1, InetAddress.getByName("localhost"))) {
assertThatThrownBy(() -> {
remoteDevelopmentService.start();
}).hasMessageContaining("Local port '" + localPort + "' is already in use (remote-host)");
}
}
}
})
.hasMessageContaining("Local port '" + localPort + "' is already in use (remote-host)");

}
}

0 comments on commit dc5a2f7

Please sign in to comment.