Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
samples: refactor importDataset test service randomly throws cancel e… (
Browse files Browse the repository at this point in the history
#281)

* samples: refactor importDataset test service randomly throws cancel exception sometimes

* fixed the lint
  • Loading branch information
munkhuushmgl committed Aug 29, 2020
1 parent 8eb5e62 commit 34e7cdd
Showing 1 changed file with 48 additions and 11 deletions.
Expand Up @@ -19,11 +19,20 @@
import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.api.core.ApiFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.CreateDatasetRequest;
import com.google.cloud.automl.v1beta1.Dataset;
import com.google.cloud.automl.v1beta1.LocationName;
import com.google.cloud.automl.v1beta1.TextExtractionDatasetMetadata;
import com.google.longrunning.Operation;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -56,20 +65,34 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws InterruptedException, ExecutionException, IOException {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);

// Create a dataset that can be used for the import test
public void setUp()
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// Create a fake dataset to be deleted
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
// To prevent name collisions when running tests in multiple java versions at once.
// AutoML doesn't allow "-", but accepts "_"
String datasetName =
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
LanguageEntityExtractionCreateDataset.createDataset(PROJECT_ID, datasetName);
String got = bout.toString();
datasetId = got.split("Dataset id: ")[1].split("\n")[0];
try (AutoMlClient client = AutoMlClient.create()) {

LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build();
Dataset dataset =
Dataset.newBuilder()
.setDisplayName(datasetName)
.setTextExtractionDatasetMetadata(metadata)
.build();

CreateDatasetRequest request =
CreateDatasetRequest.newBuilder()
.setParent(projectLocation.toString())
.setDataset(dataset)
.build();
ApiFuture<Dataset> future = client.createDatasetCallable().futureCall(request);
Dataset createdDataset = future.get(5, TimeUnit.MINUTES);
String[] names = createdDataset.getName().split("/");
datasetId = names[names.length - 1];
}

bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
Expand All @@ -85,9 +108,23 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept

@Test
public void testImportDataset()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv");
throws InterruptedException, ExecutionException, TimeoutException, IOException {

try {
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv");
} catch (CancellationException ex) {
// capture operation ID from output and wait for that operation to be finished.
String fullOperationId = ex.getMessage().split("Operation name: ")[1].trim();
AutoMlClient client = AutoMlClient.create();
Operation importDatasetLro = client.getOperationsClient().getOperation(fullOperationId);
while (!importDatasetLro.getDone()) {
Thread.sleep(3000);
}
// retry the import.
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv");
}
String got = bout.toString();

assertThat(got).contains("Dataset imported.");
}
}

0 comments on commit 34e7cdd

Please sign in to comment.