From 46d1dbc7e0d8f81b1975047cd61429b9374e819e Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk Date: Tue, 20 Oct 2026 23:01:12 +0200 Subject: [PATCH 1/3] Refactor dumpWithoutCrash test for improved readability and reliability --- .../backtraceio/ExampleInstrumentedTest.java | 67 ++++++++++++------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java b/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java index 7875627a..506b7747 100644 --- a/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java +++ b/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java @@ -80,19 +80,19 @@ public void handledException() throws TimeoutException, CoronerResponseProcessin // upload on the single sender thread, routinely exceeds a few seconds. waiter.await(SUBMISSION_TIMEOUT_MS, TimeUnit.MILLISECONDS, 1); Assert.assertNotNull("No submission result", result[0]); - Assert.assertEquals("Submission failed: " + result[0].message, BacktraceResultStatus.Ok, result[0].status); + assertEquals("Submission failed: " + result[0].message, BacktraceResultStatus.Ok, result[0].status); String rxId = result[0].rxId; // THEN CoronerResponse response = awaitIngestedReport(rxId); - Assert.assertEquals(1, response.getResultsNumber()); + assertEquals(1, response.getResultsNumber()); String resultErrorMsg = response.getAttribute(0, "error.message", String.class); - Assert.assertEquals("Invalid index of selected element!", resultErrorMsg); + assertEquals("Invalid index of selected element!", resultErrorMsg); String resultClassifier = response.getAttribute(0, "classifiers", String.class); - Assert.assertEquals("java.lang.IndexOutOfBoundsException", resultClassifier); + assertEquals("java.lang.IndexOutOfBoundsException", resultClassifier); } /** @@ -119,32 +119,51 @@ private CoronerResponse awaitIngestedReport(String rxId) throws InterruptedExcep return null; } + /** + * Polls Coroner until a native dump report with error.message "DumpWithoutCrash" within + * [timestampStart, now] is ingested. + */ + private CoronerResponse awaitIngestedDumpWithoutCrash(long timestampStart) throws InterruptedException { + long deadline = SystemClock.elapsedRealtime() + INGESTION_TIMEOUT_MS; + Exception lastPollFailure = null; + while (SystemClock.elapsedRealtime() < deadline) { + try { + CoronerResponse response = this.getCoronerClient() + .errorTypeTimestampFilter( + "Crash", + Long.toString(timestampStart), + Long.toString(this.getSecondsTimestampNowGMT()), + Arrays.asList("error.message")); + lastPollFailure = null; + if (response != null && response.getResultsNumber() > 0) { + for (int i = 0; i < response.getResultsNumber(); i++) { + String message = response.getAttribute(i, "error.message", String.class); + if ("DumpWithoutCrash".equals(message)) { + return response; + } + } + } + } catch (Exception ex) { + lastPollFailure = ex; + } + Thread.sleep(THREAD_SLEEP_TIME_MS); + } + Assert.fail("DumpWithoutCrash report not ingested within " + INGESTION_TIMEOUT_MS + " ms" + + (lastPollFailure == null ? "" : "; last poll failure: " + lastPollFailure.getMessage())); + return null; + } + @Test - public void dumpWithoutCrash() throws CoronerResponseProcessingException, InterruptedException { + public void dumpWithoutCrash() throws InterruptedException { // GIVEN - CoronerResponse response = null; long timestampStart = this.getSecondsTimestampNowGMT(); // WHEN onView(withId(R.id.dumpWithoutCrash)).perform(click()); // UI action - Thread.sleep(THREAD_SLEEP_TIME_MS * 10); // THEN - try { - response = this.getCoronerClient() - .errorTypeTimestampFilter( - "Crash", - Long.toString(timestampStart), - Long.toString(this.getSecondsTimestampNowGMT()), - Arrays.asList("error.message")); - } catch (Exception ex) { - Assert.fail(ex.getMessage()); - } - - Assert.assertNotNull(response); - Assert.assertEquals(1, response.getResultsNumber()); - String val = response.getAttribute(0, "error.message", String.class); - Assert.assertEquals("DumpWithoutCrash", val); + CoronerResponse response = awaitIngestedDumpWithoutCrash(timestampStart); + Assert.assertNotNull("No Coroner response for DumpWithoutCrash", response); } // @Test @@ -176,9 +195,9 @@ public void unhandledException() throws CoronerResponseProcessingException, Inte } Assert.assertNotNull(response); - Assert.assertEquals(1, response.getResultsNumber()); + assertEquals(1, response.getResultsNumber()); String val = response.getAttribute(0, "error.message", String.class); - Assert.assertEquals("Dump without crash", val); + assertEquals("Dump without crash", val); } @Test From c156096a624d4116af10414e3f8e8b81c782c534 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk Date: Wed, 18 Nov 2026 22:05:03 +0100 Subject: [PATCH 2/3] Add report GUID handling to instrumentation tests and MainActivity --- .../backtraceio/ExampleInstrumentedTest.java | 49 +++++-------------- .../backtraceio/backtraceio/MainActivity.java | 17 ++++++- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java b/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java index 506b7747..fcad9431 100644 --- a/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java +++ b/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java @@ -20,6 +20,7 @@ import backtraceio.library.models.BacktraceResult; import backtraceio.library.models.types.BacktraceResultStatus; import java.util.Arrays; +import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import net.jodah.concurrentunit.Waiter; @@ -40,12 +41,16 @@ public class ExampleInstrumentedTest extends InstrumentedTest { private static final long SUBMISSION_TIMEOUT_MS = 60_000; private static final long INGESTION_TIMEOUT_MS = 60_000; + /** Per-test report GUID; declared before the rule so it is initialized when the rule is built. */ + private final String reportGuid = UUID.randomUUID().toString(); + @Rule public ActivityScenarioRule mActivityRule = new ActivityScenarioRule<>(new Intent(ApplicationProvider.getApplicationContext(), MainActivity.class) // Metrics share the sender thread with reports, so an unreachable events endpoint // would delay the uploads these tests wait for. None of these tests assert on metrics. - .putExtra(MainActivity.EXTRA_DISABLE_METRICS, true)); + .putExtra(MainActivity.EXTRA_DISABLE_METRICS, true) + .putExtra(MainActivity.EXTRA_REPORT_GUID, reportGuid)); @Before public void enableBreadcrumbs() { @@ -119,42 +124,8 @@ private CoronerResponse awaitIngestedReport(String rxId) throws InterruptedExcep return null; } - /** - * Polls Coroner until a native dump report with error.message "DumpWithoutCrash" within - * [timestampStart, now] is ingested. - */ - private CoronerResponse awaitIngestedDumpWithoutCrash(long timestampStart) throws InterruptedException { - long deadline = SystemClock.elapsedRealtime() + INGESTION_TIMEOUT_MS; - Exception lastPollFailure = null; - while (SystemClock.elapsedRealtime() < deadline) { - try { - CoronerResponse response = this.getCoronerClient() - .errorTypeTimestampFilter( - "Crash", - Long.toString(timestampStart), - Long.toString(this.getSecondsTimestampNowGMT()), - Arrays.asList("error.message")); - lastPollFailure = null; - if (response != null && response.getResultsNumber() > 0) { - for (int i = 0; i < response.getResultsNumber(); i++) { - String message = response.getAttribute(i, "error.message", String.class); - if ("DumpWithoutCrash".equals(message)) { - return response; - } - } - } - } catch (Exception ex) { - lastPollFailure = ex; - } - Thread.sleep(THREAD_SLEEP_TIME_MS); - } - Assert.fail("DumpWithoutCrash report not ingested within " + INGESTION_TIMEOUT_MS + " ms" - + (lastPollFailure == null ? "" : "; last poll failure: " + lastPollFailure.getMessage())); - return null; - } - @Test - public void dumpWithoutCrash() throws InterruptedException { + public void dumpWithoutCrash() { // GIVEN long timestampStart = this.getSecondsTimestampNowGMT(); @@ -162,8 +133,10 @@ public void dumpWithoutCrash() throws InterruptedException { onView(withId(R.id.dumpWithoutCrash)).perform(click()); // UI action // THEN - CoronerResponse response = awaitIngestedDumpWithoutCrash(timestampStart); - Assert.assertNotNull("No Coroner response for DumpWithoutCrash", response); + // Correlated by this test's GUID and filtered on the message, so other Crash reports in the + // window, dumps from other devices, and duplicate dumps are all rejected. + CoronerNativeReportAssertions.awaitExactlyOneWithMessage( + this.getCoronerClient(), reportGuid, "DumpWithoutCrash", timestampStart); } // @Test diff --git a/example-app/src/main/java/backtraceio/backtraceio/MainActivity.java b/example-app/src/main/java/backtraceio/backtraceio/MainActivity.java index 5f13de3b..9c768a19 100644 --- a/example-app/src/main/java/backtraceio/backtraceio/MainActivity.java +++ b/example-app/src/main/java/backtraceio/backtraceio/MainActivity.java @@ -38,6 +38,13 @@ public class MainActivity extends AppCompatActivity { */ public static final String EXTRA_DISABLE_METRICS = "backtraceio.backtraceio.DISABLE_METRICS"; + /** + * Intent extra overriding the report {@code guid} attribute. Instrumentation tests set a + * per-test value so Coroner queries correlate only the reports this test produced, not + * reports from other tests or devices in the same run. + */ + public static final String EXTRA_REPORT_GUID = "backtraceio.backtraceio.REPORT_GUID"; + private BacktraceClient backtraceClient; private OnServerResponseEventListener listener; private final int anrTimeout = 3000; @@ -57,7 +64,8 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); boolean enableMetrics = !getIntent().getBooleanExtra(EXTRA_DISABLE_METRICS, false); - backtraceClient = initializeBacktrace(BuildConfig.BACKTRACE_SUBMISSION_URL, enableMetrics); + String reportGuid = getIntent().getStringExtra(EXTRA_REPORT_GUID); + backtraceClient = initializeBacktrace(BuildConfig.BACKTRACE_SUBMISSION_URL, enableMetrics, reportGuid); symlinkAndWriteFile(); } @@ -78,7 +86,8 @@ private void symlinkAndWriteFile() { writeMyCustomFile(fileNameDateString); } - private BacktraceClient initializeBacktrace(final String submissionUrl, final boolean enableMetrics) { + private BacktraceClient initializeBacktrace( + final String submissionUrl, final boolean enableMetrics, final String reportGuid) { BacktraceCredentials credentials = new BacktraceCredentials(submissionUrl); Context context = getApplicationContext(); @@ -96,6 +105,10 @@ private BacktraceClient initializeBacktrace(final String submissionUrl, final bo put("custom.attribute", "My Custom Attribute"); } }; + if (reportGuid != null) { + // Must be set before native integration starts: the native backend reads `guid` once. + attributes.put("guid", reportGuid); + } List attachments = new ArrayList() { { From bbc807079f123e11ab2d814e45727042faaacb1b Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk Date: Thu, 19 Nov 2026 21:01:05 +0100 Subject: [PATCH 3/3] Update report GUID handling in instrumentation tests for consistency across tests --- .../backtraceio/ExampleInstrumentedTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java b/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java index fcad9431..fd58df4e 100644 --- a/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java +++ b/example-app/src/androidTest/java/backtraceio/backtraceio/ExampleInstrumentedTest.java @@ -41,8 +41,14 @@ public class ExampleInstrumentedTest extends InstrumentedTest { private static final long SUBMISSION_TIMEOUT_MS = 60_000; private static final long INGESTION_TIMEOUT_MS = 60_000; - /** Per-test report GUID; declared before the rule so it is initialized when the rule is built. */ - private final String reportGuid = UUID.randomUUID().toString(); + /** + * Report GUID shared by every test in this instrumentation process. Native integration + * (Crashpad) initializes once per process and ignores later re-initialization, so the GUID + * applied to native reports is the one from the first MainActivity launch. A per-test GUID + * would therefore never match native reports from any test but the first; a process-wide + * GUID matches regardless of test order. Message and timestamp filters keep tests apart. + */ + private static final String reportGuid = UUID.randomUUID().toString(); @Rule public ActivityScenarioRule mActivityRule =