abdullah alamoudi has uploaded a new change for review.
https://asterix-gerrit.ics.uci.edu/1705
Change subject: Refactor TestExecutor
......................................................................
Refactor TestExecutor
Change-Id: I0c586ad12f059eaf29c4031ccdcff10d0fb46aa0
---
M
asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestExecutor.java
1 file changed, 149 insertions(+), 130 deletions(-)
git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb
refs/changes/05/1705/1
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 8457681..0fcb785 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
@@ -100,8 +100,7 @@
private static final Pattern REGEX_LINES_PATTERN =
Pattern.compile("^(-)?/(.*)/([im]*)$");
private static final Pattern POLL_TIMEOUT_PATTERN =
Pattern.compile("polltimeoutsecs=(\\d+)(\\D|$)",
Pattern.MULTILINE);
- private static final Pattern POLL_DELAY_PATTERN =
- Pattern.compile("polldelaysecs=(\\d+)(\\D|$)", Pattern.MULTILINE);
+ private static final Pattern POLL_DELAY_PATTERN =
Pattern.compile("polldelaysecs=(\\d+)(\\D|$)", Pattern.MULTILINE);
private static final Pattern HANDLE_VARIABLE_PATTERN =
Pattern.compile("handlevariable=(\\w+)");
private static final Pattern VARIABLE_REF_PATTERN =
Pattern.compile("\\$(\\w+)");
@@ -755,9 +754,7 @@
if (ctx.getFile().getName().endsWith("aql")) {
executeDDL(statement, getEndpoint(Servlets.AQL_DDL));
} else {
- InputStream resultStream = executeQueryService(statement,
getEndpoint(Servlets.QUERY_SERVICE),
- OutputFormat.CLEAN_JSON);
- ResultExtractor.extract(resultStream);
+ executeSqlppDdl(statement);
}
break;
case "update":
@@ -768,48 +765,13 @@
if (ctx.getFile().getName().endsWith("aql")) {
executeUpdate(statement, getEndpoint(Servlets.AQL_UPDATE));
} else {
- InputStream resultStream = executeQueryService(statement,
getEndpoint(Servlets.QUERY_SERVICE),
- OutputFormat.forCompilationUnit(cUnit));
- ResultExtractor.extract(resultStream);
+ executeSqlppUpdate(statement,
OutputFormat.forCompilationUnit(cUnit));
}
break;
case "pollget":
case "pollquery":
- // polltimeoutsecs=nnn, polldelaysecs=nnn
- int timeoutSecs = getTimeoutSecs(statement);
- int retryDelaySecs = getRetryDelaySecs(statement);
- long startTime = System.currentTimeMillis();
- long limitTime = startTime +
TimeUnit.SECONDS.toMillis(timeoutSecs);
- ctx.setType(ctx.getType().substring("poll".length()));
- boolean expectedException = false;
- Exception finalException;
- LOGGER.fine("polling for up to " + timeoutSecs + " seconds w/
" + retryDelaySecs + " second(s) delay");
- while (true) {
- try {
- executeTestFile(testCaseCtx, ctx, variableCtx,
statement, isDmlRecoveryTest, pb, cUnit,
- queryCount, expectedResultFileCtxs, testFile,
actualPath);
- finalException = null;
- break;
- } catch (Exception e) {
- if (isExpected(e, cUnit)) {
- expectedException = true;
- finalException = e;
- break;
- }
- if ((System.currentTimeMillis() > limitTime)) {
- finalException = e;
- break;
- }
- LOGGER.fine("sleeping " + retryDelaySecs + " second(s)
before polling again");
-
Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelaySecs));
- }
- }
- if (expectedException) {
- throw finalException;
- } else if (finalException != null){
- throw new Exception("Poll limit (" + timeoutSecs + "s)
exceeded without obtaining expected result",
- finalException);
- }
+ poll(testCaseCtx, ctx, variableCtx, statement,
isDmlRecoveryTest, pb, cUnit, queryCount,
+ expectedResultFileCtxs, testFile, actualPath);
break;
case "query":
case "async":
@@ -821,65 +783,18 @@
executeScript(pb, pb.environment().get("SCRIPT_HOME") +
File.separator + "dml_recovery"
+ File.separator + "stop_and_start.sh");
}
- InputStream resultStream = null;
- OutputFormat fmt = OutputFormat.forCompilationUnit(cUnit);
- final String reqType = ctx.getType();
- final List<CompilationUnit.Parameter> params =
cUnit.getParameter();
- if (ctx.getFile().getName().endsWith("aql")) {
- if (reqType.equalsIgnoreCase("query")) {
- resultStream = executeQuery(statement, fmt,
getEndpoint(Servlets.AQL_QUERY), params);
- } else {
- final URI endpoint = getEndpoint(Servlets.AQL);
- if (reqType.equalsIgnoreCase("async")) {
- resultStream = executeAnyAQLAsync(statement,
false, fmt, endpoint, variableCtx);
- } else if (reqType.equalsIgnoreCase("deferred")) {
- resultStream = executeAnyAQLAsync(statement, true,
fmt, endpoint, variableCtx);
- }
- Assert.assertNotNull("no handle for " + reqType + "
test " + testFile.toString(),
- resultStream);
- }
- } else {
- String delivery = DELIVERY_IMMEDIATE;
- if (reqType.equalsIgnoreCase("async")) {
- delivery = DELIVERY_ASYNC;
- } else if (reqType.equalsIgnoreCase("deferred")) {
- delivery = DELIVERY_DEFERRED;
- }
- final URI uri = getEndpoint(Servlets.QUERY_SERVICE);
- if (DELIVERY_IMMEDIATE.equals(delivery)) {
- resultStream = executeQueryService(statement, fmt,
uri, params, true, true);
- resultStream = ResultExtractor.extract(resultStream);
- } else {
- String handleVar = getHandleVariable(statement);
- resultStream =
- executeQueryService(statement, fmt, uri,
upsertParam(params, "mode", delivery), true);
- String handle =
ResultExtractor.extractHandle(resultStream);
- Assert.assertNotNull("no handle for " + reqType + "
test " + testFile.toString(), handleVar);
- variableCtx.put(handleVar, handle);
- }
- }
- if (queryCount.intValue() >= expectedResultFileCtxs.size()) {
- Assert.fail("no result file for " + testFile.toString() +
"; queryCount: " + queryCount
- + ", filectxs.size: " +
expectedResultFileCtxs.size());
- }
- expectedResultFile =
expectedResultFileCtxs.get(queryCount.intValue()).getFile();
-
- File actualResultFile =
- testCaseCtx.getActualResultFile(cUnit,
expectedResultFile, new File(actualPath));
- writeOutputToFile(actualResultFile, resultStream);
-
- runScriptAndCompareWithResult(testFile, new
PrintWriter(System.err), expectedResultFile,
- actualResultFile);
- queryCount.increment();
-
- // Deletes the matched result file.
- actualResultFile.getParentFile().delete();
+ expectedResultFile = (queryCount.intValue() >=
expectedResultFileCtxs.size()) ? null
+ :
expectedResultFileCtxs.get(queryCount.intValue()).getFile();
+ File actualResultFile = expectedResultFile == null ? null
+ : testCaseCtx.getActualResultFile(cUnit,
expectedResultFile, new File(actualPath));
+ query(OutputFormat.forCompilationUnit(cUnit), ctx.getType(),
cUnit.getParameter(), statement, testFile,
+ variableCtx, expectedResultFile, actualResultFile,
queryCount, expectedResultFileCtxs.size());
break;
case "mgx":
executeManagixCommand(stripLineComments(statement).trim());
break;
case "txnqbc": // qbc represents query before crash
- resultStream = executeQuery(statement,
OutputFormat.forCompilationUnit(cUnit),
+ InputStream resultStream = executeQuery(statement,
OutputFormat.forCompilationUnit(cUnit),
getEndpoint(Servlets.AQL_QUERY), cUnit.getParameter());
qbcFile = getTestCaseQueryBeforeCrashFile(actualPath,
testCaseCtx, cUnit);
writeOutputToFile(qbcFile, resultStream);
@@ -959,33 +874,10 @@
break;
case "get":
case "post":
- fmt = OutputFormat.forCompilationUnit(cUnit);
- String handleVar = getHandleVariable(statement);
- final String trimmedPathAndQuery =
stripLineComments(stripJavaComments(statement)).trim();
- final String variablesReplaced =
replaceVarRef(trimmedPathAndQuery, variableCtx);
- if ("http".equals(ctx.extension())) {
- resultStream = executeHttp(ctx.getType(),
variablesReplaced, fmt);
- } else if ("uri".equals(ctx.extension())) {
- resultStream = executeURI(ctx.getType(),
URI.create(variablesReplaced), fmt);
- } else {
- throw new IllegalArgumentException(
- "Unexpected format for method " + ctx.getType() +
": " + ctx.extension());
- }
- if (handleVar != null) {
- String handle =
ResultExtractor.extractHandle(resultStream);
- if (handle != null) {
- variableCtx.put(handleVar, handle);
- } else {
- throw new Exception("no handle for test " +
testFile.toString());
- }
- } else {
- expectedResultFile =
expectedResultFileCtxs.get(queryCount.intValue()).getFile();
- actualResultFile = testCaseCtx.getActualResultFile(cUnit,
expectedResultFile, new File(actualPath));
- writeOutputToFile(actualResultFile, resultStream);
- runScriptAndCompareWithResult(testFile, new
PrintWriter(System.err), expectedResultFile,
- actualResultFile);
- }
- queryCount.increment();
+ expectedResultFile =
expectedResultFileCtxs.get(queryCount.intValue()).getFile();
+ actualResultFile = testCaseCtx.getActualResultFile(cUnit,
expectedResultFile, new File(actualPath));
+ getPost(OutputFormat.forCompilationUnit(cUnit), statement,
variableCtx, ctx.extension(), ctx.getType(),
+ testFile, expectedResultFile, actualResultFile,
queryCount);
break;
case "server": // (start <test server name> <port>
// [<arg1>][<arg2>][<arg3>]...|stop
(<port>|all))
@@ -1078,6 +970,134 @@
}
}
+ private void getPost(OutputFormat fmt, String statement, Map<String,
Object> variableCtx, String extension,
+ String type, File testFile, File expectedResultFile, File
actualResultFile, MutableInt queryCount)
+ throws Exception {
+ String handleVar = getHandleVariable(statement);
+ final String trimmedPathAndQuery =
stripLineComments(stripJavaComments(statement)).trim();
+ final String variablesReplaced = replaceVarRef(trimmedPathAndQuery,
variableCtx);
+ InputStream resultStream;
+ if ("http".equals(extension)) {
+ resultStream = executeHttp(type, variablesReplaced, fmt);
+ } else if ("uri".equals(extension)) {
+ resultStream = executeURI(type, URI.create(variablesReplaced),
fmt);
+ } else {
+ throw new IllegalArgumentException("Unexpected format for method "
+ type + ": " + extension);
+ }
+ if (handleVar != null) {
+ String handle = ResultExtractor.extractHandle(resultStream);
+ if (handle != null) {
+ variableCtx.put(handleVar, handle);
+ } else {
+ throw new Exception("no handle for test " +
testFile.toString());
+ }
+ } else {
+ writeOutputToFile(actualResultFile, resultStream);
+ runScriptAndCompareWithResult(testFile, new
PrintWriter(System.err), expectedResultFile, actualResultFile);
+ }
+ queryCount.increment();
+ }
+
+ public void query(OutputFormat fmt, String reqType,
List<CompilationUnit.Parameter> params, String statement,
+ File testFile, Map<String, Object> variableCtx, File
expectedResultFile, File actualResultFile,
+ MutableInt queryCount, int numResultFiles) throws Exception {
+ InputStream resultStream = null;
+ if (testFile.getName().endsWith("aql")) {
+ if (reqType.equalsIgnoreCase("query")) {
+ resultStream = executeQuery(statement, fmt,
getEndpoint(Servlets.AQL_QUERY), params);
+ } else {
+ final URI endpoint = getEndpoint(Servlets.AQL);
+ if (reqType.equalsIgnoreCase("async")) {
+ resultStream = executeAnyAQLAsync(statement, false, fmt,
endpoint, variableCtx);
+ } else if (reqType.equalsIgnoreCase("deferred")) {
+ resultStream = executeAnyAQLAsync(statement, true, fmt,
endpoint, variableCtx);
+ }
+ Assert.assertNotNull("no handle for " + reqType + " test " +
testFile.toString(), resultStream);
+ }
+ } else {
+ String delivery = DELIVERY_IMMEDIATE;
+ if (reqType.equalsIgnoreCase("async")) {
+ delivery = DELIVERY_ASYNC;
+ } else if (reqType.equalsIgnoreCase("deferred")) {
+ delivery = DELIVERY_DEFERRED;
+ }
+ final URI uri = getEndpoint(Servlets.QUERY_SERVICE);
+ if (DELIVERY_IMMEDIATE.equals(delivery)) {
+ resultStream = executeQueryService(statement, fmt, uri,
params, true, true);
+ resultStream = ResultExtractor.extract(resultStream);
+ } else {
+ String handleVar = getHandleVariable(statement);
+ resultStream = executeQueryService(statement, fmt, uri,
upsertParam(params, "mode", delivery), true);
+ String handle = ResultExtractor.extractHandle(resultStream);
+ Assert.assertNotNull("no handle for " + reqType + " test " +
testFile.toString(), handleVar);
+ variableCtx.put(handleVar, handle);
+ }
+ }
+ writeOutputToFile(actualResultFile, resultStream);
+ if (expectedResultFile == null) {
+ Assert.fail("no result file for " + testFile.toString() + ";
queryCount: " + queryCount
+ + ", filectxs.size: " + numResultFiles);
+ }
+
+ runScriptAndCompareWithResult(testFile, new PrintWriter(System.err),
expectedResultFile, actualResultFile);
+ queryCount.increment();
+
+ // Deletes the matched result file.
+ actualResultFile.getParentFile().delete();
+ }
+
+ private void poll(TestCaseContext testCaseCtx, TestFileContext ctx,
Map<String, Object> variableCtx,
+ String statement, boolean isDmlRecoveryTest, ProcessBuilder pb,
CompilationUnit cUnit,
+ MutableInt queryCount, List<TestFileContext>
expectedResultFileCtxs, File testFile, String actualPath)
+ throws Exception {
+ // polltimeoutsecs=nnn, polldelaysecs=nnn
+ int timeoutSecs = getTimeoutSecs(statement);
+ int retryDelaySecs = getRetryDelaySecs(statement);
+ long startTime = System.currentTimeMillis();
+ long limitTime = startTime + TimeUnit.SECONDS.toMillis(timeoutSecs);
+ ctx.setType(ctx.getType().substring("poll".length()));
+ boolean expectedException = false;
+ Exception finalException;
+ LOGGER.fine("polling for up to " + timeoutSecs + " seconds w/ " +
retryDelaySecs + " second(s) delay");
+ while (true) {
+ try {
+ executeTestFile(testCaseCtx, ctx, variableCtx, statement,
isDmlRecoveryTest, pb, cUnit, queryCount,
+ expectedResultFileCtxs, testFile, actualPath);
+ finalException = null;
+ break;
+ } catch (Exception e) {
+ if (isExpected(e, cUnit)) {
+ expectedException = true;
+ finalException = e;
+ break;
+ }
+ if ((System.currentTimeMillis() > limitTime)) {
+ finalException = e;
+ break;
+ }
+ LOGGER.fine("sleeping " + retryDelaySecs + " second(s) before
polling again");
+ Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelaySecs));
+ }
+ }
+ if (expectedException) {
+ throw finalException;
+ } else if (finalException != null) {
+ throw new Exception("Poll limit (" + timeoutSecs + "s) exceeded
without obtaining expected result",
+ finalException);
+ }
+ }
+
+ public void executeSqlppUpdate(String statement, OutputFormat
outputFormat) throws Exception {
+ InputStream resultStream = executeQueryService(statement,
getEndpoint(Servlets.QUERY_SERVICE), outputFormat);
+ ResultExtractor.extract(resultStream);
+ }
+
+ public void executeSqlppDdl(String statement) throws Exception {
+ InputStream resultStream =
+ executeQueryService(statement,
getEndpoint(Servlets.QUERY_SERVICE), OutputFormat.CLEAN_JSON);
+ ResultExtractor.extract(resultStream);
+ }
+
protected static boolean isExpected(Exception e, CompilationUnit cUnit) {
final List<String> expErrors = cUnit.getExpectedError();
for (String exp : expErrors) {
@@ -1088,7 +1108,7 @@
return false;
}
- protected int getTimeoutSecs(String statement) {
+ public static int getTimeoutSecs(String statement) {
final Matcher timeoutMatcher = POLL_TIMEOUT_PATTERN.matcher(statement);
if (timeoutMatcher.find()) {
return Integer.parseInt(timeoutMatcher.group(1));
@@ -1097,7 +1117,7 @@
}
}
- protected static int getRetryDelaySecs(String statement) {
+ public static int getRetryDelaySecs(String statement) {
final Matcher retryDelayMatcher =
POLL_DELAY_PATTERN.matcher(statement);
return retryDelayMatcher.find() ?
Integer.parseInt(retryDelayMatcher.group(1)) : 1;
}
@@ -1233,8 +1253,8 @@
private static File getTestCaseQueryBeforeCrashFile(String actualPath,
TestCaseContext testCaseCtx,
CompilationUnit cUnit) {
return new File(
- actualPath + File.separator +
testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_")
- + "_" + cUnit.getName() + "_qbc.adm");
+ actualPath + File.separator +
testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
+ + cUnit.getName() + "_qbc.adm");
}
protected URI getEndpoint(String servlet) throws URISyntaxException {
@@ -1257,8 +1277,7 @@
getEndpoint(Servlets.QUERY_SERVICE),
OutputFormat.CLEAN_JSON);
String out = IOUtils.toString(resultStream);
ObjectMapper om = new ObjectMapper();
- om.setConfig(
-
om.getDeserializationConfig().with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT));
+
om.setConfig(om.getDeserializationConfig().with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT));
JsonNode result;
try {
result = om.readValue(out, ObjectNode.class).get("results");
--
To view, visit https://asterix-gerrit.ics.uci.edu/1705
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0c586ad12f059eaf29c4031ccdcff10d0fb46aa0
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: abdullah alamoudi <[email protected]>