toolkit/components/crashes/tests/xpcshell/test_crash_manager.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 "use strict";
     6 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
     8 let bsp = Cu.import("resource://gre/modules/CrashManager.jsm", this);
     9 Cu.import("resource://gre/modules/Promise.jsm", this);
    10 Cu.import("resource://gre/modules/Task.jsm", this);
    11 Cu.import("resource://gre/modules/osfile.jsm", this);
    13 Cu.import("resource://testing-common/CrashManagerTest.jsm", this);
    15 const DUMMY_DATE = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
    16 DUMMY_DATE.setMilliseconds(0);
    18 function run_test() {
    19   do_get_profile();
    20   configureLogging();
    21   run_next_test();
    22 }
    24 add_task(function* test_constructor_ok() {
    25   let m = new CrashManager({
    26     pendingDumpsDir: "/foo",
    27     submittedDumpsDir: "/bar",
    28     eventsDirs: [],
    29     storeDir: "/baz",
    30   });
    31   Assert.ok(m, "CrashManager can be created.");
    32 });
    34 add_task(function* test_constructor_invalid() {
    35   Assert.throws(() => {
    36     new CrashManager({foo: true});
    37   });
    38 });
    40 add_task(function* test_get_manager() {
    41   let m = yield getManager();
    42   Assert.ok(m, "CrashManager obtained.");
    44   yield m.createDummyDump(true);
    45   yield m.createDummyDump(false);
    46 });
    48 // Unsubmitted dump files on disk are detected properly.
    49 add_task(function* test_pending_dumps() {
    50   let m = yield getManager();
    51   let now = Date.now();
    52   let ids = [];
    53   const COUNT = 5;
    55   for (let i = 0; i < COUNT; i++) {
    56     ids.push(yield m.createDummyDump(false, new Date(now - i * 86400000)));
    57   }
    58   yield m.createIgnoredDumpFile("ignored", false);
    60   let entries = yield m.pendingDumps();
    61   Assert.equal(entries.length, COUNT, "proper number detected.");
    63   for (let entry of entries) {
    64     Assert.equal(typeof(entry), "object", "entry is an object");
    65     Assert.ok("id" in entry, "id in entry");
    66     Assert.ok("path" in entry, "path in entry");
    67     Assert.ok("date" in entry, "date in entry");
    68     Assert.notEqual(ids.indexOf(entry.id), -1, "ID is known");
    69   }
    71   for (let i = 0; i < COUNT; i++) {
    72     Assert.equal(entries[i].id, ids[COUNT-i-1], "Entries sorted by mtime");
    73   }
    74 });
    76 // Submitted dump files on disk are detected properly.
    77 add_task(function* test_submitted_dumps() {
    78   let m = yield getManager();
    79   let COUNT = 5;
    81   for (let i = 0; i < COUNT; i++) {
    82     yield m.createDummyDump(true);
    83   }
    84   yield m.createIgnoredDumpFile("ignored", true);
    86   let entries = yield m.submittedDumps();
    87   Assert.equal(entries.length, COUNT, "proper number detected.");
    89   let hrID = yield m.createDummyDump(true, new Date(), true);
    90   entries = yield m.submittedDumps();
    91   Assert.equal(entries.length, COUNT + 1, "hr- in filename detected.");
    93   let gotIDs = new Set([e.id for (e of entries)]);
    94   Assert.ok(gotIDs.has(hrID));
    95 });
    97 // The store should expire after inactivity.
    98 add_task(function* test_store_expires() {
    99   let m = yield getManager();
   101   Object.defineProperty(m, "STORE_EXPIRATION_MS", {
   102     value: 250,
   103   });
   105   let store = yield m._getStore();
   106   Assert.ok(store);
   107   Assert.equal(store, m._store);
   109   yield sleep(300);
   110   Assert.ok(!m._store, "Store has gone away.");
   111 });
   113 // Ensure discovery of unprocessed events files works.
   114 add_task(function* test_unprocessed_events_files() {
   115   let m = yield getManager();
   116   yield m.createEventsFile("1", "test.1", new Date(), "foo", 0);
   117   yield m.createEventsFile("2", "test.1", new Date(), "bar", 0);
   118   yield m.createEventsFile("1", "test.1", new Date(), "baz", 1);
   120   let paths = yield m._getUnprocessedEventsFiles();
   121   Assert.equal(paths.length, 3);
   122 });
   124 // Ensure only 1 aggregateEventsFiles() is allowed at a time.
   125 add_task(function* test_aggregate_events_locking() {
   126   let m = yield getManager();
   128   let p1 = m.aggregateEventsFiles();
   129   let p2 = m.aggregateEventsFiles();
   131   Assert.strictEqual(p1, p2, "Same promise should be returned.");
   132 });
   134 // Malformed events files should be deleted.
   135 add_task(function* test_malformed_files_deleted() {
   136   let m = yield getManager();
   138   yield m.createEventsFile("1", "crash.main.1", new Date(), "foo\nbar");
   140   let count = yield m.aggregateEventsFiles();
   141   Assert.equal(count, 1);
   142   let crashes = yield m.getCrashes();
   143   Assert.equal(crashes.length, 0);
   145   count = yield m.aggregateEventsFiles();
   146   Assert.equal(count, 0);
   147 });
   149 // Unknown event types should be ignored.
   150 add_task(function* test_aggregate_ignore_unknown_events() {
   151   let m = yield getManager();
   153   yield m.createEventsFile("1", "crash.main.1", DUMMY_DATE, "id1");
   154   yield m.createEventsFile("2", "foobar.1", new Date(), "dummy");
   156   let count = yield m.aggregateEventsFiles();
   157   Assert.equal(count, 2);
   159   count = yield m.aggregateEventsFiles();
   160   Assert.equal(count, 1);
   162   count = yield m.aggregateEventsFiles();
   163   Assert.equal(count, 1);
   164 });
   166 add_task(function* test_prune_old() {
   167   let m = yield getManager();
   168   let oldDate = new Date(Date.now() - 86400000);
   169   let newDate = new Date(Date.now() - 10000);
   170   yield m.createEventsFile("1", "crash.main.1", oldDate, "id1");
   171   yield m.createEventsFile("2", "crash.plugin.1", newDate, "id2");
   173   yield m.aggregateEventsFiles();
   175   let crashes = yield m.getCrashes();
   176   Assert.equal(crashes.length, 2);
   178   yield m.pruneOldCrashes(new Date(oldDate.getTime() + 10000));
   180   crashes = yield m.getCrashes();
   181   Assert.equal(crashes.length, 1, "Old crash has been pruned.");
   183   let c = crashes[0];
   184   Assert.equal(c.id, "id2", "Proper crash was pruned.");
   186   // We can't test exact boundary conditions because dates from filesystem
   187   // don't have same guarantees as JS dates.
   188   yield m.pruneOldCrashes(new Date(newDate.getTime() + 5000));
   189   crashes = yield m.getCrashes();
   190   Assert.equal(crashes.length, 0);
   191 });
   193 add_task(function* test_schedule_maintenance() {
   194   let m = yield getManager();
   195   yield m.createEventsFile("1", "crash.main.1", DUMMY_DATE, "id1");
   197   let oldDate = new Date(Date.now() - m.PURGE_OLDER_THAN_DAYS * 2 * 24 * 60 * 60 * 1000);
   198   yield m.createEventsFile("2", "crash.main.1", oldDate, "id2");
   200   yield m.scheduleMaintenance(25);
   201   let crashes = yield m.getCrashes();
   202   Assert.equal(crashes.length, 1);
   203   Assert.equal(crashes[0].id, "id1");
   204 });
   206 add_task(function* test_main_crash_event_file() {
   207   let m = yield getManager();
   208   yield m.createEventsFile("1", "crash.main.1", DUMMY_DATE, "id1");
   209   let count = yield m.aggregateEventsFiles();
   210   Assert.equal(count, 1);
   212   let crashes = yield m.getCrashes();
   213   Assert.equal(crashes.length, 1);
   214   Assert.equal(crashes[0].id, "id1");
   215   Assert.equal(crashes[0].type, "main-crash");
   216   Assert.deepEqual(crashes[0].crashDate, DUMMY_DATE);
   218   count = yield m.aggregateEventsFiles();
   219   Assert.equal(count, 0);
   220 });
   222 add_task(function* test_multiline_crash_id_rejected() {
   223   let m = yield getManager();
   224   yield m.createEventsFile("1", "crash.main.1", DUMMY_DATE, "id1\nid2");
   225   yield m.aggregateEventsFiles();
   226   let crashes = yield m.getCrashes();
   227   Assert.equal(crashes.length, 0);
   228 });
   230 add_task(function* test_plugin_crash_event_file() {
   231   let m = yield getManager();
   232   yield m.createEventsFile("1", "crash.plugin.1", DUMMY_DATE, "id1");
   233   let count = yield m.aggregateEventsFiles();
   234   Assert.equal(count, 1);
   236   let crashes = yield m.getCrashes();
   237   Assert.equal(crashes.length, 1);
   238   Assert.equal(crashes[0].id, "id1");
   239   Assert.equal(crashes[0].type, "plugin-crash");
   240   Assert.deepEqual(crashes[0].crashDate, DUMMY_DATE);
   242   count = yield m.aggregateEventsFiles();
   243   Assert.equal(count, 0);
   244 });
   246 add_task(function* test_plugin_hang_event_file() {
   247   let m = yield getManager();
   248   yield m.createEventsFile("1", "hang.plugin.1", DUMMY_DATE, "id1");
   249   let count = yield m.aggregateEventsFiles();
   250   Assert.equal(count, 1);
   252   let crashes = yield m.getCrashes();
   253   Assert.equal(crashes.length, 1);
   254   Assert.equal(crashes[0].id, "id1");
   255   Assert.equal(crashes[0].type, "plugin-hang");
   256   Assert.deepEqual(crashes[0].crashDate, DUMMY_DATE);
   258   count = yield m.aggregateEventsFiles();
   259   Assert.equal(count, 0);
   260 });
   262 // Excessive amounts of files should be processed properly.
   263 add_task(function* test_high_water_mark() {
   264   let m = yield getManager();
   266   let store = yield m._getStore();
   268   for (let i = 0; i < store.HIGH_WATER_DAILY_THRESHOLD + 1; i++) {
   269     yield m.createEventsFile("m" + i, "crash.main.1", DUMMY_DATE, "m" + i);
   270     yield m.createEventsFile("pc" + i, "crash.plugin.1", DUMMY_DATE, "pc" + i);
   271     yield m.createEventsFile("ph" + i, "hang.plugin.1", DUMMY_DATE, "ph" + i);
   272   }
   274   let count = yield m.aggregateEventsFiles();
   275   Assert.equal(count, 3 * bsp.CrashStore.prototype.HIGH_WATER_DAILY_THRESHOLD + 3);
   277   // Need to fetch again in case the first one was garbage collected.
   278   store = yield m._getStore();
   279   // +1 is for preserved main process crash.
   280   Assert.equal(store.crashesCount, 3 * store.HIGH_WATER_DAILY_THRESHOLD + 1);
   281 });

mercurial