|
28 | 28 | import static org.junit.Assert.assertTrue; |
29 | 29 |
|
30 | 30 | import com.google.cloud.bigquery.JobStatistics.CopyStatistics; |
31 | | -import com.google.common.collect.ImmutableList; |
32 | 31 |
|
33 | 32 | import org.easymock.EasyMock; |
34 | 33 | import org.junit.After; |
35 | 34 | import org.junit.Test; |
36 | 35 |
|
37 | | -import java.util.List; |
| 36 | +import java.util.concurrent.TimeUnit; |
38 | 37 |
|
39 | 38 | public class JobTest { |
40 | 39 |
|
@@ -182,60 +181,71 @@ public void testIsDone_NotExists() throws Exception { |
182 | 181 | } |
183 | 182 |
|
184 | 183 | @Test |
185 | | - public void testWhenDone_Success() throws InterruptedException { |
| 184 | + public void testWaitFor() throws InterruptedException { |
186 | 185 | initializeExpectedJob(2); |
187 | | - Job.CompletionCallback callback = EasyMock.mock(Job.CompletionCallback.class); |
188 | 186 | BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; |
189 | 187 | JobStatus status = createStrictMock(JobStatus.class); |
190 | 188 | expect(status.state()).andReturn(JobStatus.State.DONE); |
191 | | - expect(status.error()).andReturn(null); |
192 | 189 | expect(bigquery.options()).andReturn(mockOptions); |
193 | 190 | Job completedJob = expectedJob.toBuilder().status(status).build(); |
194 | 191 | expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(completedJob); |
195 | 192 | expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(completedJob); |
196 | | - callback.success(completedJob); |
197 | | - EasyMock.expectLastCall(); |
198 | | - replay(status, bigquery, callback); |
| 193 | + replay(status, bigquery); |
199 | 194 | initializeJob(); |
200 | | - job.whenDone(callback); |
201 | | - verify(status, callback); |
| 195 | + assertSame(completedJob, job.waitFor()); |
| 196 | + verify(status); |
202 | 197 | } |
203 | 198 |
|
204 | 199 | @Test |
205 | | - public void testWhenDone_Error() throws InterruptedException { |
206 | | - initializeExpectedJob(2); |
207 | | - Job.CompletionCallback callback = EasyMock.mock(Job.CompletionCallback.class); |
| 200 | + public void testWaitFor_Null() throws InterruptedException { |
| 201 | + initializeExpectedJob(1); |
| 202 | + BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; |
| 203 | + expect(bigquery.options()).andReturn(mockOptions); |
| 204 | + expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null); |
| 205 | + expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(null); |
| 206 | + replay(bigquery); |
| 207 | + initializeJob(); |
| 208 | + assertNull(job.waitFor()); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void testWaitForWithTimeUnit() throws InterruptedException { |
| 213 | + initializeExpectedJob(3); |
208 | 214 | BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; |
209 | | - BigQueryError error = new BigQueryError("reason", "location", "message"); |
210 | | - List<BigQueryError> executionErrors = ImmutableList.of(error); |
| 215 | + TimeUnit timeUnit = createStrictMock(TimeUnit.class); |
| 216 | + timeUnit.sleep(42); |
| 217 | + EasyMock.expectLastCall(); |
211 | 218 | JobStatus status = createStrictMock(JobStatus.class); |
| 219 | + expect(status.state()).andReturn(JobStatus.State.RUNNING); |
212 | 220 | expect(status.state()).andReturn(JobStatus.State.DONE); |
213 | | - expect(status.error()).andReturn(error); |
214 | | - expect(status.executionErrors()).andReturn(executionErrors); |
215 | 221 | expect(bigquery.options()).andReturn(mockOptions); |
| 222 | + Job runningJob = expectedJob.toBuilder().status(status).build(); |
216 | 223 | Job completedJob = expectedJob.toBuilder().status(status).build(); |
| 224 | + expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(runningJob); |
217 | 225 | expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(completedJob); |
218 | 226 | expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(completedJob); |
219 | | - callback.error(error, executionErrors); |
220 | | - EasyMock.expectLastCall(); |
221 | | - replay(status, bigquery, callback); |
| 227 | + replay(status, bigquery, timeUnit); |
222 | 228 | initializeJob(); |
223 | | - job.whenDone(callback); |
224 | | - verify(status, callback); |
| 229 | + assertSame(completedJob, job.waitFor(42, timeUnit)); |
| 230 | + verify(status, timeUnit); |
225 | 231 | } |
226 | 232 |
|
227 | 233 | @Test |
228 | | - public void testWhenDone_Null() throws InterruptedException { |
229 | | - initializeExpectedJob(1); |
230 | | - Job.CompletionCallback callback = EasyMock.mock(Job.CompletionCallback.class); |
| 234 | + public void testWaitForWithTimeUnit_Null() throws InterruptedException { |
| 235 | + initializeExpectedJob(2); |
231 | 236 | BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; |
| 237 | + TimeUnit timeUnit = createStrictMock(TimeUnit.class); |
| 238 | + timeUnit.sleep(42); |
| 239 | + EasyMock.expectLastCall(); |
232 | 240 | expect(bigquery.options()).andReturn(mockOptions); |
| 241 | + Job runningJob = expectedJob.toBuilder().status(new JobStatus(JobStatus.State.RUNNING)).build(); |
| 242 | + expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(runningJob); |
233 | 243 | expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null); |
234 | 244 | expect(bigquery.getJob(JOB_INFO.jobId())).andReturn(null); |
235 | | - replay(bigquery, callback); |
| 245 | + replay(bigquery, timeUnit); |
236 | 246 | initializeJob(); |
237 | | - job.whenDone(callback); |
238 | | - verify(callback); |
| 247 | + assertNull(job.waitFor(42, timeUnit)); |
| 248 | + verify(bigquery, timeUnit); |
239 | 249 | } |
240 | 250 |
|
241 | 251 | @Test |
|
0 commit comments