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

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:7ce5a3abc81b
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 "use strict";
5
6 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
7
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);
12
13 Cu.import("resource://testing-common/CrashManagerTest.jsm", this);
14
15 const DUMMY_DATE = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
16 DUMMY_DATE.setMilliseconds(0);
17
18 function run_test() {
19 do_get_profile();
20 configureLogging();
21 run_next_test();
22 }
23
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 });
33
34 add_task(function* test_constructor_invalid() {
35 Assert.throws(() => {
36 new CrashManager({foo: true});
37 });
38 });
39
40 add_task(function* test_get_manager() {
41 let m = yield getManager();
42 Assert.ok(m, "CrashManager obtained.");
43
44 yield m.createDummyDump(true);
45 yield m.createDummyDump(false);
46 });
47
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;
54
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);
59
60 let entries = yield m.pendingDumps();
61 Assert.equal(entries.length, COUNT, "proper number detected.");
62
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 }
70
71 for (let i = 0; i < COUNT; i++) {
72 Assert.equal(entries[i].id, ids[COUNT-i-1], "Entries sorted by mtime");
73 }
74 });
75
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;
80
81 for (let i = 0; i < COUNT; i++) {
82 yield m.createDummyDump(true);
83 }
84 yield m.createIgnoredDumpFile("ignored", true);
85
86 let entries = yield m.submittedDumps();
87 Assert.equal(entries.length, COUNT, "proper number detected.");
88
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.");
92
93 let gotIDs = new Set([e.id for (e of entries)]);
94 Assert.ok(gotIDs.has(hrID));
95 });
96
97 // The store should expire after inactivity.
98 add_task(function* test_store_expires() {
99 let m = yield getManager();
100
101 Object.defineProperty(m, "STORE_EXPIRATION_MS", {
102 value: 250,
103 });
104
105 let store = yield m._getStore();
106 Assert.ok(store);
107 Assert.equal(store, m._store);
108
109 yield sleep(300);
110 Assert.ok(!m._store, "Store has gone away.");
111 });
112
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);
119
120 let paths = yield m._getUnprocessedEventsFiles();
121 Assert.equal(paths.length, 3);
122 });
123
124 // Ensure only 1 aggregateEventsFiles() is allowed at a time.
125 add_task(function* test_aggregate_events_locking() {
126 let m = yield getManager();
127
128 let p1 = m.aggregateEventsFiles();
129 let p2 = m.aggregateEventsFiles();
130
131 Assert.strictEqual(p1, p2, "Same promise should be returned.");
132 });
133
134 // Malformed events files should be deleted.
135 add_task(function* test_malformed_files_deleted() {
136 let m = yield getManager();
137
138 yield m.createEventsFile("1", "crash.main.1", new Date(), "foo\nbar");
139
140 let count = yield m.aggregateEventsFiles();
141 Assert.equal(count, 1);
142 let crashes = yield m.getCrashes();
143 Assert.equal(crashes.length, 0);
144
145 count = yield m.aggregateEventsFiles();
146 Assert.equal(count, 0);
147 });
148
149 // Unknown event types should be ignored.
150 add_task(function* test_aggregate_ignore_unknown_events() {
151 let m = yield getManager();
152
153 yield m.createEventsFile("1", "crash.main.1", DUMMY_DATE, "id1");
154 yield m.createEventsFile("2", "foobar.1", new Date(), "dummy");
155
156 let count = yield m.aggregateEventsFiles();
157 Assert.equal(count, 2);
158
159 count = yield m.aggregateEventsFiles();
160 Assert.equal(count, 1);
161
162 count = yield m.aggregateEventsFiles();
163 Assert.equal(count, 1);
164 });
165
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");
172
173 yield m.aggregateEventsFiles();
174
175 let crashes = yield m.getCrashes();
176 Assert.equal(crashes.length, 2);
177
178 yield m.pruneOldCrashes(new Date(oldDate.getTime() + 10000));
179
180 crashes = yield m.getCrashes();
181 Assert.equal(crashes.length, 1, "Old crash has been pruned.");
182
183 let c = crashes[0];
184 Assert.equal(c.id, "id2", "Proper crash was pruned.");
185
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 });
192
193 add_task(function* test_schedule_maintenance() {
194 let m = yield getManager();
195 yield m.createEventsFile("1", "crash.main.1", DUMMY_DATE, "id1");
196
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");
199
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 });
205
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);
211
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);
217
218 count = yield m.aggregateEventsFiles();
219 Assert.equal(count, 0);
220 });
221
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 });
229
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);
235
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);
241
242 count = yield m.aggregateEventsFiles();
243 Assert.equal(count, 0);
244 });
245
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);
251
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);
257
258 count = yield m.aggregateEventsFiles();
259 Assert.equal(count, 0);
260 });
261
262 // Excessive amounts of files should be processed properly.
263 add_task(function* test_high_water_mark() {
264 let m = yield getManager();
265
266 let store = yield m._getStore();
267
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 }
273
274 let count = yield m.aggregateEventsFiles();
275 Assert.equal(count, 3 * bsp.CrashStore.prototype.HIGH_WATER_DAILY_THRESHOLD + 3);
276
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