From 96d301750afde77ee8ee37e35257d330681ae599 Mon Sep 17 00:00:00 2001 From: Guillaume Perrot Date: Fri, 2 Nov 2018 18:15:26 -0700 Subject: [PATCH] Fix test reliability of truncating causes --- .../appcenter/crashes/CrashesAndroidTest.java | 58 ------------------- .../crashes/utils/ErrorLogHelperTest.java | 14 +++++ 2 files changed, 14 insertions(+), 58 deletions(-) diff --git a/sdk/appcenter-crashes/src/androidTest/java/com/microsoft/appcenter/crashes/CrashesAndroidTest.java b/sdk/appcenter-crashes/src/androidTest/java/com/microsoft/appcenter/crashes/CrashesAndroidTest.java index a73e55aedf..6de4959431 100644 --- a/sdk/appcenter-crashes/src/androidTest/java/com/microsoft/appcenter/crashes/CrashesAndroidTest.java +++ b/sdk/appcenter-crashes/src/androidTest/java/com/microsoft/appcenter/crashes/CrashesAndroidTest.java @@ -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. */ @@ -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 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 { diff --git a/sdk/appcenter-crashes/src/test/java/com/microsoft/appcenter/crashes/utils/ErrorLogHelperTest.java b/sdk/appcenter-crashes/src/test/java/com/microsoft/appcenter/crashes/utils/ErrorLogHelperTest.java index 03424a28d4..4eb38e2a3b 100644 --- a/sdk/appcenter-crashes/src/test/java/com/microsoft/appcenter/crashes/utils/ErrorLogHelperTest.java +++ b/sdk/appcenter-crashes/src/test/java/com/microsoft/appcenter/crashes/utils/ErrorLogHelperTest.java @@ -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); + } }