summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/test/unit/test_crash_AsyncShutdown.js
blob: 3f1fa8af8bdaeadefad7a8ce0b367fd0eb45bdb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that AsyncShutdown report errors correctly

// Note: these functions are evaluated in their own process, hence the need
// to import modules into each function.

function setup_crash() {
  /* global AsyncShutdown */
  ChromeUtils.import("resource://gre/modules/AsyncShutdown.jsm", this);
  ChromeUtils.import("resource://gre/modules/Services.jsm", this);
  ChromeUtils.import("resource://gre/modules/Promise.jsm", this);

  Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
  Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 10);

  let TOPIC = "testing-async-shutdown-crash";
  let phase = AsyncShutdown._getPhase(TOPIC);
  phase.addBlocker("A blocker that is never satisfied", function() {
    dump("Installing blocker\n");
    let deferred = Promise.defer();
    return deferred.promise;
  });

  Services.obs.notifyObservers(null, TOPIC);
  dump(new Error().stack + "\n");
  dump("Waiting for crash\n");
}

function after_crash(mdump, extra) {
  info("after crash: " + extra.AsyncShutdownTimeout);
  let data = JSON.parse(extra.AsyncShutdownTimeout);
  Assert.equal(data.phase, "testing-async-shutdown-crash");
  Assert.equal(data.conditions[0].name, "A blocker that is never satisfied");
  // This test spawns subprocesses by using argument "-e" of xpcshell, so
  // this is the filename known to xpcshell.
  Assert.equal(data.conditions[0].filename, "-e");
}

// Test that AsyncShutdown + OS.File reports errors correctly, in a case in which
// the latest operation succeeded

function setup_osfile_crash_noerror() {
  ChromeUtils.import("resource://gre/modules/Services.jsm", this);
  ChromeUtils.import("resource://gre/modules/osfile.jsm", this);
  ChromeUtils.import("resource://gre/modules/Promise.jsm", this);

  Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 1);
  Services.prefs.setBoolPref("toolkit.osfile.native", false);

  OS.File.profileBeforeChange.addBlocker(
    "Adding a blocker that will never be resolved",
    () => Promise.defer().promise
  );
  OS.File.getCurrentDirectory();

  Services.obs.notifyObservers(null, "profile-before-change");
  dump("Waiting for crash\n");
}

function after_osfile_crash_noerror(mdump, extra) {
  info("after OS.File crash: " + extra.AsyncShutdownTimeout);
  let data = JSON.parse(extra.AsyncShutdownTimeout);
  let state = data.conditions[0].state;
  info("Keys: " + Object.keys(state).join(", "));
  Assert.equal(data.phase, "profile-before-change");
  Assert.ok(state.launched);
  Assert.ok(!state.shutdown);
  Assert.ok(state.worker);
  Assert.ok(!!state.latestSent);
  Assert.equal(state.latestSent[1], "getCurrentDirectory");
}

// Test that AsyncShutdown + OS.File reports errors correctly, in a case in which
// the latest operation failed

function setup_osfile_crash_exn() {
  ChromeUtils.import("resource://gre/modules/Services.jsm", this);
  ChromeUtils.import("resource://gre/modules/osfile.jsm", this);
  ChromeUtils.import("resource://gre/modules/Promise.jsm", this);

  Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 1);
  Services.prefs.setBoolPref("toolkit.osfile.native", false);

  OS.File.profileBeforeChange.addBlocker(
    "Adding a blocker that will never be resolved",
    () => Promise.defer().promise
  );
  OS.File.read("I do not exist");

  Services.obs.notifyObservers(null, "profile-before-change");
  dump("Waiting for crash\n");
}

function after_osfile_crash_exn(mdump, extra) {
  info("after OS.File crash: " + extra.AsyncShutdownTimeout);
  let data = JSON.parse(extra.AsyncShutdownTimeout);
  let state = data.conditions[0].state;
  info("Keys: " + Object.keys(state).join(", "));
  Assert.equal(data.phase, "profile-before-change");
  Assert.ok(!state.shutdown);
  Assert.ok(state.worker);
  Assert.ok(!!state.latestSent);
  Assert.equal(state.latestSent[1], "read");
}

add_task(async function run_test() {
  await do_crash(setup_crash, after_crash);
  await do_crash(setup_osfile_crash_noerror, after_osfile_crash_noerror);
  await do_crash(setup_osfile_crash_exn, after_osfile_crash_exn);
});