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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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);
     6 let Path = OS.Constants.Path;
     8 add_task(function init() {
     9   do_get_profile();
    10 });
    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 });
    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 });
    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);
    45   do_print("Closing the file, we should now be able to reset");
    46   yield openedFile.close();
    47   yield OS.File.resetWorker();
    48 });
    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);
    62   do_print("Closing the directory, we should now be able to reset");
    63   yield iterator.close();
    64   yield OS.File.resetWorker();
    65 });
    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 });
    78 function run_test() {
    79   run_next_test();
    80 }

mercurial