Skip to content

Commit b939c07

Browse files
committed
Add snippets for waitFor().
This moves the hand-written snippets to JobSnippets and adds IT tests.
1 parent 9a0e9de commit b939c07

File tree

3 files changed

+94
-21
lines changed
  • google-cloud-bigquery/src/main/java/com/google/cloud/bigquery
  • google-cloud-examples/src

3 files changed

+94
-21
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,14 @@ public boolean exists() {
136136

137137
/**
138138
* Checks if this job has completed its execution, either failing or succeeding. If the job does
139-
* not exist this method returns {@code true}. You can wait for job completion with:
139+
* not exist this method returns {@code true}.
140+
*
141+
* <p>Example of waiting for a job until it reports that it is done.
140142
* <pre> {@code
141-
* while(!job.isDone()) {
143+
* while (!job.isDone()) {
142144
* Thread.sleep(1000L);
143-
* }}</pre>
145+
* }
146+
* }</pre>
144147
*
145148
* @return {@code true} if this job is in {@link JobStatus.State#DONE} state or if it does not
146149
* exist, {@code false} if the state is not {@link JobStatus.State#DONE}
@@ -158,7 +161,7 @@ public boolean isDone() {
158161
* value use {@link WaitForOption#checkEvery(long, TimeUnit)}. Use
159162
* {@link WaitForOption#timeout(long, TimeUnit)} to set the maximum time to wait.
160163
*
161-
* <p>Example usage of {@code waitFor()}:
164+
* <p>Example usage of {@code waitFor()}.
162165
* <pre> {@code
163166
* Job completedJob = job.waitFor();
164167
* if (completedJob == null) {
@@ -167,19 +170,23 @@ public boolean isDone() {
167170
* // job failed, handle error
168171
* } else {
169172
* // job completed successfully
170-
* }}</pre>
173+
* }
174+
* }</pre>
171175
*
172-
* <p>Example usage of {@code waitFor()} with checking period and timeout:
176+
* <p>Example usage of {@code waitFor()} with checking period and timeout.
173177
* <pre> {@code
174-
* Job completedJob = job.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS),
175-
* WaitForOption.timeout(60, TimeUnit.SECONDS));
178+
* Job completedJob =
179+
* job.waitFor(
180+
* WaitForOption.checkEvery(1, TimeUnit.SECONDS),
181+
* WaitForOption.timeout(60, TimeUnit.SECONDS));
176182
* if (completedJob == null) {
177183
* // job no longer exists
178184
* } else if (completedJob.status().error() != null) {
179185
* // job failed, handle error
180186
* } else {
181187
* // job completed successfully
182-
* }}</pre>
188+
* }
189+
* }</pre>
183190
*
184191
* @param waitOptions options to configure checking period and timeout
185192
* @throws BigQueryException upon failure

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222

2323
package com.google.cloud.examples.bigquery.snippets;
2424

25+
import com.google.cloud.WaitForOption;
2526
import com.google.cloud.bigquery.Job;
2627

28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
30+
2731
public class JobSnippets {
2832

2933
private final Job job;
@@ -36,16 +40,59 @@ public JobSnippets(Job job) {
3640
* Example of waiting for a job until it reports that it is done.
3741
*/
3842
// [TARGET isDone()]
39-
public boolean isDone() {
43+
public void isDone() throws InterruptedException {
44+
// [START isDone]
45+
while (!job.isDone()) {
46+
Thread.sleep(1000L);
47+
}
48+
// [END isDone]
49+
}
50+
51+
/**
52+
* Example usage of {@code waitFor()}.
53+
*/
54+
// [TARGET waitFor(WaitForOption...)]
55+
public boolean waitFor() throws InterruptedException {
4056
try {
41-
// [START isDone]
42-
while (!job.isDone()) {
43-
Thread.sleep(1000L);
57+
// [START waitFor]
58+
Job completedJob = job.waitFor();
59+
if (completedJob == null) {
60+
// job no longer exists
61+
} else if (completedJob.status().error() != null) {
62+
// job failed, handle error
63+
} else {
64+
// job completed successfully
4465
}
45-
// [END isDone]
46-
} catch (InterruptedException e) {
66+
// [END waitFor]
67+
} catch (TimeoutException e) {
68+
// Timeouts shouldn't happen without a timeout option.
4769
return false;
4870
}
4971
return true;
5072
}
73+
74+
/**
75+
* Example usage of {@code waitFor()} with checking period and timeout.
76+
*/
77+
// [TARGET waitFor(WaitForOption...)]
78+
public boolean waitForWithOptions() throws InterruptedException {
79+
try {
80+
// [START waitFor]
81+
Job completedJob =
82+
job.waitFor(
83+
WaitForOption.checkEvery(1, TimeUnit.SECONDS),
84+
WaitForOption.timeout(60, TimeUnit.SECONDS));
85+
if (completedJob == null) {
86+
// job no longer exists
87+
} else if (completedJob.status().error() != null) {
88+
// job failed, handle error
89+
} else {
90+
// job completed successfully
91+
}
92+
// [END waitFor]
93+
} catch (TimeoutException e) {
94+
return true;
95+
}
96+
return true;
97+
}
5198
}

google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
public class ITJobSnippets {
3232

33+
private static final String QUERY =
34+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;";
35+
3336
private static BigQuery bigquery;
3437

3538
@BeforeClass
@@ -39,15 +42,31 @@ public static void beforeClass() {
3942

4043
@Test
4144
public void testIsDone() throws Exception {
42-
JobConfiguration jobConfig =
43-
QueryJobConfiguration.builder(
44-
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;")
45-
.useLegacySql(false)
46-
.build();
45+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
46+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
47+
Job job = bigquery.create(jobInfo);
48+
JobSnippets jobSnippets = new JobSnippets(job);
49+
jobSnippets.isDone();
50+
assertTrue(job.isDone());
51+
}
52+
53+
@Test
54+
public void testWaitFor() throws Exception {
55+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
56+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
57+
Job job = bigquery.create(jobInfo);
58+
JobSnippets jobSnippets = new JobSnippets(job);
59+
boolean result = jobSnippets.waitFor();
60+
assertTrue(result);
61+
}
62+
63+
@Test
64+
public void testWaitForWithOptions() throws Exception {
65+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
4766
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
4867
Job job = bigquery.create(jobInfo);
4968
JobSnippets jobSnippets = new JobSnippets(job);
50-
boolean result = jobSnippets.isDone();
69+
boolean result = jobSnippets.waitForWithOptions();
5170
assertTrue(result);
5271
}
5372
}

0 commit comments

Comments
 (0)