summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_reset.js
blob: f1e1b14d14af8a7ec2e28c6c4d7d0520dcfd20c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var Path = OS.Constants.Path;

add_task(function* init() {
  do_get_profile();
});

add_task(function* reset_before_launching() {
  do_print("Reset without launching OS.File, it shouldn't break");
  yield OS.File.resetWorker();
});

add_task(function* transparent_reset() {
  for (let i = 1; i < 3; ++i) {
    do_print("Do stome stuff before and after " + i + " reset(s), " +
             "it shouldn't break");
    let CONTENT = "some content " + i;
    let path = OS.Path.join(Path.profileDir, "tmp");
    yield OS.File.writeAtomic(path, CONTENT);
    for (let j = 0; j < i; ++j) {
      yield OS.File.resetWorker();
    }
    let data = yield OS.File.read(path);
    let string = (new TextDecoder()).decode(data);
    do_check_eq(string, CONTENT);
  }
});

add_task(function* file_open_cannot_reset() {
  let TEST_FILE = OS.Path.join(Path.profileDir, "tmp-" + Math.random());
  do_print("Leaking file descriptor " + TEST_FILE + ", we shouldn't be able to reset");
  let openedFile = yield OS.File.open(TEST_FILE, { create: true} );
  let thrown = false;
  try {
    yield OS.File.resetWorker();
  } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) {
    thrown = true;
  }
  do_check_true(thrown);

  do_print("Closing the file, we should now be able to reset");
  yield openedFile.close();
  yield OS.File.resetWorker();
});

add_task(function* dir_open_cannot_reset() {
  let TEST_DIR = yield OS.File.getCurrentDirectory();
  do_print("Leaking directory " + TEST_DIR + ", we shouldn't be able to reset");
  let iterator = new OS.File.DirectoryIterator(TEST_DIR);
  let thrown = false;
  try {
    yield OS.File.resetWorker();
  } catch (ex if ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) {
    thrown = true;
  }
  do_check_true(thrown);

  do_print("Closing the directory, we should now be able to reset");
  yield iterator.close();
  yield OS.File.resetWorker();
});

add_task(function* race_against_itself() {
  do_print("Attempt to get resetWorker() to race against itself");
  // Arbitrary operation, just to wake up the worker
  try {
    yield OS.File.read("/foo");
  } catch (ex) {
  }

  let all = [];
  for (let i = 0; i < 100; ++i) {
    all.push(OS.File.resetWorker());
  }

  yield Promise.all(all);
});


add_task(function* finish_with_a_reset() {
  do_print("Reset without waiting for the result");
  // Arbitrary operation, just to wake up the worker
  try {
    yield OS.File.read("/foo");
  } catch (ex) {
  }
  // Now reset
  /*don't yield*/ OS.File.resetWorker();
});

function run_test() {
  run_next_test();
}