michael@0: Components.utils.import("resource://gre/modules/Services.jsm", this); michael@0: Components.utils.import("resource://gre/modules/Promise.jsm", this); michael@0: Components.utils.import("resource://gre/modules/Task.jsm", this); michael@0: Components.utils.import("resource://gre/modules/osfile.jsm", this); michael@0: michael@0: let Path = OS.Constants.Path; michael@0: michael@0: add_task(function init() { michael@0: do_get_profile(); michael@0: }); michael@0: michael@0: add_task(function reset_before_launching() { michael@0: do_print("Reset without launching OS.File, it shouldn't break"); michael@0: yield OS.File.resetWorker(); michael@0: }); michael@0: michael@0: add_task(function transparent_reset() { michael@0: for (let i = 1; i < 3; ++i) { michael@0: do_print("Do stome stuff before and after " + i + " reset(s), " + michael@0: "it shouldn't break"); michael@0: let CONTENT = "some content " + i; michael@0: let path = OS.Path.join(Path.profileDir, "tmp"); michael@0: yield OS.File.writeAtomic(path, CONTENT); michael@0: for (let j = 0; j < i; ++j) { michael@0: yield OS.File.resetWorker(); michael@0: } michael@0: let data = yield OS.File.read(path); michael@0: let string = (new TextDecoder()).decode(data); michael@0: do_check_eq(string, CONTENT); michael@0: } michael@0: }); michael@0: michael@0: add_task(function file_open_cannot_reset() { michael@0: let TEST_FILE = OS.Path.join(Path.profileDir, "tmp-" + Math.random()); michael@0: do_print("Leaking file descriptor " + TEST_FILE + ", we shouldn't be able to reset"); michael@0: let openedFile = yield OS.File.open(TEST_FILE, { create: true} ); michael@0: let thrown = false; michael@0: try { michael@0: yield OS.File.resetWorker(); michael@0: } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) { michael@0: thrown = true; michael@0: } michael@0: do_check_true(thrown); michael@0: michael@0: do_print("Closing the file, we should now be able to reset"); michael@0: yield openedFile.close(); michael@0: yield OS.File.resetWorker(); michael@0: }); michael@0: michael@0: add_task(function dir_open_cannot_reset() { michael@0: let TEST_DIR = yield OS.File.getCurrentDirectory(); michael@0: do_print("Leaking directory " + TEST_DIR + ", we shouldn't be able to reset"); michael@0: let iterator = new OS.File.DirectoryIterator(TEST_DIR); michael@0: let thrown = false; michael@0: try { michael@0: yield OS.File.resetWorker(); michael@0: } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) { michael@0: thrown = true; michael@0: } michael@0: do_check_true(thrown); michael@0: michael@0: do_print("Closing the directory, we should now be able to reset"); michael@0: yield iterator.close(); michael@0: yield OS.File.resetWorker(); michael@0: }); michael@0: michael@0: add_task(function finish_with_a_reset() { michael@0: do_print("Reset without waiting for the result"); michael@0: // Arbitrary operation, just to wake up the worker michael@0: try { michael@0: yield OS.File.read("/foo"); michael@0: } catch (ex) { michael@0: } michael@0: // Now reset michael@0: /*don't yield*/ OS.File.resetWorker(); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }