// Poll on the operation status till the query is completed
boolean isQueryRunning = true;
long pollTimeout = System.currentTimeMillis() + 100000;
OperationStatus opStatus;
OperationState state = null;
RowSet rowSetAccumulated = null;
StringBuilder logs = new StringBuilder();
while (isQueryRunning) {
// Break if polling times out
if (System.currentTimeMillis() > pollTimeout) {
break;
}
opStatus = client.getOperationStatus(operationHandle);
Assert.assertNotNull(opStatus);
state = opStatus.getState();
rowSetAccumulated = client.fetchResults(operationHandle, FetchOrientation.FETCH_NEXT, 1000,
FetchType.LOG);
for (Object[] row : rowSetAccumulated) {
logs.append(row[0]);
}
if (state == OperationState.CANCELED ||
state == OperationState.CLOSED ||
state == OperationState.FINISHED ||
state == OperationState.ERROR) {
isQueryRunning = false;
}
Thread.sleep(10);
}
// The sql should be completed now.
Assert.assertEquals("Query should be finished", OperationState.FINISHED, state);
// Verify the accumulated logs
verifyFetchedLog(logs.toString());
// Verify the fetched logs from the beginning of the log file
RowSet rowSet = client.fetchResults(operationHandle, FetchOrientation.FETCH_FIRST, 1000,
FetchType.LOG);
verifyFetchedLog(rowSet);
}