Pollquery shouldn't wait > remaining timeout for response

Change-Id: I8cf8e6d84ff826d77449c941a1f53b0720fbe683
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1839
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestExecutor.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestExecutor.java
index edbcc7e..35ab1df 100644
--- a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestExecutor.java
+++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestExecutor.java
@@ -45,9 +45,11 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import java.util.function.Predicate;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -1136,12 +1138,26 @@
         boolean expectedException = false;
         Exception finalException;
         LOGGER.fine("polling for up to " + timeoutSecs + " seconds w/ " + retryDelaySecs + " second(s) delay");
+        int responsesReceived = 0;
+        final ExecutorService executorService = Executors.newSingleThreadExecutor();
         while (true) {
             try {
-                executeTestFile(testCaseCtx, ctx, variableCtx, statement, isDmlRecoveryTest, pb, cUnit, queryCount,
-                        expectedResultFileCtxs, testFile, actualPath);
+                Future<Void> execution = executorService.submit(() -> {
+                    executeTestFile(testCaseCtx, ctx, variableCtx, statement, isDmlRecoveryTest, pb, cUnit, queryCount,
+                            expectedResultFileCtxs, testFile, actualPath);
+                    return null;
+                });
+                execution.get(limitTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
+                responsesReceived++;
                 finalException = null;
                 break;
+            } catch (TimeoutException e) {
+                if (responsesReceived == 0) {
+                    throw new Exception("Poll limit (" + timeoutSecs + "s) exceeded without obtaining *any* result from server");
+                } else {
+                    throw new Exception("Poll limit (" + timeoutSecs + "s) exceeded without obtaining expected result");
+
+                }
             } catch (Exception e) {
                 if (isExpected(e, cUnit)) {
                     expectedException = true;
@@ -1153,7 +1169,7 @@
                     break;
                 }
                 LOGGER.fine("sleeping " + retryDelaySecs + " second(s) before polling again");
-                Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelaySecs));
+                TimeUnit.SECONDS.sleep(retryDelaySecs);
             }
         }
         if (expectedException) {
@@ -1411,7 +1427,7 @@
                 try {
                     final HttpClient client = HttpClients.createDefault();
 
-                    final HttpGet get = new HttpGet(createEndpointURI("/admin/cluster", null));
+                    final HttpGet get = new HttpGet(getEndpoint(Servlets.CLUSTER_STATE));
                     final HttpResponse httpResponse = client.execute(get);
                     final int statusCode = httpResponse.getStatusLine().getStatusCode();
                     final String response = EntityUtils.toString(httpResponse.getEntity());