From 05b0f0039afb3d2105abbaa7286be10151905b8c Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Wed, 25 Nov 2026 22:17:31 +0000 Subject: [PATCH 1/2] test(compute): add JUnit 5 integration tests for WaitForOperation Add compute/cloud-client/src/test/java/compute/WaitForOperationIT.java covering the Compute Engine WaitForOperation sample: - Waiting for an in-progress zonal disk creation operation to reach DONE - Handling an operation that is already in Status.DONE - Verifying NotFoundException is thrown for a non-existent operation Uses an AutoCloseable TempZonalDisk fixture with try-with-resources to isolate and clean up temporary disks per test, and documents required environment variables in class Javadoc. --- .../test/java/compute/WaitForOperationIT.java | 224 ++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 compute/cloud-client/src/test/java/compute/WaitForOperationIT.java diff --git a/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java b/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java new file mode 100644 index 00000000000..e3fa29383ac --- /dev/null +++ b/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java @@ -0,0 +1,224 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package compute; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; +import static compute.Util.getZone; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.compute.v1.Disk; +import com.google.cloud.compute.v1.DisksClient; +import com.google.cloud.compute.v1.InsertDiskRequest; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; +import com.google.cloud.compute.v1.ZoneOperationsClient; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +/** + * Integration tests for {@link WaitForOperation}. + * + *

Environment variables for local execution: + * + *

+ */ +@Timeout(value = 5, unit = TimeUnit.MINUTES) +public class WaitForOperationIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String ZONE = getZone(); + private static final String DISK_NAME_PREFIX = "operation-create-zonal-disk-"; + + private ByteArrayOutputStream stdOut; + private PrintStream originalOut; + + /** + * Auto-closing fixture that provisions a temporary zonal disk for a test and ensures the disk is + * deleted on close after creation settles. + */ + private static final class TempZonalDisk implements AutoCloseable { + private final String projectId; + private final String zone; + private final String diskName; + private Operation latestOperation; + + private TempZonalDisk( + String projectId, String zone, String diskName, Operation initialOperation) { + this.projectId = projectId; + this.zone = zone; + this.diskName = diskName; + this.latestOperation = initialOperation; + } + + /** + * Initiates creation of a temporary 10 GB standard zonal disk without waiting for completion. + * + * @param projectId Google Cloud project ID. + * @param zone Compute Engine zone in which to create the disk. + * @return a handle holding the initial creation {@link Operation}. + */ + static TempZonalDisk create(String projectId, String zone) throws IOException { + String diskName = DISK_NAME_PREFIX + UUID.randomUUID().toString().substring(0, 8); + String diskType = String.format("zones/%s/diskTypes/pd-standard", zone); + + try (DisksClient disksClient = DisksClient.create()) { + Disk disk = + Disk.newBuilder() + .setName(diskName) + .setZone(zone) + .setType(diskType) + .setSizeGb(10L) + .build(); + InsertDiskRequest request = + InsertDiskRequest.newBuilder() + .setProject(projectId) + .setZone(zone) + .setDiskResource(disk) + .build(); + Operation initialOperation = disksClient.insertCallable().call(request); + return new TempZonalDisk(projectId, zone, diskName, initialOperation); + } + } + + Operation operation() { + return latestOperation; + } + + /** + * Polls Compute Engine until the disk creation operation reaches {@link Status#DONE}. + * + * @return the completed {@link Operation}. + */ + Operation waitUntilDone() throws IOException { + if (latestOperation.getStatus() == Status.DONE) { + return latestOperation; + } + try (ZoneOperationsClient zoneOperationsClient = ZoneOperationsClient.create()) { + while (latestOperation.getStatus() != Status.DONE) { + latestOperation = + zoneOperationsClient.wait(projectId, zone, latestOperation.getName()); + } + } + return latestOperation; + } + + /** Waits for disk creation to finish if still in progress, then deletes the temporary disk. */ + @Override + public void close() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + waitUntilDone(); + try (DisksClient disksClient = DisksClient.create()) { + disksClient.deleteAsync(projectId, zone, diskName).get(3, TimeUnit.MINUTES); + } catch (ExecutionException e) { + if (!(e.getCause() instanceof NotFoundException)) { + throw e; + } + } + } + } + + private static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s'", envVarName)) + .that(System.getenv(envVarName)) + .isNotEmpty(); + } + + @BeforeAll + public static void checkEnvironment() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @BeforeEach + public void setUp() { + stdOut = new ByteArrayOutputStream(); + originalOut = System.out; + System.setOut(new PrintStream(stdOut, true, StandardCharsets.UTF_8)); + } + + @AfterEach + public void tearDown() { + System.out.flush(); + System.setOut(originalOut); + } + + /** Verifies waiting on an in-progress zonal disk creation operation completes with DONE. */ + @Test + public void testWaitForOperation() throws Exception { + try (TempZonalDisk disk = TempZonalDisk.create(PROJECT_ID, ZONE)) { + stdOut.reset(); + WaitForOperation.waitForOperation(PROJECT_ID, disk.operation()); + + String output = stdOut.toString(StandardCharsets.UTF_8); + assertThat(output).contains("Operation Status: DONE"); + assertThat(output).doesNotContain("Error"); + } + } + + /** Verifies passing an already-completed zonal operation prints DONE without re-waiting. */ + @Test + public void testWaitForOperationAlreadyDone() throws Exception { + try (TempZonalDisk disk = TempZonalDisk.create(PROJECT_ID, ZONE)) { + Operation doneOperation = disk.waitUntilDone(); + assertThat(doneOperation.getStatus()).isEqualTo(Status.DONE); + + stdOut.reset(); + WaitForOperation.waitForOperation(PROJECT_ID, doneOperation); + + String output = stdOut.toString(StandardCharsets.UTF_8); + assertThat(output).contains("Operation Status: DONE"); + assertThat(output).doesNotContain("Error"); + } + } + + /** Verifies waiting on a non-existent zonal operation raises {@link NotFoundException}. */ + @Test + public void testWaitForOperationInvalid() { + Operation invalidOperation = + Operation.newBuilder() + .setName("non-existent-operation-" + UUID.randomUUID()) + .setZone( + String.format( + "https://www.googleapis.com/compute/v1/projects/%s/zones/%s", + PROJECT_ID, ZONE)) + .build(); + + assertThrows( + NotFoundException.class, + () -> WaitForOperation.waitForOperation(PROJECT_ID, invalidOperation)); + } +} From f5d74f06ce8932fbaa25e8583c078ae0eb4033bc Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Thu, 26 Nov 2026 00:20:27 +0000 Subject: [PATCH 2/2] test(compute): harden TempZonalDisk teardown and tighten WaitForOperationIT cases Address code review feedback on WaitForOperationIT: - Bound TempZonalDisk cleanup polling with a 3-minute deadline and 1s backoff, make initialOperation immutable, and ensure disk deletion still runs if waiting fails while preserving suppressed exceptions and thread interrupt status - Use a synthetic non-existent Status.DONE Operation in testWaitForOperationAlreadyDone to avoid provisioning a live disk and fail with NotFoundException if WaitForOperation ever re-waits - Explicitly set Status.RUNNING on the invalid Operation test case --- .../test/java/compute/WaitForOperationIT.java | 97 +++++++++++++------ 1 file changed, 69 insertions(+), 28 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java b/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java index e3fa29383ac..8d3a5b33f26 100644 --- a/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java +++ b/compute/cloud-client/src/test/java/compute/WaitForOperationIT.java @@ -32,6 +32,8 @@ import java.io.IOException; import java.io.PrintStream; import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -75,14 +77,14 @@ private static final class TempZonalDisk implements AutoCloseable { private final String projectId; private final String zone; private final String diskName; - private Operation latestOperation; + private final Operation initialOperation; private TempZonalDisk( String projectId, String zone, String diskName, Operation initialOperation) { this.projectId = projectId; this.zone = zone; this.diskName = diskName; - this.latestOperation = initialOperation; + this.initialOperation = initialOperation; } /** @@ -116,38 +118,72 @@ static TempZonalDisk create(String projectId, String zone) throws IOException { } Operation operation() { - return latestOperation; + return initialOperation; } - /** - * Polls Compute Engine until the disk creation operation reaches {@link Status#DONE}. - * - * @return the completed {@link Operation}. - */ - Operation waitUntilDone() throws IOException { - if (latestOperation.getStatus() == Status.DONE) { - return latestOperation; + private void waitUntilDone(Duration timeout) + throws IOException, InterruptedException, TimeoutException { + Operation current = initialOperation; + if (current.getStatus() == Status.DONE) { + return; } + Instant deadline = Instant.now().plus(timeout); try (ZoneOperationsClient zoneOperationsClient = ZoneOperationsClient.create()) { - while (latestOperation.getStatus() != Status.DONE) { - latestOperation = - zoneOperationsClient.wait(projectId, zone, latestOperation.getName()); + while (current.getStatus() != Status.DONE) { + if (Instant.now().isAfter(deadline)) { + throw new TimeoutException( + "Timed out waiting for disk operation " + current.getName()); + } + current = zoneOperationsClient.wait(projectId, zone, current.getName()); + if (current.getStatus() != Status.DONE) { + TimeUnit.SECONDS.sleep(1); + } } } - return latestOperation; } /** Waits for disk creation to finish if still in progress, then deletes the temporary disk. */ @Override - public void close() - throws IOException, ExecutionException, InterruptedException, TimeoutException { - waitUntilDone(); + public void close() throws Exception { + Exception waitError = null; + boolean interrupted = false; + try { + waitUntilDone(Duration.ofMinutes(3)); + } catch (InterruptedException e) { + interrupted = true; + waitError = e; + } catch (Exception e) { + waitError = e; + } + try (DisksClient disksClient = DisksClient.create()) { disksClient.deleteAsync(projectId, zone, diskName).get(3, TimeUnit.MINUTES); } catch (ExecutionException e) { if (!(e.getCause() instanceof NotFoundException)) { + if (waitError != null) { + e.addSuppressed(waitError); + } throw e; } + } catch (InterruptedException e) { + interrupted = true; + if (waitError != null) { + e.addSuppressed(waitError); + } + throw e; + } catch (Exception e) { + if (waitError != null) { + e.addSuppressed(waitError); + } + throw e; + } finally { + if (interrupted) { + Thread.currentThread().interrupt(); + } + } + + if (waitError != null) { + throw waitError; } } } @@ -191,18 +227,22 @@ public void testWaitForOperation() throws Exception { /** Verifies passing an already-completed zonal operation prints DONE without re-waiting. */ @Test - public void testWaitForOperationAlreadyDone() throws Exception { - try (TempZonalDisk disk = TempZonalDisk.create(PROJECT_ID, ZONE)) { - Operation doneOperation = disk.waitUntilDone(); - assertThat(doneOperation.getStatus()).isEqualTo(Status.DONE); + public void testWaitForOperationAlreadyDone() throws IOException { + Operation doneOperation = + Operation.newBuilder() + .setName("non-existent-done-operation-" + UUID.randomUUID()) + .setZone( + String.format( + "https://www.googleapis.com/compute/v1/projects/%s/zones/%s", + PROJECT_ID, ZONE)) + .setStatus(Status.DONE) + .build(); - stdOut.reset(); - WaitForOperation.waitForOperation(PROJECT_ID, doneOperation); + WaitForOperation.waitForOperation(PROJECT_ID, doneOperation); - String output = stdOut.toString(StandardCharsets.UTF_8); - assertThat(output).contains("Operation Status: DONE"); - assertThat(output).doesNotContain("Error"); - } + String output = stdOut.toString(StandardCharsets.UTF_8); + assertThat(output).contains("Operation Status: DONE"); + assertThat(output).doesNotContain("Error"); } /** Verifies waiting on a non-existent zonal operation raises {@link NotFoundException}. */ @@ -215,6 +255,7 @@ public void testWaitForOperationInvalid() { String.format( "https://www.googleapis.com/compute/v1/projects/%s/zones/%s", PROJECT_ID, ZONE)) + .setStatus(Status.RUNNING) .build(); assertThrows(