From afa3dda9ce585a23ab88d3ef249f2b82865ebdee Mon Sep 17 00:00:00 2001 From: nidhiii-27 Date: Fri, 22 May 2026 22:30:25 +0530 Subject: [PATCH 1/2] fix(storage): fix flaky storage integration tests --- .../storage/it/runner/registry/TestBench.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java index 4d9340762093..6977370b9519 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java @@ -262,7 +262,7 @@ public void start() { dockerImage, "gunicorn", "--bind=0.0.0.0:9000", - "--worker-class=sync", + "--worker-class=gthread", "--threads=10", "--access-logfile=-", "--keep-alive=0", @@ -401,6 +401,15 @@ private void dumpServerLog(String prefix, File out) throws IOException { } } + private static int findFreePort() { + try (java.net.ServerSocket socket = new java.net.ServerSocket(0)) { + socket.setReuseAddress(true); + return socket.getLocalPort(); + } catch (java.io.IOException e) { + throw new RuntimeException("Failed to find a free port", e); + } + } + static Builder newBuilder() { return new Builder(); } @@ -495,13 +504,16 @@ static final class Builder { private String containerName; private Builder() { - this( - false, - DEFAULT_BASE_URI, - DEFAULT_GRPC_BASE_URI, - DEFAULT_IMAGE_NAME, - DEFAULT_IMAGE_TAG, - DEFAULT_CONTAINER_NAME); + int httpPort = findFreePort(); + int grpcPort = findFreePort(); + String uuid = java.util.UUID.randomUUID().toString().substring(0, 8); + + this.ignorePullError = false; + this.baseUri = "http://127.0.0.1:" + httpPort; + this.gRPCBaseUri = "http://127.0.0.1:" + grpcPort; + this.dockerImageName = DEFAULT_IMAGE_NAME; + this.dockerImageTag = DEFAULT_IMAGE_TAG; + this.containerName = DEFAULT_CONTAINER_NAME + "_" + uuid; } private Builder( From 5ed800ca86804f6bc417e7a31f9d64f33f0f02b6 Mon Sep 17 00:00:00 2001 From: nidhiii-27 Date: Sat, 23 May 2026 15:56:58 +0530 Subject: [PATCH 2/2] refactor code --- .../storage/it/runner/registry/TestBench.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java index 6977370b9519..964c0c5cb020 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java @@ -402,8 +402,9 @@ private void dumpServerLog(String prefix, File out) throws IOException { } private static int findFreePort() { - try (java.net.ServerSocket socket = new java.net.ServerSocket(0)) { + try (java.net.ServerSocket socket = new java.net.ServerSocket()) { socket.setReuseAddress(true); + socket.bind(new java.net.InetSocketAddress("127.0.0.1", 0)); return socket.getLocalPort(); } catch (java.io.IOException e) { throw new RuntimeException("Failed to find a free port", e); @@ -451,8 +452,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://localhost"; + private static final String DEFAULT_GRPC_BASE_URI = "http://localhost"; private static final String DEFAULT_IMAGE_NAME; private static final String DEFAULT_IMAGE_TAG; @@ -504,16 +505,13 @@ static final class Builder { private String containerName; private Builder() { - int httpPort = findFreePort(); - int grpcPort = findFreePort(); - String uuid = java.util.UUID.randomUUID().toString().substring(0, 8); - - this.ignorePullError = false; - this.baseUri = "http://127.0.0.1:" + httpPort; - this.gRPCBaseUri = "http://127.0.0.1:" + grpcPort; - this.dockerImageName = DEFAULT_IMAGE_NAME; - this.dockerImageTag = DEFAULT_IMAGE_TAG; - this.containerName = DEFAULT_CONTAINER_NAME + "_" + uuid; + this( + false, + DEFAULT_BASE_URI + ":" + findFreePort(), + DEFAULT_GRPC_BASE_URI + ":" + findFreePort(), + DEFAULT_IMAGE_NAME, + DEFAULT_IMAGE_TAG, + DEFAULT_CONTAINER_NAME + "_" + java.util.UUID.randomUUID().toString().substring(0, 8)); } private Builder(