From d2ad4e65690ca98af3b2f7459383b01bf9dd07a1 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Mon, 20 Jul 2020 18:13:15 -0700 Subject: [PATCH 1/4] samples: fixed automl cancel operation bug --- .../example/automl/CancelOperationTest.java | 50 +++++++++++++++++-- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java index ea1f1dff638..2b063c9ade1 100644 --- a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java +++ b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java @@ -22,6 +22,17 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.InputConfig; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -32,9 +43,11 @@ @RunWith(JUnit4.class) public class CancelOperationTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); - private static final String OPERATION_ID = "TRL6118473245806034944"; - private String operationFullNam = - String.format("projects/%s/locations/us-central1/operations/%s", PROJECT_ID, OPERATION_ID); + private static final String DATASET_ID = "TRL8242630754622767104"; +private static final String BUCKET = "gs://translate_data_exported/translation.csv"; + + private String operationId; + private String operationFullNam; private ByteArrayOutputStream bout; private PrintStream out; @@ -51,15 +64,42 @@ public static void checkRequirements() { } @Before - public void setUp() { + public void setUp() throws InterruptedException, ExecutionException, TimeoutException, IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); + + // start a import data into dataset and cancel it before it finishes. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", DATASET_ID); + + // Get multiple Google Cloud Storage URIs to import data from + GcsSource gcsSource = + GcsSource.newBuilder().addAllInputUris(Arrays.asList(BUCKET.split(","))).build(); + + InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); + + // Start the import job + OperationFuture operation = client + .importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + operationFullNam = + operation.getName(); + } + } @After - public void tearDown() { + public void tearDown() throws IOException { System.setOut(null); + + // delete the cancelled operation + try (AutoMlClient client = AutoMlClient.create()) { + client.getOperationsClient().deleteOperation(operationFullNam); + } } @Test From f3fd54e02f86e31ef46829912517dad31f63a981 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Mon, 20 Jul 2020 18:54:03 -0700 Subject: [PATCH 2/4] fixed the lint issue --- .../example/automl/CancelOperationTest.java | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java index 2b063c9ade1..a5fd07d1582 100644 --- a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java +++ b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java @@ -19,13 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.util.Arrays; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; - import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.automl.v1beta1.AutoMlClient; import com.google.cloud.automl.v1beta1.DatasetName; @@ -33,6 +26,12 @@ import com.google.cloud.automl.v1beta1.InputConfig; import com.google.cloud.automl.v1beta1.OperationMetadata; import com.google.protobuf.Empty; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -44,7 +43,7 @@ public class CancelOperationTest { private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); private static final String DATASET_ID = "TRL8242630754622767104"; -private static final String BUCKET = "gs://translate_data_exported/translation.csv"; + private static final String BUCKET = "gs://translate_data_exported/translation.csv"; private String operationId; private String operationFullNam; @@ -52,9 +51,7 @@ public class CancelOperationTest { private PrintStream out; private static void requireEnvVar(String varName) { - assertNotNull( - System.getenv(varName), - "Environment variable '%s' is required to perform these tests.".format(varName)); + assertNotNull(System.getenv(varName), String.format(varName)); } @BeforeClass @@ -64,7 +61,8 @@ public static void checkRequirements() { } @Before - public void setUp() throws InterruptedException, ExecutionException, TimeoutException, IOException { + public void setUp() + throws InterruptedException, ExecutionException, TimeoutException, IOException { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -76,20 +74,18 @@ public void setUp() throws InterruptedException, ExecutionException, TimeoutExce // Get multiple Google Cloud Storage URIs to import data from GcsSource gcsSource = - GcsSource.newBuilder().addAllInputUris(Arrays.asList(BUCKET.split(","))).build(); + GcsSource.newBuilder().addAllInputUris(Arrays.asList(BUCKET.split(","))).build(); InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); // Start the import job - OperationFuture operation = client - .importDataAsync(datasetFullId, inputConfig); + OperationFuture operation = + client.importDataAsync(datasetFullId, inputConfig); System.out.format("Operation name: %s%n", operation.getName()); - operationFullNam = - operation.getName(); + operationFullNam = operation.getName(); } - } @After From b37bc12b0f78860534212e2b447bc924866e9e1c Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 21 Jul 2020 09:41:29 -0700 Subject: [PATCH 3/4] renamed and added sleep for waiting operation cancellation --- .../test/java/com/example/automl/CancelOperationTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java index a5fd07d1582..e3b838ae7ec 100644 --- a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java +++ b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java @@ -89,17 +89,21 @@ public void setUp() } @After - public void tearDown() throws IOException { + public void tearDown() throws IOException, InterruptedException { System.setOut(null); // delete the cancelled operation try (AutoMlClient client = AutoMlClient.create()) { + // wait for the operation to be cancelled + while(!client.getOperationsClient().getOperation(operationFullNam).getDone()) { + Thread.sleep(1000); + } client.getOperationsClient().deleteOperation(operationFullNam); } } @Test - public void testGetOperationStatus() throws IOException { + public void testCancelOperation() throws IOException { CancelOperation.cancelOperation(operationFullNam); String got = bout.toString(); assertThat(got).contains("Operation cancelled"); From 7dc574c0bc67b8044af3cfed71ca096f58b80d9d Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 21 Jul 2020 09:53:48 -0700 Subject: [PATCH 4/4] fixed the lint prob --- .../src/test/java/com/example/automl/CancelOperationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java index e3b838ae7ec..916e80cbd70 100644 --- a/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java +++ b/automl/beta/src/test/java/com/example/automl/CancelOperationTest.java @@ -95,7 +95,7 @@ public void tearDown() throws IOException, InterruptedException { // delete the cancelled operation try (AutoMlClient client = AutoMlClient.create()) { // wait for the operation to be cancelled - while(!client.getOperationsClient().getOperation(operationFullNam).getDone()) { + while (!client.getOperationsClient().getOperation(operationFullNam).getDone()) { Thread.sleep(1000); } client.getOperationsClient().deleteOperation(operationFullNam);