summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_reset.js
blob: 41dea1e9dd1cdfabc6db5b5be36f6414c5591e48 (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
96
97
98
99
100
101
102
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var Path = OS.Constants.Path;

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

add_task(async function reset_before_launching() {
  info("Reset without launching OS.File, it shouldn't break");
  await OS.File.resetWorker();
});

add_task(async function transparent_reset() {
  for (let i = 1; i < 3; ++i) {
    info(
      "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");
    await OS.File.writeAtomic(path, CONTENT);
    for (let j = 0; j < i; ++j) {
      await OS.File.resetWorker();
    }
    let data = await OS.File.read(path);
    let string = new TextDecoder().decode(data);
    Assert.equal(string, CONTENT);
  }
});

add_task(async function file_open_cannot_reset() {
  let TEST_FILE = OS.Path.join(Path.profileDir, "tmp-" + Math.random());
  info(
    "Leaking file descriptor " + TEST_FILE + ", we shouldn't be able to reset"
  );
  let openedFile = await OS.File.open(TEST_FILE, { create: true });
  let thrown = false;
  try {
    await OS.File.resetWorker();
  } catch (ex) {
    if (ex.message.includes(OS.Path.basename(TEST_FILE))) {
      thrown = true;
    } else {
      throw ex;
    }
  }
  Assert.ok(thrown);

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

add_task(async function dir_open_cannot_reset() {
  let TEST_DIR = await OS.File.getCurrentDirectory();
  info("Leaking directory " + TEST_DIR + ", we shouldn't be able to reset");
  let iterator = new OS.File.DirectoryIterator(TEST_DIR);
  let thrown = false;
  try {
    await OS.File.resetWorker();
  } catch (ex) {
    if (ex.message.includes(OS.Path.basename(TEST_DIR))) {
      thrown = true;
    } else {
      throw ex;
    }
  }
  Assert.ok(thrown);

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

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

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

  await Promise.all(all);
});

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