Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions java-storage/google-cloud-storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<testbench.keepAlive>true</testbench.keepAlive>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static FakeHttpServer of(HttpRequestHandler server) {
static FakeHttpServer of(HttpRequestHandler server, boolean trailingSlash) {
// based on
// https://github.com/netty/netty/blob/59aa6e635b9996cf21cd946e64353270679adc73/example/src/main/java/io/netty/example/http/helloworld/HttpHelloWorldServer.java
InetSocketAddress address = new InetSocketAddress("localhost", 0);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 0);
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
Expand Down Expand Up @@ -129,7 +129,7 @@ protected void initChannel(SocketChannel ch) {

InetSocketAddress socketAddress = (InetSocketAddress) channel.localAddress();
String suffix = trailingSlash ? "/" : "";
URI endpoint = URI.create("http://localhost:" + socketAddress.getPort() + suffix);
URI endpoint = URI.create("http://127.0.0.1:" + socketAddress.getPort() + suffix);
HttpStorageOptions httpStorageOptions =
HttpStorageOptions.http()
.setHost(endpoint.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void close() throws InterruptedException {
}

static FakeServer of(StorageGrpc.StorageImplBase service) throws IOException {
InetSocketAddress address = new InetSocketAddress("localhost", 0);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of hardcoding the IPv4 loopback address "127.0.0.1", consider using InetAddress.getLoopbackAddress(). This is more robust as it dynamically resolves to the correct loopback address (IPv4 or IPv6) for the environment without performing a DNS lookup, which avoids the localhost resolution hangs while maintaining compatibility with IPv6-only loopback environments.

Suggested change
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 0);
InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);

Server server = NettyServerBuilder.forAddress(address).addService(service).build();
server.start();
String endpoint = String.format(Locale.US, "%s:%d", address.getHostString(), server.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ public void readObject(
if (request.equals(req1)) {
responseObserver.onNext(resp1);
responseObserver.onNext(resp2);
responseObserver.onError(apiException(Code.DATA_LOSS));
new Thread(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException ignored) {}
responseObserver.onError(apiException(Code.DATA_LOSS));
}).start();
} else if (request.equals(req2)) {
responseObserver.onNext(resp3);
responseObserver.onNext(resp4);
Expand Down Expand Up @@ -194,7 +199,12 @@ public void readObject(
if (count == 0) {
responseObserver.onNext(resp1);
responseObserver.onNext(resp2);
responseObserver.onError(apiException(Code.DATA_LOSS));
new Thread(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException ignored) {}
responseObserver.onError(apiException(Code.DATA_LOSS));
}).start();
}
} else if (request.equals(req2)) {
ReadObjectResponse.Builder builder = resp3.toBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void start() {
ImmutableList.of(
"docker",
"run",
"-i",
"-d",
"--rm",
"--publish",
port + ":9000",
Expand Down Expand Up @@ -323,62 +323,50 @@ public void stop() {
return;
}
try {
process.destroy();
process.waitFor(2, TimeUnit.SECONDS);
boolean attemptForceStopContainer = false;
try {
int processExitValue = process.exitValue();
if (processExitValue != 0) {
attemptForceStopContainer = true;
}
LOGGER.warn("Container exit value = {}", processExitValue);
} catch (IllegalThreadStateException e) {
attemptForceStopContainer = true;
}

if (attemptForceStopContainer) {
LOGGER.warn("Container did not gracefully exit, attempting to explicitly stop it.");
if (!Boolean.getBoolean("testbench.keepAlive")) {
LOGGER.warn("Attempting to explicitly stop container: {}", containerName);
ImmutableList<String> command = ImmutableList.of("docker", "kill", containerName);
LOGGER.warn(command.toString());
Process shutdownProcess = new ProcessBuilder(command).start();
shutdownProcess.waitFor(5, TimeUnit.SECONDS);
int shutdownProcessExitValue = shutdownProcess.exitValue();
LOGGER.warn("Container exit value = {}", shutdownProcessExitValue);
LOGGER.warn("Container stop exit value = {}", shutdownProcessExitValue);

// wait for the server to shutdown
runWithRetries(
() -> {
try {
listRetryTests();
} catch (SocketException e) {
// desired result
return null;
}
throw new NotShutdownException();
},
RetrySettings.newBuilder()
.setTotalTimeoutDuration(Duration.ofSeconds(30))
.setInitialRetryDelayDuration(Duration.ofMillis(500))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelayDuration(Duration.ofSeconds(5))
.build(),
new BasicResultRetryAlgorithm<List<?>>() {
@Override
public boolean shouldRetry(Throwable previousThrowable, List<?> previousResponse) {
return previousThrowable instanceof NotShutdownException;
}
},
NanoClock.getDefaultClock());
}

// wait for the server to shutdown
runWithRetries(
() -> {
try {
listRetryTests();
} catch (SocketException e) {
// desired result
return null;
}
throw new NotShutdownException();
},
RetrySettings.newBuilder()
.setTotalTimeoutDuration(Duration.ofSeconds(30))
.setInitialRetryDelayDuration(Duration.ofMillis(500))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelayDuration(Duration.ofSeconds(5))
.build(),
new BasicResultRetryAlgorithm<List<?>>() {
@Override
public boolean shouldRetry(Throwable previousThrowable, List<?> previousResponse) {
return previousThrowable instanceof NotShutdownException;
}
},
NanoClock.getDefaultClock());
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
} finally {
try {
Files.delete(errPath);
Files.delete(outPath);
Files.delete(tempDirectory);
Files.deleteIfExists(errPath);
Files.deleteIfExists(outPath);
Files.deleteIfExists(tempDirectory);
} catch (IOException e) {
throw new RuntimeException(e);
LOGGER.warn("Failed to delete temporary files", e);
}
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -442,8 +430,8 @@ public String toString() {
}

static final class Builder {
private static final String DEFAULT_BASE_URI = "http://localhost:9000";
private static final String DEFAULT_GRPC_BASE_URI = "http://localhost:9005";
private static final String DEFAULT_BASE_URI = "http://127.0.0.1:9000";
private static final String DEFAULT_GRPC_BASE_URI = "http://127.0.0.1:9005";
private static final String DEFAULT_IMAGE_NAME;
private static final String DEFAULT_IMAGE_TAG;

Expand Down
Loading