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