Skip to content

Commit d19b465

Browse files
authored
fix: Batch issue fix (GoogleCloudPlatform#7816)
* fix: batch bucket test contents * add test annotation * fix: batch timeout issue and job exists issue * increase timeout
1 parent 0f6f837 commit d19b465

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

batch/snippets/src/test/java/BatchBasicIT.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.cloud.batch.v1.BatchServiceClient;
1919
import com.google.cloud.batch.v1.Job;
2020
import com.google.cloud.batch.v1.JobName;
21+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
2122
import java.io.ByteArrayOutputStream;
2223
import java.io.IOException;
2324
import java.io.PrintStream;
@@ -29,6 +30,7 @@
2930
import org.junit.AfterClass;
3031
import org.junit.Before;
3132
import org.junit.BeforeClass;
33+
import org.junit.Rule;
3234
import org.junit.Test;
3335
import org.junit.runner.RunWith;
3436
import org.junit.runners.JUnit4;
@@ -38,11 +40,18 @@ public class BatchBasicIT {
3840

3941
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4042
private static final String REGION = "europe-north1";
43+
private static final int MAX_ATTEMPT_COUNT = 3;
44+
private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes
4145
private static String SCRIPT_JOB_NAME;
4246
private static String CONTAINER_JOB_NAME;
4347

4448
private ByteArrayOutputStream stdOut;
4549

50+
@Rule
51+
public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(
52+
MAX_ATTEMPT_COUNT,
53+
INITIAL_BACKOFF_MILLIS);
54+
4655
// Check if the required environment variables are set.
4756
public static void requireEnvVar(String envVarName) {
4857
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))

batch/snippets/src/test/java/BatchTemplateIT.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ public static void setUp()
102102
// System.out.println("Do nothing");
103103
}
104104

105+
// Create instance templates.
105106
INSTANCE_TEMPLATE = createInstanceTemplate();
106107
TimeUnit.SECONDS.sleep(10);
107108

109+
// Create job with template.
110+
CreateWithTemplate.createWithTemplate(PROJECT_ID, REGION, SCRIPT_JOB_NAME,
111+
INSTANCE_TEMPLATE.getSelfLink());
112+
assertThat(stdOut.toString()).contains("Successfully created the job: ");
113+
108114
stdOut.close();
109115
System.setOut(out);
110116
}
@@ -222,10 +228,8 @@ public void afterEach() {
222228

223229
@Test
224230
public void testCreateWithTemplate()
225-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
226-
CreateWithTemplate.createWithTemplate(PROJECT_ID, REGION, SCRIPT_JOB_NAME,
227-
INSTANCE_TEMPLATE.getSelfLink());
228-
assertThat(stdOut.toString()).contains("Successfully created the job: ");
231+
throws IOException, InterruptedException {
229232
Util.waitForJobCompletion(Util.getJob(PROJECT_ID, REGION, SCRIPT_JOB_NAME));
233+
assertThat(stdOut.toString()).contains("Job completed");
230234
}
231235
}

batch/snippets/src/test/java/Util.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,14 @@ public static void waitForJobCompletion(Job job)
107107
String[] jobName = job.getName().split("/");
108108
Instant startTime = Instant.now();
109109
while (WAIT_STATES.contains(job.getStatus().getState())) {
110-
if (Instant.now().getEpochSecond() - startTime.getEpochSecond() > 500) {
110+
if (Instant.now().getEpochSecond() - startTime.getEpochSecond() > 900) {
111111
throw new Error("Timed out waiting for operation to complete.");
112112
}
113113
job = getJob(jobName[1], jobName[3], jobName[5]);
114-
TimeUnit.SECONDS.sleep(5);
114+
TimeUnit.SECONDS.sleep(10);
115115
}
116116
job = getJob(jobName[1], jobName[3], job.getName().split("/")[5]);
117117
assertThat(job.getStatus().getState() == State.SUCCEEDED);
118+
System.out.println("Job completed.");
118119
}
119120
}

0 commit comments

Comments
 (0)