toolkit/components/crashes/tests/xpcshell/test_crash_store.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.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /*
michael@0 5 * This file tests the CrashStore type in CrashManager.jsm.
michael@0 6 */
michael@0 7
michael@0 8 "use strict";
michael@0 9
michael@0 10 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 11
michael@0 12 let bsp = Cu.import("resource://gre/modules/CrashManager.jsm", this);
michael@0 13 Cu.import("resource://gre/modules/osfile.jsm", this);
michael@0 14 Cu.import("resource://gre/modules/Task.jsm", this);
michael@0 15
michael@0 16 const CrashStore = bsp.CrashStore;
michael@0 17
michael@0 18 let STORE_DIR_COUNT = 0;
michael@0 19
michael@0 20 function getStore() {
michael@0 21 return Task.spawn(function* () {
michael@0 22 let storeDir = do_get_tempdir().path;
michael@0 23 storeDir = OS.Path.join(storeDir, "store-" + STORE_DIR_COUNT++);
michael@0 24
michael@0 25 yield OS.File.makeDir(storeDir, {unixMode: OS.Constants.libc.S_IRWXU});
michael@0 26
michael@0 27 let s = new CrashStore(storeDir);
michael@0 28 yield s.load();
michael@0 29
michael@0 30 return s;
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 function run_test() {
michael@0 35 run_next_test();
michael@0 36 }
michael@0 37
michael@0 38 add_task(function* test_constructor() {
michael@0 39 let s = new CrashStore("/some/path");
michael@0 40 Assert.ok(s instanceof CrashStore);
michael@0 41 });
michael@0 42
michael@0 43 add_task(function test_add_crash() {
michael@0 44 let s = yield getStore();
michael@0 45
michael@0 46 Assert.equal(s.crashesCount, 0);
michael@0 47 let d = new Date(Date.now() - 5000);
michael@0 48 s.addMainProcessCrash("id1", d);
michael@0 49
michael@0 50 Assert.equal(s.crashesCount, 1);
michael@0 51
michael@0 52 let crashes = s.crashes;
michael@0 53 Assert.equal(crashes.length, 1);
michael@0 54 let c = crashes[0];
michael@0 55
michael@0 56 Assert.equal(c.id, "id1", "ID set properly.");
michael@0 57 Assert.equal(c.crashDate.getTime(), d.getTime(), "Date set.");
michael@0 58
michael@0 59 s.addMainProcessCrash("id2", new Date());
michael@0 60 Assert.equal(s.crashesCount, 2);
michael@0 61 });
michael@0 62
michael@0 63 add_task(function test_save_load() {
michael@0 64 let s = yield getStore();
michael@0 65
michael@0 66 yield s.save();
michael@0 67
michael@0 68 let d1 = new Date();
michael@0 69 let d2 = new Date(d1.getTime() - 10000);
michael@0 70 s.addMainProcessCrash("id1", d1);
michael@0 71 s.addMainProcessCrash("id2", d2);
michael@0 72
michael@0 73 yield s.save();
michael@0 74
michael@0 75 yield s.load();
michael@0 76 Assert.ok(!s.corruptDate);
michael@0 77 let crashes = s.crashes;
michael@0 78
michael@0 79 Assert.equal(crashes.length, 2);
michael@0 80 let c = s.getCrash("id1");
michael@0 81 Assert.equal(c.crashDate.getTime(), d1.getTime());
michael@0 82 });
michael@0 83
michael@0 84 add_task(function test_corrupt_json() {
michael@0 85 let s = yield getStore();
michael@0 86
michael@0 87 let buffer = new TextEncoder().encode("{bad: json-file");
michael@0 88 yield OS.File.writeAtomic(s._storePath, buffer, {compression: "lz4"});
michael@0 89
michael@0 90 yield s.load();
michael@0 91 Assert.ok(s.corruptDate, "Corrupt date is defined.");
michael@0 92
michael@0 93 let date = s.corruptDate;
michael@0 94 yield s.save();
michael@0 95 s._data = null;
michael@0 96 yield s.load();
michael@0 97 Assert.ok(s.corruptDate);
michael@0 98 Assert.equal(date.getTime(), s.corruptDate.getTime());
michael@0 99 });
michael@0 100
michael@0 101 add_task(function* test_add_main_crash() {
michael@0 102 let s = yield getStore();
michael@0 103
michael@0 104 s.addMainProcessCrash("id1", new Date());
michael@0 105 Assert.equal(s.crashesCount, 1);
michael@0 106
michael@0 107 let c = s.crashes[0];
michael@0 108 Assert.ok(c.crashDate);
michael@0 109 Assert.equal(c.type, bsp.CrashStore.prototype.TYPE_MAIN_CRASH);
michael@0 110 Assert.ok(c.isMainProcessCrash);
michael@0 111
michael@0 112 s.addMainProcessCrash("id2", new Date());
michael@0 113 Assert.equal(s.crashesCount, 2);
michael@0 114
michael@0 115 // Duplicate.
michael@0 116 s.addMainProcessCrash("id1", new Date());
michael@0 117 Assert.equal(s.crashesCount, 2);
michael@0 118
michael@0 119 Assert.equal(s.mainProcessCrashes.length, 2);
michael@0 120 });
michael@0 121
michael@0 122 add_task(function* test_add_plugin_crash() {
michael@0 123 let s = yield getStore();
michael@0 124
michael@0 125 s.addPluginCrash("id1", new Date());
michael@0 126 Assert.equal(s.crashesCount, 1);
michael@0 127
michael@0 128 let c = s.crashes[0];
michael@0 129 Assert.ok(c.crashDate);
michael@0 130 Assert.equal(c.type, bsp.CrashStore.prototype.TYPE_PLUGIN_CRASH);
michael@0 131 Assert.ok(c.isPluginCrash);
michael@0 132
michael@0 133 s.addPluginCrash("id2", new Date());
michael@0 134 Assert.equal(s.crashesCount, 2);
michael@0 135
michael@0 136 s.addPluginCrash("id1", new Date());
michael@0 137 Assert.equal(s.crashesCount, 2);
michael@0 138
michael@0 139 Assert.equal(s.pluginCrashes.length, 2);
michael@0 140 });
michael@0 141
michael@0 142 add_task(function* test_add_plugin_hang() {
michael@0 143 let s = yield getStore();
michael@0 144
michael@0 145 s.addPluginHang("id1", new Date());
michael@0 146 Assert.equal(s.crashesCount, 1);
michael@0 147
michael@0 148 let c = s.crashes[0];
michael@0 149 Assert.ok(c.crashDate);
michael@0 150 Assert.equal(c.type, bsp.CrashStore.prototype.TYPE_PLUGIN_HANG);
michael@0 151 Assert.ok(c.isPluginHang);
michael@0 152
michael@0 153 s.addPluginHang("id2", new Date());
michael@0 154 Assert.equal(s.crashesCount, 2);
michael@0 155
michael@0 156 s.addPluginHang("id1", new Date());
michael@0 157 Assert.equal(s.crashesCount, 2);
michael@0 158
michael@0 159 Assert.equal(s.pluginHangs.length, 2);
michael@0 160 });
michael@0 161
michael@0 162 add_task(function* test_add_mixed_types() {
michael@0 163 let s = yield getStore();
michael@0 164
michael@0 165 s.addMainProcessCrash("main", new Date());
michael@0 166 s.addPluginCrash("pcrash", new Date());
michael@0 167 s.addPluginHang("phang", new Date());
michael@0 168
michael@0 169 Assert.equal(s.crashesCount, 3);
michael@0 170
michael@0 171 yield s.save();
michael@0 172
michael@0 173 s._data.crashes.clear();
michael@0 174 Assert.equal(s.crashesCount, 0);
michael@0 175
michael@0 176 yield s.load();
michael@0 177
michael@0 178 Assert.equal(s.crashesCount, 3);
michael@0 179
michael@0 180 Assert.equal(s.mainProcessCrashes.length, 1);
michael@0 181 Assert.equal(s.pluginCrashes.length, 1);
michael@0 182 Assert.equal(s.pluginHangs.length, 1);
michael@0 183 });
michael@0 184
michael@0 185 // Crashes added beyond the high water mark behave properly.
michael@0 186 add_task(function* test_high_water() {
michael@0 187 let s = yield getStore();
michael@0 188
michael@0 189 let d1 = new Date(2014, 0, 1, 0, 0, 0);
michael@0 190 let d2 = new Date(2014, 0, 2, 0, 0, 0);
michael@0 191
michael@0 192 for (let i = 0; i < s.HIGH_WATER_DAILY_THRESHOLD + 1; i++) {
michael@0 193 s.addMainProcessCrash("m1" + i, d1);
michael@0 194 s.addMainProcessCrash("m2" + i, d2);
michael@0 195 s.addPluginCrash("pc1" + i, d1);
michael@0 196 s.addPluginCrash("pc2" + i, d2);
michael@0 197 s.addPluginHang("ph1" + i, d1);
michael@0 198 s.addPluginHang("ph2" + i, d2);
michael@0 199 }
michael@0 200
michael@0 201 // We preserve main process crashes. Plugin crashes and hangs beyond should
michael@0 202 // be discarded.
michael@0 203 Assert.equal(s.crashesCount, 6 * s.HIGH_WATER_DAILY_THRESHOLD + 2);
michael@0 204 Assert.equal(s.mainProcessCrashes.length, 2 * s.HIGH_WATER_DAILY_THRESHOLD + 2);
michael@0 205 Assert.equal(s.pluginCrashes.length, 2 * s.HIGH_WATER_DAILY_THRESHOLD);
michael@0 206 Assert.equal(s.pluginHangs.length, 2 * s.HIGH_WATER_DAILY_THRESHOLD);
michael@0 207
michael@0 208 // But raw counts should be preserved.
michael@0 209 let day1 = bsp.dateToDays(d1);
michael@0 210 let day2 = bsp.dateToDays(d2);
michael@0 211 Assert.ok(s._countsByDay.has(day1));
michael@0 212 Assert.ok(s._countsByDay.has(day2));
michael@0 213 Assert.equal(s._countsByDay.get(day1).get(s.TYPE_MAIN_CRASH),
michael@0 214 s.HIGH_WATER_DAILY_THRESHOLD + 1);
michael@0 215 Assert.equal(s._countsByDay.get(day1).get(s.TYPE_PLUGIN_CRASH),
michael@0 216 s.HIGH_WATER_DAILY_THRESHOLD + 1);
michael@0 217 Assert.equal(s._countsByDay.get(day1).get(s.TYPE_PLUGIN_HANG),
michael@0 218 s.HIGH_WATER_DAILY_THRESHOLD + 1);
michael@0 219
michael@0 220 yield s.save();
michael@0 221 yield s.load();
michael@0 222
michael@0 223 Assert.ok(s._countsByDay.has(day1));
michael@0 224 Assert.ok(s._countsByDay.has(day2));
michael@0 225 Assert.equal(s._countsByDay.get(day1).get(s.TYPE_MAIN_CRASH),
michael@0 226 s.HIGH_WATER_DAILY_THRESHOLD + 1);
michael@0 227 Assert.equal(s._countsByDay.get(day1).get(s.TYPE_PLUGIN_CRASH),
michael@0 228 s.HIGH_WATER_DAILY_THRESHOLD + 1);
michael@0 229 Assert.equal(s._countsByDay.get(day1).get(s.TYPE_PLUGIN_HANG),
michael@0 230 s.HIGH_WATER_DAILY_THRESHOLD + 1);
michael@0 231 });

mercurial