summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_shutdown.js
blob: 667965d9ee0d3ebafaf0065ea8733e7297804547 (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
Components.utils.import("resource://gre/modules/Services.jsm", this);
Components.utils.import("resource://gre/modules/Promise.jsm", this);
Components.utils.import("resource://gre/modules/Task.jsm", this);
Components.utils.import("resource://gre/modules/osfile.jsm", this);

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

/**
 * Test logging of file descriptors leaks.
 */
add_task(function system_shutdown() {

  // Test that unclosed files cause warnings
  // Test that unclosed directories cause warnings
  // Test that closed files do not cause warnings
  // Test that closed directories do not cause warnings
  function testLeaksOf(resource, topic) {
    return Task.spawn(function() {
      let deferred = Promise.defer();

      // Register observer
      Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
      Services.prefs.setBoolPref("toolkit.osfile.log", true);
      Services.prefs.setBoolPref("toolkit.osfile.log.redirect", true);
      Services.prefs.setCharPref("toolkit.osfile.test.shutdown.observer", topic);

      let observer = function(aMessage) {
        try {
          do_print("Got message: " + aMessage);
          if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) {
            return;
          }
          let message = aMessage.message;
          do_print("Got message: " + message);
          if (message.indexOf("TEST OS Controller WARNING") < 0) {
            return;
          }
          do_print("Got message: " + message + ", looking for resource " + resource);
          if (message.indexOf(resource) < 0) {
            return;
          }
          do_print("Resource: " + resource + " found");
          do_execute_soon(deferred.resolve);
        } catch (ex) {
          do_execute_soon(function() {
            deferred.reject(ex);
          });
        }
      };
      Services.console.registerListener(observer);
      Services.obs.notifyObservers(null, topic, null);
      do_timeout(1000, function() {
        do_print("Timeout while waiting for resource: " + resource);
        deferred.reject("timeout");
      });

      let resolved = false;
      try {
        yield deferred.promise;
        resolved = true;
      } catch (ex if ex == "timeout") {
        resolved = false;
      }
      Services.console.unregisterListener(observer);
      Services.prefs.clearUserPref("toolkit.osfile.log");
      Services.prefs.clearUserPref("toolkit.osfile.log.redirect");
      Services.prefs.clearUserPref("toolkit.osfile.test.shutdown.observer");
      Services.prefs.clearUserPref("toolkit.async_shutdown.testing", true);

      throw new Task.Result(resolved);
    });
  }

  let TEST_DIR = OS.Path.join((yield OS.File.getCurrentDirectory()), "..");
  do_print("Testing for leaks of directory iterator " + TEST_DIR);
  let iterator = new OS.File.DirectoryIterator(TEST_DIR);
  do_print("At this stage, we leak the directory");
  do_check_true((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.leak")));
  yield iterator.close();
  do_print("At this stage, we don't leak the directory anymore");
  do_check_false((yield testLeaksOf(TEST_DIR, "test.shutdown.dir.noleak")));

  let TEST_FILE = OS.Path.join(OS.Constants.Path.profileDir, "test");
  do_print("Testing for leaks of file descriptor: " + TEST_FILE);
  let openedFile = yield OS.File.open(TEST_FILE, { create: true} );
  do_print("At this stage, we leak the file");
  do_check_true((yield testLeaksOf(TEST_FILE, "test.shutdown.file.leak")));
  yield openedFile.close();
  do_print("At this stage, we don't leak the file anymore");
  do_check_false((yield testLeaksOf(TEST_FILE, "test.shutdown.file.leak.2")));
});


function run_test() {
  run_next_test();
}