From 934c6b4dd259e18d1aee88abbffd4b88d1b70343 Mon Sep 17 00:00:00 2001 From: dfirova <93149631+dfirova@users.noreply.github.com> Date: Mon, 26 Sep 2022 15:04:17 +0300 Subject: [PATCH] fix(samples): Removed delays on LRO (#527) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix (samples) Removed delays on LRO. * fix (samples) removed delays on lro. * pr fix: replaced while with polling. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * pr fix: fixed tests. * pr fix: fixed test. Co-authored-by: Owl Bot --- .../java/product/AddFulfillmentPlaces.java | 9 +++++---- .../java/product/RemoveFulfillmentPlaces.java | 9 +++++---- .../src/main/java/product/SetInventory.java | 20 ++++++++----------- .../product/AddFulfillmentPlacesTest.java | 2 +- .../product/RemoveFulfillmentPlacesTest.java | 2 +- .../test/java/product/SetInventoryTest.java | 2 +- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java b/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java index d33562b2..0f871965 100644 --- a/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java +++ b/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java @@ -25,7 +25,7 @@ import com.google.cloud.retail.v2.ProductServiceClient; import java.io.IOException; import java.util.UUID; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutionException; public class AddFulfillmentPlaces { @@ -72,11 +72,12 @@ public static void addFulfillmentPlaces(String productName, String placeId) // completing all of your requests, call the "close" method on the client to // safely clean up any remaining background resources. try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - serviceClient.addFulfillmentPlacesAsync(addFulfillmentPlacesRequest); // This is a long-running operation and its result is not immediately // present with get operations,thus we simulate wait with sleep method. - System.out.println("Add fulfillment places, wait 45 seconds: "); - TimeUnit.SECONDS.sleep(45); + System.out.println("Waiting for operation to finish..."); + serviceClient.addFulfillmentPlacesAsync(addFulfillmentPlacesRequest).getPollingFuture().get(); + } catch (ExecutionException e) { + System.out.printf("Exception occurred during longrunning operation: %s%n", e.getMessage()); } } } diff --git a/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java b/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java index eb8d23a0..6a532dca 100644 --- a/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java +++ b/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java @@ -25,7 +25,7 @@ import com.google.cloud.retail.v2.RemoveFulfillmentPlacesRequest; import java.io.IOException; import java.util.UUID; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutionException; public class RemoveFulfillmentPlaces { @@ -73,11 +73,12 @@ public static void removeFulfillmentPlaces(String productName, String storeId) // completing all of your requests, call the "close" method on the client to // safely clean up any remaining background resources. try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest); // This is a long-running operation and its result is not immediately // present with get operations,thus we simulate wait with sleep method. - System.out.println("Remove fulfillment places, wait 30 seconds."); - TimeUnit.SECONDS.sleep(30); + System.out.println("Waiting for operation to finish..."); + serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest).getPollingFuture().get(); + } catch (ExecutionException e) { + System.out.printf("Exception occurred during longrunning operation: %s%n", e.getMessage()); } } } diff --git a/samples/interactive-tutorials/src/main/java/product/SetInventory.java b/samples/interactive-tutorials/src/main/java/product/SetInventory.java index 513ba27d..583df6b0 100644 --- a/samples/interactive-tutorials/src/main/java/product/SetInventory.java +++ b/samples/interactive-tutorials/src/main/java/product/SetInventory.java @@ -21,18 +21,14 @@ import static setup.SetupCleanup.getProduct; import com.google.cloud.ServiceOptions; -import com.google.cloud.retail.v2.FulfillmentInfo; -import com.google.cloud.retail.v2.PriceInfo; -import com.google.cloud.retail.v2.Product; +import com.google.cloud.retail.v2.*; import com.google.cloud.retail.v2.Product.Availability; -import com.google.cloud.retail.v2.ProductServiceClient; -import com.google.cloud.retail.v2.SetInventoryRequest; import com.google.protobuf.FieldMask; import com.google.protobuf.Int32Value; import java.io.IOException; import java.util.Arrays; import java.util.UUID; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutionException; public class SetInventory { @@ -106,12 +102,12 @@ public static void setInventory(String productName) throws IOException, Interrup // completing all of your requests, call the "close" method on the client to // safely clean up any remaining background resources. try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - serviceClient.setInventoryAsync(setInventoryRequest); + // This is a long-running operation and its result is not immediately + // present with get operations,thus we simulate wait with sleep method. + System.out.println("Waiting for operation to finish..."); + serviceClient.setInventoryAsync(setInventoryRequest).getPollingFuture().get(); + } catch (ExecutionException e) { + System.out.printf("Exception occurred during longrunning operation: %s%n", e.getMessage()); } - - // This is a long-running operation and its result is not immediately - // present with get operations,thus we simulate wait with sleep method. - System.out.println("Set inventory, wait 30 seconds."); - TimeUnit.SECONDS.sleep(30); } } diff --git a/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java b/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java index ea17c286..c80d7d65 100644 --- a/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java +++ b/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java @@ -65,7 +65,7 @@ public void testAddFulfillment() { String outputResult = bout.toString(); assertThat(outputResult).contains("Add fulfilment places"); - assertThat(outputResult).contains("Add fulfillment places, wait 45 seconds"); + assertThat(outputResult).contains("Waiting for operation to finish..."); } @After diff --git a/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java b/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java index bddaca37..fd4d041e 100644 --- a/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java +++ b/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java @@ -64,7 +64,7 @@ public void testRemoveFulfillmentPlaces() { String outputResult = bout.toString(); assertThat(outputResult).contains("Remove fulfilment places with current date"); - assertThat(outputResult).contains("Remove fulfillment places, wait 30 seconds"); + assertThat(outputResult).contains("Waiting for operation to finish..."); assertThat(outputResult).contains("Delete product request name"); assertThat(outputResult).contains("was deleted"); } diff --git a/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java b/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java index 747da07e..12d0f716 100644 --- a/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java +++ b/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java @@ -64,7 +64,7 @@ public void testSetInventoryTest() { String outputResult = bout.toString(); assertThat(outputResult).contains("Set inventory request"); - assertThat(outputResult).contains("Set inventory, wait 30 seconds"); + assertThat(outputResult).contains("Waiting for operation to finish..."); } @After