Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | let Path = OS.Constants.Path; |
michael@0 | 7 | |
michael@0 | 8 | add_task(function init() { |
michael@0 | 9 | do_get_profile(); |
michael@0 | 10 | }); |
michael@0 | 11 | |
michael@0 | 12 | add_task(function reset_before_launching() { |
michael@0 | 13 | do_print("Reset without launching OS.File, it shouldn't break"); |
michael@0 | 14 | yield OS.File.resetWorker(); |
michael@0 | 15 | }); |
michael@0 | 16 | |
michael@0 | 17 | add_task(function transparent_reset() { |
michael@0 | 18 | for (let i = 1; i < 3; ++i) { |
michael@0 | 19 | do_print("Do stome stuff before and after " + i + " reset(s), " + |
michael@0 | 20 | "it shouldn't break"); |
michael@0 | 21 | let CONTENT = "some content " + i; |
michael@0 | 22 | let path = OS.Path.join(Path.profileDir, "tmp"); |
michael@0 | 23 | yield OS.File.writeAtomic(path, CONTENT); |
michael@0 | 24 | for (let j = 0; j < i; ++j) { |
michael@0 | 25 | yield OS.File.resetWorker(); |
michael@0 | 26 | } |
michael@0 | 27 | let data = yield OS.File.read(path); |
michael@0 | 28 | let string = (new TextDecoder()).decode(data); |
michael@0 | 29 | do_check_eq(string, CONTENT); |
michael@0 | 30 | } |
michael@0 | 31 | }); |
michael@0 | 32 | |
michael@0 | 33 | add_task(function file_open_cannot_reset() { |
michael@0 | 34 | let TEST_FILE = OS.Path.join(Path.profileDir, "tmp-" + Math.random()); |
michael@0 | 35 | do_print("Leaking file descriptor " + TEST_FILE + ", we shouldn't be able to reset"); |
michael@0 | 36 | let openedFile = yield OS.File.open(TEST_FILE, { create: true} ); |
michael@0 | 37 | let thrown = false; |
michael@0 | 38 | try { |
michael@0 | 39 | yield OS.File.resetWorker(); |
michael@0 | 40 | } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) { |
michael@0 | 41 | thrown = true; |
michael@0 | 42 | } |
michael@0 | 43 | do_check_true(thrown); |
michael@0 | 44 | |
michael@0 | 45 | do_print("Closing the file, we should now be able to reset"); |
michael@0 | 46 | yield openedFile.close(); |
michael@0 | 47 | yield OS.File.resetWorker(); |
michael@0 | 48 | }); |
michael@0 | 49 | |
michael@0 | 50 | add_task(function dir_open_cannot_reset() { |
michael@0 | 51 | let TEST_DIR = yield OS.File.getCurrentDirectory(); |
michael@0 | 52 | do_print("Leaking directory " + TEST_DIR + ", we shouldn't be able to reset"); |
michael@0 | 53 | let iterator = new OS.File.DirectoryIterator(TEST_DIR); |
michael@0 | 54 | let thrown = false; |
michael@0 | 55 | try { |
michael@0 | 56 | yield OS.File.resetWorker(); |
michael@0 | 57 | } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) { |
michael@0 | 58 | thrown = true; |
michael@0 | 59 | } |
michael@0 | 60 | do_check_true(thrown); |
michael@0 | 61 | |
michael@0 | 62 | do_print("Closing the directory, we should now be able to reset"); |
michael@0 | 63 | yield iterator.close(); |
michael@0 | 64 | yield OS.File.resetWorker(); |
michael@0 | 65 | }); |
michael@0 | 66 | |
michael@0 | 67 | add_task(function finish_with_a_reset() { |
michael@0 | 68 | do_print("Reset without waiting for the result"); |
michael@0 | 69 | // Arbitrary operation, just to wake up the worker |
michael@0 | 70 | try { |
michael@0 | 71 | yield OS.File.read("/foo"); |
michael@0 | 72 | } catch (ex) { |
michael@0 | 73 | } |
michael@0 | 74 | // Now reset |
michael@0 | 75 | /*don't yield*/ OS.File.resetWorker(); |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | function run_test() { |
michael@0 | 79 | run_next_test(); |
michael@0 | 80 | } |