Skip to content

Commit

Permalink
Merge pull request #867 from Microsoft/fix/test-reliability
Browse files Browse the repository at this point in the history
Fix test reliability of truncating causes
  • Loading branch information
dhei committed Nov 5, 2018
2 parents e2bd17b + 96d3017 commit a152e73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ private static Error generateStackOverflowError() {
}
}

@SuppressWarnings("SameParameterValue")
private static RuntimeException generateExceptionWithManyCauses(int causes) {
Exception e = new Exception();
for (int i = 0; i < causes; i++) {
e = new Exception(Integer.valueOf(i).toString(), e);
}
return new RuntimeException(e);
}

private void startFresh(CrashesListener listener) {

/* Configure new instance. */
Expand Down Expand Up @@ -218,55 +209,6 @@ public void run() {
assertTrue(Crashes.hasCrashedInLastSession().get());
}

@Test
public void getLastSessionCrashReportExceptionWithALotOfCauses() throws Exception {

/* Null before start. */
Crashes.unsetInstance();
assertNull(Crashes.getLastSessionCrashReport().get());
assertFalse(Crashes.hasCrashedInLastSession().get());

/* Crash on 1st process. */
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
startFresh(null);
assertNull(Crashes.getLastSessionCrashReport().get());
assertFalse(Crashes.hasCrashedInLastSession().get());
final RuntimeException exception = generateExceptionWithManyCauses(17);
final Thread thread = new Thread() {

@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();

/* Get last session crash on 2nd process. */
startFresh(null);
assertTrue(Crashes.hasCrashedInLastSession().get());
ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
assertNotNull(errorReport);

/*
* We don't check throwable is set, it will randomly fail with StackOverflowError on a
* ARM emulator. In both case the JSON is saved and we check it.
* We also have a unit test that use mocks in CrashesTest to verify what happens on a
* simulated StackOverflowError, testing that on emulator is unreliable.
*/

/* Check managed error was sent as truncated. */
ArgumentCaptor<ManagedErrorLog> errorLog = ArgumentCaptor.forClass(ManagedErrorLog.class);
verify(mChannel).enqueue(errorLog.capture(), eq(Crashes.ERROR_GROUP));
int causesCount = 0;
com.microsoft.appcenter.crashes.ingestion.models.Exception e = errorLog.getValue().getException();
while (e.getInnerExceptions() != null && (e = e.getInnerExceptions().get(0)) != null) {
causesCount++;
}
assertEquals(ErrorLogHelper.CAUSE_LIMIT, causesCount + 1);
}

@Test
public void getLastSessionCrashReportNative() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,18 @@ public void validateProperties() {
assertEquals(1, actualProperties.size());
assertEquals(truncatedMapItem, actualProperties.get(truncatedMapItem));
}

@Test
public void truncateCauses() {
RuntimeException e = new RuntimeException();
for (int i = 0; i < 32; i++) {
e = new RuntimeException(Integer.valueOf(i).toString(), e);
}
int depth = 1;
Exception model = ErrorLogHelper.getModelExceptionFromThrowable(e);
while (model.getInnerExceptions() != null && (model = model.getInnerExceptions().get(0)) != null) {
depth++;
}
assertEquals(ErrorLogHelper.CAUSE_LIMIT, depth);
}
}

0 comments on commit a152e73

Please sign in to comment.