toolkit/modules/tests/xpcshell/test_AsyncShutdown.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.

michael@0 1 let Cu = Components.utils;
michael@0 2
michael@0 3 Cu.import("resource://gre/modules/Services.jsm");
michael@0 4 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 5 Cu.import("resource://gre/modules/AsyncShutdown.jsm");
michael@0 6
michael@0 7 Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
michael@0 8
michael@0 9 /**
michael@0 10 * An asynchronous task that takes several ticks to complete.
michael@0 11 *
michael@0 12 * @param {*=} resolution The value with which the resulting promise will be
michael@0 13 * resolved once the task is complete. This may be a rejected promise,
michael@0 14 * in which case the resulting promise will itself be rejected.
michael@0 15 * @param {object=} outResult An object modified by side-effect during the task.
michael@0 16 * Initially, its field |isFinished| is set to |false|. Once the task is
michael@0 17 * complete, its field |isFinished| is set to |true|.
michael@0 18 *
michael@0 19 * @return {promise} A promise fulfilled once the task is complete
michael@0 20 */
michael@0 21 function longRunningAsyncTask(resolution = undefined, outResult = {}) {
michael@0 22 outResult.isFinished = false;
michael@0 23 if (!("countFinished" in outResult)) {
michael@0 24 outResult.countFinished = 0;
michael@0 25 }
michael@0 26 let deferred = Promise.defer();
michael@0 27 do_timeout(100, function() {
michael@0 28 ++outResult.countFinished;
michael@0 29 outResult.isFinished = true;
michael@0 30 deferred.resolve(resolution);
michael@0 31 });
michael@0 32 return deferred.promise;
michael@0 33 }
michael@0 34
michael@0 35 /**
michael@0 36 * Generate a unique notification topic.
michael@0 37 */
michael@0 38 function getUniqueTopic() {
michael@0 39 const PREFIX = "testing-phases-";
michael@0 40 return PREFIX + ++getUniqueTopic.counter;
michael@0 41 }
michael@0 42 getUniqueTopic.counter = 0;
michael@0 43
michael@0 44 add_task(function test_no_condition() {
michael@0 45 do_print("Testing a phase with no condition");
michael@0 46 let topic = getUniqueTopic();
michael@0 47 AsyncShutdown._getPhase(topic);
michael@0 48 Services.obs.notifyObservers(null, topic, null);
michael@0 49 do_print("Phase with no condition didn't lock");
michael@0 50 });
michael@0 51
michael@0 52 add_task(function test_simple_async() {
michael@0 53 do_print("Testing various combinations of a phase with a single condition");
michael@0 54 for (let arg of [undefined, null, "foo", 100, new Error("BOOM")]) {
michael@0 55 for (let resolution of [arg, Promise.reject(arg)]) {
michael@0 56 for (let success of [false, true]) {
michael@0 57 for (let state of [[null],
michael@0 58 [],
michael@0 59 [() => "some state"],
michael@0 60 [function() {
michael@0 61 throw new Error("State BOOM"); }],
michael@0 62 [function() {
michael@0 63 return {
michael@0 64 toJSON: function() {
michael@0 65 throw new Error("State.toJSON BOOM");
michael@0 66 }
michael@0 67 };
michael@0 68 }]]) {
michael@0 69 // Asynchronous phase
michael@0 70 do_print("Asynchronous test with " + arg + ", " + resolution);
michael@0 71 let topic = getUniqueTopic();
michael@0 72 let outParam = { isFinished: false };
michael@0 73 AsyncShutdown._getPhase(topic).addBlocker(
michael@0 74 "Async test",
michael@0 75 function() {
michael@0 76 if (success) {
michael@0 77 return longRunningAsyncTask(resolution, outParam);
michael@0 78 } else {
michael@0 79 throw resolution;
michael@0 80 }
michael@0 81 },
michael@0 82 ...state
michael@0 83 );
michael@0 84 do_check_false(outParam.isFinished);
michael@0 85 Services.obs.notifyObservers(null, topic, null);
michael@0 86 do_check_eq(outParam.isFinished, success);
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 // Synchronous phase - just test that we don't throw/freeze
michael@0 91 do_print("Synchronous test with " + arg + ", " + resolution);
michael@0 92 let topic = getUniqueTopic();
michael@0 93 AsyncShutdown._getPhase(topic).addBlocker(
michael@0 94 "Sync test",
michael@0 95 resolution
michael@0 96 );
michael@0 97 Services.obs.notifyObservers(null, topic, null);
michael@0 98 }
michael@0 99 }
michael@0 100 });
michael@0 101
michael@0 102 add_task(function test_many() {
michael@0 103 do_print("Testing various combinations of a phase with many conditions");
michael@0 104 let topic = getUniqueTopic();
michael@0 105 let phase = AsyncShutdown._getPhase(topic);
michael@0 106 let outParams = [];
michael@0 107 for (let arg of [undefined, null, "foo", 100, new Error("BOOM")]) {
michael@0 108 for (let resolution of [arg, Promise.reject(arg)]) {
michael@0 109 let outParam = { isFinished: false };
michael@0 110 phase.addBlocker(
michael@0 111 "Test",
michael@0 112 () => longRunningAsyncTask(resolution, outParam)
michael@0 113 );
michael@0 114 }
michael@0 115 }
michael@0 116 do_check_true(outParams.every((x) => !x.isFinished));
michael@0 117 Services.obs.notifyObservers(null, topic, null);
michael@0 118 do_check_true(outParams.every((x) => x.isFinished));
michael@0 119 });
michael@0 120
michael@0 121 function get_exn(f) {
michael@0 122 try {
michael@0 123 f();
michael@0 124 return null;
michael@0 125 } catch (ex) {
michael@0 126 return ex;
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 add_task(function test_various_failures() {
michael@0 131 do_print("Ensure that we cannot add a condition for a phase that is already complete");
michael@0 132 let topic = getUniqueTopic();
michael@0 133 let phase = AsyncShutdown._getPhase(topic);
michael@0 134 Services.obs.notifyObservers(null, topic, null);
michael@0 135 let exn = get_exn(() => phase.addBlocker("Test", true));
michael@0 136 do_check_true(!!exn);
michael@0 137
michael@0 138 do_print("Ensure that an incomplete blocker causes a TypeError");
michael@0 139
michael@0 140 exn = get_exn(() => phase.addBlocker());
michael@0 141 do_check_eq(exn.name, "TypeError");
michael@0 142
michael@0 143 exn = get_exn(() => phase.addBlocker(null, true));
michael@0 144 do_check_eq(exn.name, "TypeError");
michael@0 145
michael@0 146 exn = get_exn(() => phase.addBlocker("Test 2", () => true, "not a function"));
michael@0 147 do_check_eq(exn.name, "TypeError");
michael@0 148 });
michael@0 149
michael@0 150 add_task(function() {
michael@0 151 Services.prefs.clearUserPref("toolkit.asyncshutdown.testing");
michael@0 152 });
michael@0 153
michael@0 154 function run_test() {
michael@0 155 run_next_test();
michael@0 156 }

mercurial