2727import static org .junit .Assert .assertSame ;
2828import static org .junit .Assert .assertTrue ;
2929
30+ import com .google .cloud .Clock ;
31+ import com .google .cloud .WaitForOption ;
3032import com .google .cloud .bigquery .JobStatistics .CopyStatistics ;
3133
34+ import org .easymock .EasyMock ;
3235import org .junit .After ;
36+ import org .junit .Rule ;
3337import org .junit .Test ;
38+ import org .junit .rules .ExpectedException ;
39+
40+ import java .util .concurrent .TimeUnit ;
41+ import java .util .concurrent .TimeoutException ;
3442
3543public class JobTest {
3644
@@ -66,6 +74,9 @@ public class JobTest {
6674 private Job expectedJob ;
6775 private Job job ;
6876
77+ @ Rule
78+ public final ExpectedException thrown = ExpectedException .none ();
79+
6980 private void initializeExpectedJob (int optionsCalls ) {
7081 expect (serviceMockReturnsOptions .options ()).andReturn (mockOptions ).times (optionsCalls );
7182 replay (serviceMockReturnsOptions );
@@ -177,13 +188,113 @@ public void testIsDone_NotExists() throws Exception {
177188 assertTrue (job .isDone ());
178189 }
179190
191+ @ Test
192+ public void testWaitFor () throws InterruptedException , TimeoutException {
193+ initializeExpectedJob (2 );
194+ BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
195+ JobStatus status = createStrictMock (JobStatus .class );
196+ expect (status .state ()).andReturn (JobStatus .State .DONE );
197+ expect (bigquery .options ()).andReturn (mockOptions );
198+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
199+ Job completedJob = expectedJob .toBuilder ().status (status ).build ();
200+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (completedJob );
201+ expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (completedJob );
202+ replay (status , bigquery , mockOptions );
203+ initializeJob ();
204+ assertSame (completedJob , job .waitFor ());
205+ verify (status , mockOptions );
206+ }
207+
208+ @ Test
209+ public void testWaitFor_Null () throws InterruptedException , TimeoutException {
210+ initializeExpectedJob (1 );
211+ BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
212+ expect (bigquery .options ()).andReturn (mockOptions );
213+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
214+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (null );
215+ expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (null );
216+ replay (bigquery , mockOptions );
217+ initializeJob ();
218+ assertNull (job .waitFor ());
219+ verify (mockOptions );
220+ }
221+
222+ @ Test
223+ public void testWaitForWithCheckingPeriod () throws InterruptedException , TimeoutException {
224+ initializeExpectedJob (3 );
225+ BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
226+ TimeUnit timeUnit = createStrictMock (TimeUnit .class );
227+ timeUnit .sleep (42 );
228+ EasyMock .expectLastCall ();
229+ JobStatus status = createStrictMock (JobStatus .class );
230+ expect (status .state ()).andReturn (JobStatus .State .RUNNING );
231+ expect (status .state ()).andReturn (JobStatus .State .DONE );
232+ expect (bigquery .options ()).andReturn (mockOptions );
233+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
234+ Job runningJob = expectedJob .toBuilder ().status (status ).build ();
235+ Job completedJob = expectedJob .toBuilder ().status (status ).build ();
236+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (runningJob );
237+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (completedJob );
238+ expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (completedJob );
239+ replay (status , bigquery , timeUnit , mockOptions );
240+ initializeJob ();
241+ assertSame (completedJob , job .waitFor (WaitForOption .checkEvery (42 , timeUnit )));
242+ verify (status , timeUnit , mockOptions );
243+ }
244+
245+ @ Test
246+ public void testWaitForWithCheckingPeriod_Null () throws InterruptedException , TimeoutException {
247+ initializeExpectedJob (2 );
248+ BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
249+ TimeUnit timeUnit = createStrictMock (TimeUnit .class );
250+ timeUnit .sleep (42 );
251+ EasyMock .expectLastCall ();
252+ expect (bigquery .options ()).andReturn (mockOptions );
253+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
254+ Job runningJob = expectedJob .toBuilder ().status (new JobStatus (JobStatus .State .RUNNING )).build ();
255+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (runningJob );
256+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (null );
257+ expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (null );
258+ replay (bigquery , timeUnit , mockOptions );
259+ initializeJob ();
260+ assertNull (job .waitFor (WaitForOption .checkEvery (42 , timeUnit )));
261+ verify (bigquery , timeUnit , mockOptions );
262+ }
263+
264+ @ Test
265+ public void testWaitForWithTimeout () throws InterruptedException , TimeoutException {
266+ initializeExpectedJob (2 );
267+ BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
268+ TimeUnit timeUnit = createStrictMock (TimeUnit .class );
269+ timeUnit .sleep (1 );
270+ EasyMock .expectLastCall ();
271+ Clock clock = createStrictMock (Clock .class );
272+ expect (clock .millis ()).andReturn (0L );
273+ expect (clock .millis ()).andReturn (1L );
274+ expect (clock .millis ()).andReturn (3L );
275+ JobStatus status = createStrictMock (JobStatus .class );
276+ expect (status .state ()).andReturn (JobStatus .State .RUNNING );
277+ expect (status .state ()).andReturn (JobStatus .State .RUNNING );
278+ expect (bigquery .options ()).andReturn (mockOptions );
279+ expect (mockOptions .clock ()).andReturn (clock );
280+ Job runningJob = expectedJob .toBuilder ().status (status ).build ();
281+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (runningJob );
282+ expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (runningJob );
283+ replay (status , bigquery , timeUnit , clock , mockOptions );
284+ initializeJob ();
285+ thrown .expect (TimeoutException .class );
286+ job .waitFor (WaitForOption .checkEvery (1 , timeUnit ),
287+ WaitForOption .timeout (3 , TimeUnit .MILLISECONDS ));
288+ verify (status , timeUnit , clock , mockOptions );
289+ }
290+
180291 @ Test
181292 public void testReload () throws Exception {
182293 initializeExpectedJob (4 );
183294 JobInfo updatedInfo = JOB_INFO .toBuilder ().etag ("etag" ).build ();
184295 Job expectedJob = new Job (serviceMockReturnsOptions , new JobInfo .BuilderImpl (updatedInfo ));
185296 expect (bigquery .options ()).andReturn (mockOptions );
186- expect (bigquery .getJob (JOB_INFO .jobId (). job () )).andReturn (expectedJob );
297+ expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (expectedJob );
187298 replay (bigquery );
188299 initializeJob ();
189300 Job updatedJob = job .reload ();
@@ -194,7 +305,7 @@ public void testReload() throws Exception {
194305 public void testReloadNull () throws Exception {
195306 initializeExpectedJob (1 );
196307 expect (bigquery .options ()).andReturn (mockOptions );
197- expect (bigquery .getJob (JOB_INFO .jobId (). job () )).andReturn (null );
308+ expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (null );
198309 replay (bigquery );
199310 initializeJob ();
200311 assertNull (job .reload ());
@@ -206,7 +317,7 @@ public void testReloadWithOptions() throws Exception {
206317 JobInfo updatedInfo = JOB_INFO .toBuilder ().etag ("etag" ).build ();
207318 Job expectedJob = new Job (serviceMockReturnsOptions , new JobInfo .BuilderImpl (updatedInfo ));
208319 expect (bigquery .options ()).andReturn (mockOptions );
209- expect (bigquery .getJob (JOB_INFO .jobId (). job () , BigQuery .JobOption .fields ()))
320+ expect (bigquery .getJob (JOB_INFO .jobId (), BigQuery .JobOption .fields ()))
210321 .andReturn (expectedJob );
211322 replay (bigquery );
212323 initializeJob ();
0 commit comments