@Before public void setUp() throws IOException { MockitoAnnotations.initMocks(this); when(mockBigqueryClient.jobs()).thenReturn(mockJobs); when(mockJobs.query(anyString(), any(QueryRequest.class))).thenReturn(mockQuery); PowerMockito.mockStatic(BigqueryClient.class); when(BigqueryClient.getNewBigquerryClient(anyString())).thenReturn(mockBigqueryClient); bqClient = spy(new BigqueryClient("test-app")); }
@Test public void testQueryWithRetriesWhenServiceFails() throws Exception { when(mockQuery.execute()).thenThrow(new IOException()); thrown.expect(RuntimeException.class); thrown.expectMessage("Unable to get BigQuery response after retrying"); try { bqClient.queryWithRetries(query, projectId); } finally { verify(mockJobs, atLeast(BigqueryClient.MAX_QUERY_RETRIES)) .query(eq(projectId), any(QueryRequest.class)); } }
@Nonnull public QueryResponse queryWithRetries(String query, String projectId, boolean typed) throws IOException, InterruptedException { Sleeper sleeper = Sleeper.DEFAULT; BackOff backoff = BackOffAdapter.toGcpBackOff(BACKOFF_FACTORY.backoff()); IOException lastException = null; QueryRequest bqQueryRequest = new QueryRequest().setQuery(query); do { if (lastException != null) { LOG.warn("Retrying query ({}) after exception", bqQueryRequest.getQuery(), lastException); } try { QueryResponse response = bqClient.jobs().query(projectId, bqQueryRequest).execute(); if (response != null) { return typed ? getTypedTableRows(response) : response; } else { lastException = new IOException("Expected valid response from query job, but received null."); } } catch (IOException e) { // ignore and retry lastException = e; } } while (BackOffUtils.next(sleeper, backoff)); throw new RuntimeException( String.format( "Unable to get BigQuery response after retrying %d times using query (%s)", MAX_QUERY_RETRIES, bqQueryRequest.getQuery()), lastException); }
@Override public QueryResponse query(QueryRequest request) { try { return bigquery.jobs().query(this.options.projectId(), request).execute(); } catch (IOException ex) { throw translate(ex); } }
@Test public void testQueryWithRetriesWhenQueryResponseNull() throws Exception { when(mockQuery.execute()).thenReturn(null); thrown.expect(RuntimeException.class); thrown.expectMessage("Unable to get BigQuery response after retrying"); try { bqClient.queryWithRetries(query, projectId); } finally { verify(mockJobs, atLeast(BigqueryClient.MAX_QUERY_RETRIES)) .query(eq(projectId), any(QueryRequest.class)); } } }
@Override public QueryResponse query(QueryRequest request) { try { return bigquery.jobs().query(this.options.projectId(), request).execute(); } catch (IOException ex) { throw translate(ex); } }