mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-15 14:25:52 +00:00
Bug 927560 - Shutdown osfile_async_worker.js, companion tests;r=froydnj
This commit is contained in:
parent
6afd91eca2
commit
90d10a9c6f
@ -162,7 +162,6 @@ let test = maketest("Main", function main(test) {
|
||||
yield test_iter();
|
||||
yield test_exists();
|
||||
yield test_debug_test();
|
||||
yield test_system_shutdown();
|
||||
yield test_duration();
|
||||
info("Test is over");
|
||||
SimpleTest.finish();
|
||||
@ -724,92 +723,6 @@ let test_debug_test = maketest("debug_test", function debug_test(test) {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Test logging of file descriptors leaks.
|
||||
*/
|
||||
let test_system_shutdown = maketest("system_shutdown", function system_shutdown(test) {
|
||||
|
||||
// Test that unclosed files cause warnings
|
||||
// Test that unclosed directories cause warnings
|
||||
// Test that closed files do not cause warnings
|
||||
// Test that closed directories do not cause warnings
|
||||
|
||||
return Task.spawn(function () {
|
||||
function testLeaksOf(resource, topic) {
|
||||
return Task.spawn(function() {
|
||||
let deferred = Promise.defer();
|
||||
|
||||
// Register observer
|
||||
Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log", true);
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log.redirect", true);
|
||||
Services.prefs.setCharPref("toolkit.osfile.test.shutdown.observer", topic);
|
||||
|
||||
let observer = {
|
||||
observe: function(aMessage) {
|
||||
try {
|
||||
test.info("Got message: " + aMessage);
|
||||
if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) {
|
||||
return;
|
||||
}
|
||||
let message = aMessage.message;
|
||||
test.info("Got message: " + message);
|
||||
if (message.indexOf("TEST OS Controller WARNING") < 0) {
|
||||
return;
|
||||
}
|
||||
test.info("Got message: " + message + ", looking for resource " + resource);
|
||||
if (message.indexOf(resource) < 0) {
|
||||
return;
|
||||
}
|
||||
test.info("Resource: " + resource + " found");
|
||||
setTimeout(deferred.resolve);
|
||||
} catch (ex) {
|
||||
setTimeout(function() {
|
||||
deferred.reject(ex);
|
||||
});
|
||||
}
|
||||
}};
|
||||
Services.console.registerListener(observer);
|
||||
Services.obs.notifyObservers(null, topic, null);
|
||||
setTimeout(function() {
|
||||
test.info("Timeout while waiting for resource: " + resource);
|
||||
deferred.reject("timeout");
|
||||
}, 1000);
|
||||
|
||||
let resolved = false;
|
||||
try {
|
||||
yield deferred.promise;
|
||||
resolved = true;
|
||||
} catch (ex) {
|
||||
if (ex != "timeout") {
|
||||
test.ok(false, "Error during 'test.osfile.web-workers-shutdown'" + ex);
|
||||
}
|
||||
resolved = false;
|
||||
}
|
||||
Services.console.unregisterListener(observer);
|
||||
Services.prefs.clearUserPref("toolkit.osfile.log");
|
||||
Services.prefs.clearUserPref("toolkit.osfile.log.redirect");
|
||||
Services.prefs.clearUserPref("toolkit.osfile.test.shutdown.observer");
|
||||
Services.prefs.clearUserPref("toolkit.async_shutdown.testing", true);
|
||||
|
||||
throw new Task.Result(resolved);
|
||||
});
|
||||
}
|
||||
|
||||
let TEST_DIR = OS.Path.join((yield OS.File.getCurrentDirectory()), "..");
|
||||
test.info("Testing for leaks of directory iterator " + TEST_DIR);
|
||||
let iterator = new OS.File.DirectoryIterator(TEST_DIR);
|
||||
ok((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.leak")), "Detected directory leak");
|
||||
yield iterator.close();
|
||||
ok(!(yield testLeaksOf(TEST_DIR, "test.shutdown.dir.noleak")), "We don't leak the directory anymore");
|
||||
|
||||
test.info("Testing for leaks of file descriptor: " + EXISTING_FILE);
|
||||
let openedFile = yield OS.File.open(EXISTING_FILE);
|
||||
ok((yield testLeaksOf(EXISTING_FILE, "test.shutdown.file.leak")), "Detected file leak");
|
||||
yield openedFile.close();
|
||||
ok(!(yield testLeaksOf(EXISTING_FILE, "test.shutdown.file.noleak")), "We don't leak the file anymore");
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Test optional duration reporting that can be used for telemetry.
|
||||
|
80
toolkit/components/osfile/tests/xpcshell/test_reset.js
Normal file
80
toolkit/components/osfile/tests/xpcshell/test_reset.js
Normal file
@ -0,0 +1,80 @@
|
||||
Components.utils.import("resource://gre/modules/Services.jsm", this);
|
||||
Components.utils.import("resource://gre/modules/Promise.jsm", this);
|
||||
Components.utils.import("resource://gre/modules/Task.jsm", this);
|
||||
Components.utils.import("resource://gre/modules/osfile.jsm", this);
|
||||
|
||||
let Path = OS.Constants.Path;
|
||||
|
||||
add_task(function init() {
|
||||
do_get_profile();
|
||||
});
|
||||
|
||||
add_task(function reset_before_launching() {
|
||||
do_print("Reset without launching OS.File, it shouldn't break");
|
||||
yield OS.File.resetWorker();
|
||||
});
|
||||
|
||||
add_task(function transparent_reset() {
|
||||
for (let i = 1; i < 3; ++i) {
|
||||
do_print("Do stome stuff before and after " + i + " reset(s), " +
|
||||
"it shouldn't break");
|
||||
let CONTENT = "some content " + i;
|
||||
let path = OS.Path.join(Path.profileDir, "tmp");
|
||||
yield OS.File.writeAtomic(path, CONTENT);
|
||||
for (let j = 0; j < i; ++j) {
|
||||
yield OS.File.resetWorker();
|
||||
}
|
||||
let data = yield OS.File.read(path);
|
||||
let string = (new TextDecoder()).decode(data);
|
||||
do_check_eq(string, CONTENT);
|
||||
}
|
||||
});
|
||||
|
||||
add_task(function file_open_cannot_reset() {
|
||||
let TEST_FILE = OS.Path.join(Path.profileDir, "tmp-" + Math.random());
|
||||
do_print("Leaking file descriptor " + TEST_FILE + ", we shouldn't be able to reset");
|
||||
let openedFile = yield OS.File.open(TEST_FILE, { create: true} );
|
||||
let thrown = false;
|
||||
try {
|
||||
yield OS.File.resetWorker();
|
||||
} catch (ex if ex.message.indexOf(TEST_FILE) != -1 ) {
|
||||
thrown = true;
|
||||
}
|
||||
do_check_true(thrown);
|
||||
|
||||
do_print("Closing the file, we should now be able to reset");
|
||||
yield openedFile.close();
|
||||
yield OS.File.resetWorker();
|
||||
});
|
||||
|
||||
add_task(function file_open_cannot_reset() {
|
||||
let TEST_DIR = yield OS.File.getCurrentDirectory();
|
||||
do_print("Leaking directory " + TEST_DIR + ", we shouldn't be able to reset");
|
||||
let iterator = new OS.File.DirectoryIterator(TEST_DIR);
|
||||
let thrown = false;
|
||||
try {
|
||||
yield OS.File.resetWorker();
|
||||
} catch (ex if ex.message.indexOf(TEST_DIR) != -1 ) {
|
||||
thrown = true;
|
||||
}
|
||||
do_check_true(thrown);
|
||||
|
||||
do_print("Closing the directory, we should now be able to reset");
|
||||
yield iterator.close();
|
||||
yield OS.File.resetWorker();
|
||||
});
|
||||
|
||||
add_task(function finish_with_a_reset() {
|
||||
do_print("Reset without waiting for the result");
|
||||
// Arbitrary operation, just to wake up the worker
|
||||
try {
|
||||
yield OS.File.read("/foo");
|
||||
} catch (ex) {
|
||||
}
|
||||
// Now reset
|
||||
/*don't yield*/ OS.File.resetWorker();
|
||||
});
|
||||
|
||||
function run_test() {
|
||||
run_next_test();
|
||||
}
|
98
toolkit/components/osfile/tests/xpcshell/test_shutdown.js
Normal file
98
toolkit/components/osfile/tests/xpcshell/test_shutdown.js
Normal file
@ -0,0 +1,98 @@
|
||||
Components.utils.import("resource://gre/modules/Services.jsm", this);
|
||||
Components.utils.import("resource://gre/modules/Promise.jsm", this);
|
||||
Components.utils.import("resource://gre/modules/Task.jsm", this);
|
||||
Components.utils.import("resource://gre/modules/osfile.jsm", this);
|
||||
|
||||
add_task(function init() {
|
||||
do_get_profile();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test logging of file descriptors leaks.
|
||||
*/
|
||||
add_task(function system_shutdown() {
|
||||
|
||||
// Test that unclosed files cause warnings
|
||||
// Test that unclosed directories cause warnings
|
||||
// Test that closed files do not cause warnings
|
||||
// Test that closed directories do not cause warnings
|
||||
function testLeaksOf(resource, topic) {
|
||||
return Task.spawn(function() {
|
||||
let deferred = Promise.defer();
|
||||
|
||||
// Register observer
|
||||
Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log", true);
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log.redirect", true);
|
||||
Services.prefs.setCharPref("toolkit.osfile.test.shutdown.observer", topic);
|
||||
|
||||
let observer = function(aMessage) {
|
||||
try {
|
||||
do_print("Got message: " + aMessage);
|
||||
if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) {
|
||||
return;
|
||||
}
|
||||
let message = aMessage.message;
|
||||
do_print("Got message: " + message);
|
||||
if (message.indexOf("TEST OS Controller WARNING") < 0) {
|
||||
return;
|
||||
}
|
||||
do_print("Got message: " + message + ", looking for resource " + resource);
|
||||
if (message.indexOf(resource) < 0) {
|
||||
return;
|
||||
}
|
||||
do_print("Resource: " + resource + " found");
|
||||
do_execute_soon(deferred.resolve);
|
||||
} catch (ex) {
|
||||
do_execute_soon(function() {
|
||||
deferred.reject(ex);
|
||||
});
|
||||
}
|
||||
};
|
||||
Services.console.registerListener(observer);
|
||||
Services.obs.notifyObservers(null, topic, null);
|
||||
do_timeout(1000, function() {
|
||||
do_print("Timeout while waiting for resource: " + resource);
|
||||
deferred.reject("timeout");
|
||||
});
|
||||
|
||||
let resolved = false;
|
||||
try {
|
||||
yield deferred.promise;
|
||||
resolved = true;
|
||||
} catch (ex if ex == "timeout") {
|
||||
resolved = false;
|
||||
}
|
||||
Services.console.unregisterListener(observer);
|
||||
Services.prefs.clearUserPref("toolkit.osfile.log");
|
||||
Services.prefs.clearUserPref("toolkit.osfile.log.redirect");
|
||||
Services.prefs.clearUserPref("toolkit.osfile.test.shutdown.observer");
|
||||
Services.prefs.clearUserPref("toolkit.async_shutdown.testing", true);
|
||||
|
||||
throw new Task.Result(resolved);
|
||||
});
|
||||
}
|
||||
|
||||
let TEST_DIR = OS.Path.join((yield OS.File.getCurrentDirectory()), "..");
|
||||
do_print("Testing for leaks of directory iterator " + TEST_DIR);
|
||||
let iterator = new OS.File.DirectoryIterator(TEST_DIR);
|
||||
do_print("At this stage, we leak the directory");
|
||||
do_check_true((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.leak")));
|
||||
yield iterator.close();
|
||||
do_print("At this stage, we don't leak the directory anymore");
|
||||
do_check_false((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.noleak")));
|
||||
|
||||
let TEST_FILE = OS.Path.join(OS.Constants.Path.profileDir, "test");
|
||||
do_print("Testing for leaks of file descriptor: " + TEST_FILE);
|
||||
let openedFile = yield OS.File.open(TEST_FILE, { create: true} );
|
||||
do_print("At this stage, we leak the file");
|
||||
do_check_true((yield testLeaksOf(TEST_FILE, "test.shutdown.file.leak")));
|
||||
yield openedFile.close();
|
||||
do_print("At this stage, we don't leak the file anymore");
|
||||
do_check_false((yield testLeaksOf(TEST_FILE, "test.shutdown.file.leak")));
|
||||
});
|
||||
|
||||
|
||||
function run_test() {
|
||||
run_next_test();
|
||||
}
|
@ -13,4 +13,6 @@ tail =
|
||||
[test_exception.js]
|
||||
[test_path_constants.js]
|
||||
[test_removeDir.js]
|
||||
[test_reset.js]
|
||||
[test_shutdown.js]
|
||||
[test_unique.js]
|
||||
|
Loading…
Reference in New Issue
Block a user