Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | Components.utils.import("resource://gre/modules/Services.jsm", this); |
michael@0 | 2 | Components.utils.import("resource://gre/modules/Promise.jsm", this); |
michael@0 | 3 | Components.utils.import("resource://gre/modules/Task.jsm", this); |
michael@0 | 4 | Components.utils.import("resource://gre/modules/osfile.jsm", this); |
michael@0 | 5 | |
michael@0 | 6 | add_task(function init() { |
michael@0 | 7 | do_get_profile(); |
michael@0 | 8 | }); |
michael@0 | 9 | |
michael@0 | 10 | /** |
michael@0 | 11 | * Test logging of file descriptors leaks. |
michael@0 | 12 | */ |
michael@0 | 13 | add_task(function system_shutdown() { |
michael@0 | 14 | |
michael@0 | 15 | // Test that unclosed files cause warnings |
michael@0 | 16 | // Test that unclosed directories cause warnings |
michael@0 | 17 | // Test that closed files do not cause warnings |
michael@0 | 18 | // Test that closed directories do not cause warnings |
michael@0 | 19 | function testLeaksOf(resource, topic) { |
michael@0 | 20 | return Task.spawn(function() { |
michael@0 | 21 | let deferred = Promise.defer(); |
michael@0 | 22 | |
michael@0 | 23 | // Register observer |
michael@0 | 24 | Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true); |
michael@0 | 25 | Services.prefs.setBoolPref("toolkit.osfile.log", true); |
michael@0 | 26 | Services.prefs.setBoolPref("toolkit.osfile.log.redirect", true); |
michael@0 | 27 | Services.prefs.setCharPref("toolkit.osfile.test.shutdown.observer", topic); |
michael@0 | 28 | |
michael@0 | 29 | let observer = function(aMessage) { |
michael@0 | 30 | try { |
michael@0 | 31 | do_print("Got message: " + aMessage); |
michael@0 | 32 | if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) { |
michael@0 | 33 | return; |
michael@0 | 34 | } |
michael@0 | 35 | let message = aMessage.message; |
michael@0 | 36 | do_print("Got message: " + message); |
michael@0 | 37 | if (message.indexOf("TEST OS Controller WARNING") < 0) { |
michael@0 | 38 | return; |
michael@0 | 39 | } |
michael@0 | 40 | do_print("Got message: " + message + ", looking for resource " + resource); |
michael@0 | 41 | if (message.indexOf(resource) < 0) { |
michael@0 | 42 | return; |
michael@0 | 43 | } |
michael@0 | 44 | do_print("Resource: " + resource + " found"); |
michael@0 | 45 | do_execute_soon(deferred.resolve); |
michael@0 | 46 | } catch (ex) { |
michael@0 | 47 | do_execute_soon(function() { |
michael@0 | 48 | deferred.reject(ex); |
michael@0 | 49 | }); |
michael@0 | 50 | } |
michael@0 | 51 | }; |
michael@0 | 52 | Services.console.registerListener(observer); |
michael@0 | 53 | Services.obs.notifyObservers(null, topic, null); |
michael@0 | 54 | do_timeout(1000, function() { |
michael@0 | 55 | do_print("Timeout while waiting for resource: " + resource); |
michael@0 | 56 | deferred.reject("timeout"); |
michael@0 | 57 | }); |
michael@0 | 58 | |
michael@0 | 59 | let resolved = false; |
michael@0 | 60 | try { |
michael@0 | 61 | yield deferred.promise; |
michael@0 | 62 | resolved = true; |
michael@0 | 63 | } catch (ex if ex == "timeout") { |
michael@0 | 64 | resolved = false; |
michael@0 | 65 | } |
michael@0 | 66 | Services.console.unregisterListener(observer); |
michael@0 | 67 | Services.prefs.clearUserPref("toolkit.osfile.log"); |
michael@0 | 68 | Services.prefs.clearUserPref("toolkit.osfile.log.redirect"); |
michael@0 | 69 | Services.prefs.clearUserPref("toolkit.osfile.test.shutdown.observer"); |
michael@0 | 70 | Services.prefs.clearUserPref("toolkit.async_shutdown.testing", true); |
michael@0 | 71 | |
michael@0 | 72 | throw new Task.Result(resolved); |
michael@0 | 73 | }); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | let TEST_DIR = OS.Path.join((yield OS.File.getCurrentDirectory()), ".."); |
michael@0 | 77 | do_print("Testing for leaks of directory iterator " + TEST_DIR); |
michael@0 | 78 | let iterator = new OS.File.DirectoryIterator(TEST_DIR); |
michael@0 | 79 | do_print("At this stage, we leak the directory"); |
michael@0 | 80 | do_check_true((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.leak"))); |
michael@0 | 81 | yield iterator.close(); |
michael@0 | 82 | do_print("At this stage, we don't leak the directory anymore"); |
michael@0 | 83 | do_check_false((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.noleak"))); |
michael@0 | 84 | |
michael@0 | 85 | let TEST_FILE = OS.Path.join(OS.Constants.Path.profileDir, "test"); |
michael@0 | 86 | do_print("Testing for leaks of file descriptor: " + TEST_FILE); |
michael@0 | 87 | let openedFile = yield OS.File.open(TEST_FILE, { create: true} ); |
michael@0 | 88 | do_print("At this stage, we leak the file"); |
michael@0 | 89 | do_check_true((yield testLeaksOf(TEST_FILE, "test.shutdown.file.leak"))); |
michael@0 | 90 | yield openedFile.close(); |
michael@0 | 91 | do_print("At this stage, we don't leak the file anymore"); |
michael@0 | 92 | do_check_false((yield testLeaksOf(TEST_FILE, "test.shutdown.file.leak.2"))); |
michael@0 | 93 | }); |
michael@0 | 94 | |
michael@0 | 95 | |
michael@0 | 96 | function run_test() { |
michael@0 | 97 | run_next_test(); |
michael@0 | 98 | } |