Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down