From bd5241acf6eaf629f46724c06aad4973a0c273e3 Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Mon, 31 Aug 2020 17:55:27 -0700 Subject: [PATCH] samples: fixed flaky listmodel by increasing the timeout (#286) * samples: fixed flaky listmodel by increasing the timeout * fixed the lint issue --- .../java/com/example/automl/ListModels.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/automl/snippets/src/main/java/com/example/automl/ListModels.java b/automl/snippets/src/main/java/com/example/automl/ListModels.java index d42bb0b4e9f..9b2e5427c3b 100644 --- a/automl/snippets/src/main/java/com/example/automl/ListModels.java +++ b/automl/snippets/src/main/java/com/example/automl/ListModels.java @@ -18,10 +18,12 @@ // [START automl_list_models] import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.AutoMlSettings; import com.google.cloud.automl.v1.ListModelsRequest; import com.google.cloud.automl.v1.LocationName; import com.google.cloud.automl.v1.Model; import java.io.IOException; +import org.threeten.bp.Duration; class ListModels { @@ -36,12 +38,25 @@ static void listModels(String projectId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. - try (AutoMlClient client = AutoMlClient.create()) { + AutoMlSettings.Builder autoMlSettingsBuilder = AutoMlSettings.newBuilder(); + + autoMlSettingsBuilder + .listModelsSettings() + .setRetrySettings( + autoMlSettingsBuilder + .listModelsSettings() + .getRetrySettings() + .toBuilder() + .setTotalTimeout(Duration.ofSeconds(20)) + .build()); + AutoMlSettings autoMlSettings = autoMlSettingsBuilder.build(); + + try (AutoMlClient client = AutoMlClient.create(autoMlSettings)) { // A resource that represents Google Cloud Platform location. LocationName projectLocation = LocationName.of(projectId, "us-central1"); // Create list models request. - ListModelsRequest listModlesRequest = + ListModelsRequest listModelsRequest = ListModelsRequest.newBuilder() .setParent(projectLocation.toString()) .setFilter("") @@ -49,7 +64,7 @@ static void listModels(String projectId) throws IOException { // List all the models available in the region by applying filter. System.out.println("List of models:"); - for (Model model : client.listModels(listModlesRequest).iterateAll()) { + for (Model model : client.listModels(listModelsRequest).iterateAll()) { // Display the model information. System.out.format("Model name: %s\n", model.getName()); // To get the model id, you have to parse it out of the `name` field. As models Ids are