Skip to content

Commit 5275013

Browse files
authored
Fix Flaky DLP tests (#2447)
* modified tests to cancel job * added delay to tests * added comment
1 parent 062c29f commit 5275013

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

dlp/src/test/java/dlp/snippets/InspectTests.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import static org.hamcrest.core.StringContains.containsString;
2121
import static org.junit.Assert.assertNotNull;
2222

23+
import com.google.cloud.dlp.v2.DlpServiceClient;
24+
import com.google.privacy.dlp.v2.CancelDlpJobRequest;
2325
import java.io.ByteArrayOutputStream;
2426
import java.io.IOException;
2527
import java.io.PrintStream;
2628
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
2730
import org.junit.After;
2831
import org.junit.Before;
2932
import org.junit.Test;
@@ -100,31 +103,59 @@ public void testInspectImageFile() {
100103

101104
@Test
102105
public void testInspectGcsFile() throws InterruptedException, ExecutionException, IOException {
106+
103107
InspectGcsFile.inspectGcsFile(PROJECT_ID, GCS_PATH, TOPIC_ID, SUBSCRIPTION_ID);
108+
// Await job creation
109+
TimeUnit.SECONDS.sleep(3);
104110

105111
String output = bout.toString();
106-
assertThat(output, containsString("Info type: PHONE_NUMBER"));
107-
assertThat(output, containsString("Info type: EMAIL_ADDRESS"));
112+
assertThat(output, containsString("Job created: "));
113+
114+
// Cancelling the job early to conserve quota
115+
String jobId = output.split("Job created: ")[1].split("\n")[0];
116+
CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build();
117+
try (DlpServiceClient client = DlpServiceClient.create()) {
118+
client.cancelDlpJob(request);
119+
}
108120
}
109121

110122
@Test
111123
public void testInspectDatastoreEntity()
112124
throws InterruptedException, ExecutionException, IOException {
125+
113126
InspectDatastoreEntity.insepctDatastoreEntity(
114127
PROJECT_ID, datastoreNamespace, datastoreKind, TOPIC_ID, SUBSCRIPTION_ID);
128+
// Await job creation
129+
TimeUnit.SECONDS.sleep(3);
115130

116131
String output = bout.toString();
117-
assertThat(output, containsString("Info type: PHONE_NUMBER"));
118-
assertThat(output, containsString("Info type: EMAIL_ADDRESS"));
132+
assertThat(output, containsString("Job created: "));
133+
134+
// Cancelling the job early to conserve quota
135+
String jobId = output.split("Job created: ")[1].split("\n")[0];
136+
CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build();
137+
try (DlpServiceClient client = DlpServiceClient.create()) {
138+
client.cancelDlpJob(request);
139+
}
119140
}
120141

121142
@Test
122143
public void testInspectBigQueryTable()
123144
throws InterruptedException, ExecutionException, IOException {
145+
124146
InspectBigQueryTable.inspectBigQueryTable(
125147
PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID);
148+
// Await job creation
149+
TimeUnit.SECONDS.sleep(3);
126150

127151
String output = bout.toString();
128-
assertThat(output, containsString("Info type: PHONE_NUMBER"));
152+
assertThat(output, containsString("Job created: "));
153+
154+
// Cancelling the job early to conserve quota
155+
String jobId = output.split("Job created: ")[1].split("\n")[0];
156+
CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build();
157+
try (DlpServiceClient client = DlpServiceClient.create()) {
158+
client.cancelDlpJob(request);
159+
}
129160
}
130161
}

0 commit comments

Comments
 (0)