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
3234import org .easymock .EasyMock ;
3335import org .junit .After ;
36+ import org .junit .Rule ;
3437import org .junit .Test ;
38+ import org .junit .rules .ExpectedException ;
3539
3640import java .util .concurrent .TimeUnit ;
41+ import java .util .concurrent .TimeoutException ;
3742
3843public class JobTest {
3944
@@ -69,6 +74,9 @@ public class JobTest {
6974 private Job expectedJob ;
7075 private Job job ;
7176
77+ @ Rule
78+ public final ExpectedException thrown = ExpectedException .none ();
79+
7280 private void initializeExpectedJob (int optionsCalls ) {
7381 expect (serviceMockReturnsOptions .options ()).andReturn (mockOptions ).times (optionsCalls );
7482 replay (serviceMockReturnsOptions );
@@ -181,35 +189,38 @@ public void testIsDone_NotExists() throws Exception {
181189 }
182190
183191 @ Test
184- public void testWaitFor () throws InterruptedException {
192+ public void testWaitFor () throws InterruptedException , TimeoutException {
185193 initializeExpectedJob (2 );
186194 BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
187195 JobStatus status = createStrictMock (JobStatus .class );
188196 expect (status .state ()).andReturn (JobStatus .State .DONE );
189197 expect (bigquery .options ()).andReturn (mockOptions );
198+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
190199 Job completedJob = expectedJob .toBuilder ().status (status ).build ();
191200 expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (completedJob );
192201 expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (completedJob );
193- replay (status , bigquery );
202+ replay (status , bigquery , mockOptions );
194203 initializeJob ();
195204 assertSame (completedJob , job .waitFor ());
196- verify (status );
205+ verify (status , mockOptions );
197206 }
198207
199208 @ Test
200- public void testWaitFor_Null () throws InterruptedException {
209+ public void testWaitFor_Null () throws InterruptedException , TimeoutException {
201210 initializeExpectedJob (1 );
202211 BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
203212 expect (bigquery .options ()).andReturn (mockOptions );
213+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
204214 expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (null );
205215 expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (null );
206- replay (bigquery );
216+ replay (bigquery , mockOptions );
207217 initializeJob ();
208218 assertNull (job .waitFor ());
219+ verify (mockOptions );
209220 }
210221
211222 @ Test
212- public void testWaitForWithTimeUnit () throws InterruptedException {
223+ public void testWaitForWithCheckingPeriod () throws InterruptedException , TimeoutException {
213224 initializeExpectedJob (3 );
214225 BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
215226 TimeUnit timeUnit = createStrictMock (TimeUnit .class );
@@ -219,33 +230,62 @@ public void testWaitForWithTimeUnit() throws InterruptedException {
219230 expect (status .state ()).andReturn (JobStatus .State .RUNNING );
220231 expect (status .state ()).andReturn (JobStatus .State .DONE );
221232 expect (bigquery .options ()).andReturn (mockOptions );
233+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
222234 Job runningJob = expectedJob .toBuilder ().status (status ).build ();
223235 Job completedJob = expectedJob .toBuilder ().status (status ).build ();
224236 expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (runningJob );
225237 expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (completedJob );
226238 expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (completedJob );
227- replay (status , bigquery , timeUnit );
239+ replay (status , bigquery , timeUnit , mockOptions );
228240 initializeJob ();
229- assertSame (completedJob , job .waitFor (42 , timeUnit ));
230- verify (status , timeUnit );
241+ assertSame (completedJob , job .waitFor (WaitForOption . checkEvery ( 42 , timeUnit ) ));
242+ verify (status , timeUnit , mockOptions );
231243 }
232244
233245 @ Test
234- public void testWaitForWithTimeUnit_Null () throws InterruptedException {
246+ public void testWaitForWithCheckingPeriod_Null () throws InterruptedException , TimeoutException {
235247 initializeExpectedJob (2 );
236248 BigQuery .JobOption [] expectedOptions = {BigQuery .JobOption .fields (BigQuery .JobField .STATUS )};
237249 TimeUnit timeUnit = createStrictMock (TimeUnit .class );
238250 timeUnit .sleep (42 );
239251 EasyMock .expectLastCall ();
240252 expect (bigquery .options ()).andReturn (mockOptions );
253+ expect (mockOptions .clock ()).andReturn (Clock .defaultClock ());
241254 Job runningJob = expectedJob .toBuilder ().status (new JobStatus (JobStatus .State .RUNNING )).build ();
242255 expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (runningJob );
243256 expect (bigquery .getJob (JOB_INFO .jobId (), expectedOptions )).andReturn (null );
244257 expect (bigquery .getJob (JOB_INFO .jobId ())).andReturn (null );
245- replay (bigquery , timeUnit );
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 );
246284 initializeJob ();
247- assertNull (job .waitFor (42 , timeUnit ));
248- verify (bigquery , timeUnit );
285+ thrown .expect (TimeoutException .class );
286+ job .waitFor (WaitForOption .checkEvery (1 , timeUnit ),
287+ WaitForOption .timeout (3 , TimeUnit .MILLISECONDS ));
288+ verify (status , timeUnit , clock , mockOptions );
249289 }
250290
251291 @ Test
0 commit comments