fune/toolkit/components/backgroundtasks/tests/BackgroundTask_crash.jsm
Nick Alexander ef585bc010 Bug 1679440 - Add test ensuring crash reporter works for background tasks. r=mossop,gsvelto
This adds a test to ensure that crash reporting works inside of
`application --backgroundtask ...` invocations.  A test-only `crash`
task is added that uses `CrashTestUtils.jsm` to trigger a crash.  The
`xpcshell` harness invokes that background task and processes any
minidump and extras just as it does for existing `xpcshell`
subprocesses.

The test is homed in `toolkit/crashreporter`, rather than in
`toolkit/components/backgroundtasks`, because there is special
handling for `CrashTestUtils.jsm` and the `testcrasher` library.  It's
probably possible to make that infrastructure usable from multiple
locations but it seems low value.

Differential Revision: https://phabricator.services.mozilla.com/D98096
2021-01-25 23:45:29 +00:00

56 lines
1.9 KiB
JavaScript

/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var EXPORTED_SYMBOLS = ["runBackgroundTask"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
async function runBackgroundTask(commandLine) {
// This task depends on `CrashTestUtils.jsm` and requires the
// sibling `testcrasher` library to be in the current working
// directory. Fail right away if we can't find the module or the
// native library.
let testPath = Services.dirsvc.get("CurWorkD", Ci.nsIFile).path;
const { CrashTestUtils } = ChromeUtils.import(
`file://${testPath}/CrashTestUtils.jsm`
);
// Get the temp dir.
var env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
var tmpd = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
tmpd.initWithPath(env.get("XPCSHELL_TEST_TEMP_DIR"));
var crashReporter = Cc["@mozilla.org/toolkit/crash-reporter;1"].getService(
Ci.nsICrashReporter
);
// We need to call this or crash events go in an undefined location.
crashReporter.UpdateCrashEventsDir();
// Setting the minidump path is not allowed in content processes,
// but background tasks run in the parent.
crashReporter.minidumpPath = tmpd;
// Arguments are [crashType, key1, value1, key2, value2, ...].
let i = 0;
let crashType = Number.parseInt(commandLine.getArgument(i));
i += 1;
while (i + 1 < commandLine.length) {
let key = commandLine.getArgument(i);
let value = commandLine.getArgument(i + 1);
i += 2;
crashReporter.annotateCrashReport(key, value);
}
console.log(`Crashing with crash type ${crashType}`);
// Now actually crash.
CrashTestUtils.crash(crashType);
// This is moot, since we crashed, but...
return 1;
}