diff --git a/src/main/java/hudson/plugins/android_emulator/monkey/MonkeyRecorder.java b/src/main/java/hudson/plugins/android_emulator/monkey/MonkeyRecorder.java index 79be934a..6f4a83c3 100644 --- a/src/main/java/hudson/plugins/android_emulator/monkey/MonkeyRecorder.java +++ b/src/main/java/hudson/plugins/android_emulator/monkey/MonkeyRecorder.java @@ -85,34 +85,52 @@ static MonkeyAction parseMonkeyOutput(AbstractBuild build, PrintStream log return new MonkeyAction(MonkeyResult.NothingToParse); } + Matcher matcher = Pattern.compile(":AllowPackage: (.*?)").matcher(monkeyOutput); + + String packageId = "_package_"; + if (matcher.find()) { + packageId = matcher.group(1); + } + // If we don't recognise any outcomes, then say so MonkeyResult result = MonkeyResult.UnrecognisedFormat; // Extract common data int totalEventCount = 0; - Matcher matcher = Pattern.compile(":Monkey: seed=-?\\d+ count=(\\d+)").matcher(monkeyOutput); - if (matcher.find()) { - totalEventCount = Integer.parseInt(matcher.group(1)); + int outputCount = 0; + matcher = Pattern.compile(":Monkey: seed=-?\\d+ count=(\\d+)").matcher(monkeyOutput); + while (matcher.find()) { + totalEventCount += Integer.parseInt(matcher.group(1)); + outputCount++; } // Determine outcome + int finishCount = 0; + matcher = Pattern.compile("// Monkey finished").matcher(monkeyOutput); + while (matcher.find()) { + finishCount++; + } int eventsCompleted = 0; - if (monkeyOutput.contains("// Monkey finished")) { + if (finishCount > 0 && finishCount >= outputCount) { result = MonkeyResult.Success; eventsCompleted = totalEventCount; } else { // If it didn't finish, assume failure matcher = Pattern.compile("Events injected: (\\d+)").matcher(monkeyOutput); - if (matcher.find()) { - eventsCompleted = Integer.parseInt(matcher.group(1)); + while (matcher.find()) { + eventsCompleted += Integer.parseInt(matcher.group(1)); } // Determine failure type - matcher = Pattern.compile("// (CRASH|NOT RESPONDING)").matcher(monkeyOutput); + matcher = Pattern.compile("// (CRASH|NOT RESPONDING): (.*?) \\(pid (\\d+)\\)").matcher(monkeyOutput); if (matcher.find()) { String reason = matcher.group(1); if ("CRASH".equals(reason)) { - result = MonkeyResult.Crash; + if (!matcher.group(2).equals(packageId)) { + result = MonkeyResult.Success; + } else { + result = MonkeyResult.Crash; + } } else if ("NOT RESPONDING".equals(reason)) { result = MonkeyResult.AppNotResponding; } diff --git a/src/test/java/hudson/plugins/android_emulator/monkey/MonkeyRecorderTest.java b/src/test/java/hudson/plugins/android_emulator/monkey/MonkeyRecorderTest.java index 3f2a8bba..8c0f6f39 100644 --- a/src/test/java/hudson/plugins/android_emulator/monkey/MonkeyRecorderTest.java +++ b/src/test/java/hudson/plugins/android_emulator/monkey/MonkeyRecorderTest.java @@ -17,9 +17,11 @@ public class MonkeyRecorderTest extends TestCase { - private static final String MONKEY_CRASH = "// CRASH"; - private static final String MONKEY_ANR = "// NOT RESPONDING"; + private static final String MONKEY_CRASH = "// CRASH: _package_ (pid 0)"; + private static final String MONKEY_ANR = "// NOT RESPONDING: _package_ (pid 0)"; + private static final String MONKEY_OUTSIDE_PACKAGE_CRASH = "// CRASH: _outside_package_ (pid 0)"; private static final String MONKEY_SUCCESS = "// Monkey finished"; + private static final String MONKEY_START_HEADER = ":Monkey: seed=0 count=1234\n"; public void testNotRunForBuild_Aborted() throws InterruptedException, IOException { assertNoParsingForBadBuild(Result.ABORTED); @@ -63,6 +65,10 @@ public void testCrash() { parseOutputAndAssert(MONKEY_CRASH, MonkeyResult.Crash); } + public void testOutsidePackageCrash() { + parseOutputAndAssert(MONKEY_OUTSIDE_PACKAGE_CRASH, MonkeyResult.Success); + } + public void testCrash_FailureBegetsFailure() { parseOutputAndAssert(MONKEY_CRASH, BuildOutcome.FAILURE, Result.FAILURE, MonkeyResult.Crash); } @@ -104,6 +110,26 @@ public void testSuccessWithTotalCount() { parseOutputAndAssert(output, MonkeyResult.Success, 1234, 1234); } + public void testPartialSuccess() { + String output = MONKEY_START_HEADER; + output += "Events injected: 1234"; + output += MONKEY_SUCCESS; + output += MONKEY_START_HEADER; + output += "Events injected: 12"; + output += MONKEY_CRASH; + parseOutputAndAssert(output, MonkeyResult.Crash, 1246, 2468); + } + + public void testMultipleSuccess() { + String output = MONKEY_START_HEADER; + output += "Events injected: 1234"; + output += MONKEY_SUCCESS; + output += MONKEY_START_HEADER; + output += "Events injected: 1234"; + output += MONKEY_SUCCESS; + parseOutputAndAssert(output, MonkeyResult.Success, 2468, 2468); + } + private void parseOutputAndAssert(String monkeyOutput, MonkeyResult expectedResult) { parseOutputAndAssert(monkeyOutput, BuildOutcome.IGNORE, null, expectedResult, 0, 0); }