Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,12 +41,22 @@ public class ExampleInstrumentedTest extends InstrumentedTest {
private static final long SUBMISSION_TIMEOUT_MS = 60_000;
private static final long INGESTION_TIMEOUT_MS = 60_000;

/**
* 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<MainActivity> 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() {
Expand Down Expand Up @@ -80,19 +91,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);
}

/**
Expand Down Expand Up @@ -120,31 +131,18 @@ private CoronerResponse awaitIngestedReport(String rxId) throws InterruptedExcep
}

@Test
public void dumpWithoutCrash() throws CoronerResponseProcessingException, InterruptedException {
public void dumpWithoutCrash() {
// 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);
// 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
Expand Down Expand Up @@ -176,9 +174,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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();
Expand All @@ -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<String> attachments = new ArrayList<String>() {
{
Expand Down