toolkit/components/osfile/tests/xpcshell/test_reset.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_reset.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +Components.utils.import("resource://gre/modules/Services.jsm", this);
     1.5 +Components.utils.import("resource://gre/modules/Promise.jsm", this);
     1.6 +Components.utils.import("resource://gre/modules/Task.jsm", this);
     1.7 +Components.utils.import("resource://gre/modules/osfile.jsm", this);
     1.8 +
     1.9 +let Path = OS.Constants.Path;
    1.10 +
    1.11 +add_task(function init() {
    1.12 +  do_get_profile();
    1.13 +});
    1.14 +
    1.15 +add_task(function reset_before_launching() {
    1.16 +  do_print("Reset without launching OS.File, it shouldn't break");
    1.17 +  yield OS.File.resetWorker();
    1.18 +});
    1.19 +
    1.20 +add_task(function transparent_reset() {
    1.21 +  for (let i = 1; i < 3; ++i) {
    1.22 +    do_print("Do stome stuff before and after " + i + " reset(s), " +
    1.23 +             "it shouldn't break");
    1.24 +    let CONTENT = "some content " + i;
    1.25 +    let path = OS.Path.join(Path.profileDir, "tmp");
    1.26 +    yield OS.File.writeAtomic(path, CONTENT);
    1.27 +    for (let j = 0; j < i; ++j) {
    1.28 +      yield OS.File.resetWorker();
    1.29 +    }
    1.30 +    let data = yield OS.File.read(path);
    1.31 +    let string = (new TextDecoder()).decode(data);
    1.32 +    do_check_eq(string, CONTENT);
    1.33 +  }
    1.34 +});
    1.35 +
    1.36 +add_task(function file_open_cannot_reset() {
    1.37 +  let TEST_FILE = OS.Path.join(Path.profileDir, "tmp-" + Math.random());
    1.38 +  do_print("Leaking file descriptor " + TEST_FILE + ", we shouldn't be able to reset");
    1.39 +  let openedFile = yield OS.File.open(TEST_FILE, { create: true} );
    1.40 +  let thrown = false;
    1.41 +  try {
    1.42 +    yield OS.File.resetWorker();
    1.43 +  } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) {
    1.44 +    thrown = true;
    1.45 +  }
    1.46 +  do_check_true(thrown);
    1.47 +
    1.48 +  do_print("Closing the file, we should now be able to reset");
    1.49 +  yield openedFile.close();
    1.50 +  yield OS.File.resetWorker();
    1.51 +});
    1.52 +
    1.53 +add_task(function dir_open_cannot_reset() {
    1.54 +  let TEST_DIR = yield OS.File.getCurrentDirectory();
    1.55 +  do_print("Leaking directory " + TEST_DIR + ", we shouldn't be able to reset");
    1.56 +  let iterator = new OS.File.DirectoryIterator(TEST_DIR);
    1.57 +  let thrown = false;
    1.58 +  try {
    1.59 +    yield OS.File.resetWorker();
    1.60 +  } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) {
    1.61 +    thrown = true;
    1.62 +  }
    1.63 +  do_check_true(thrown);
    1.64 +
    1.65 +  do_print("Closing the directory, we should now be able to reset");
    1.66 +  yield iterator.close();
    1.67 +  yield OS.File.resetWorker();
    1.68 +});
    1.69 +
    1.70 +add_task(function finish_with_a_reset() {
    1.71 +  do_print("Reset without waiting for the result");
    1.72 +  // Arbitrary operation, just to wake up the worker
    1.73 +  try {
    1.74 +    yield OS.File.read("/foo");
    1.75 +  } catch (ex) {
    1.76 +  }
    1.77 +  // Now reset
    1.78 +  /*don't yield*/ OS.File.resetWorker();
    1.79 +});
    1.80 +
    1.81 +function run_test() {
    1.82 +  run_next_test();
    1.83 +}

mercurial