2016-11-22 01:41:20 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
const {utils: Cu} = Components;
|
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
const {PollPromise} = Cu.import("chrome://marionette/content/sync.js", {});
|
2017-10-02 18:38:33 +03:00
|
|
|
|
|
|
|
const DEFAULT_TIMEOUT = 2000;
|
2016-11-22 01:41:20 +03:00
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
add_task(async function test_PollPromise_types() {
|
2016-11-22 01:41:20 +03:00
|
|
|
for (let typ of [true, false, "foo", 42, [], {}]) {
|
2017-10-02 18:50:11 +03:00
|
|
|
strictEqual(typ, await new PollPromise(resolve => resolve(typ)));
|
2016-11-22 01:41:20 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
add_task(async function test_PollPromise_timeoutElapse() {
|
2016-11-22 01:41:20 +03:00
|
|
|
let nevals = 0;
|
|
|
|
let start = new Date().getTime();
|
2017-10-02 18:50:11 +03:00
|
|
|
await new PollPromise((resolve, reject) => {
|
2016-11-22 01:41:20 +03:00
|
|
|
++nevals;
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
let end = new Date().getTime();
|
2017-10-02 18:38:33 +03:00
|
|
|
greaterOrEqual((end - start), DEFAULT_TIMEOUT);
|
2017-02-13 23:02:01 +03:00
|
|
|
greaterOrEqual(nevals, 15);
|
2016-11-22 01:41:20 +03:00
|
|
|
});
|
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
add_task(async function test_PollPromise_rethrowError() {
|
2016-11-22 01:41:20 +03:00
|
|
|
let nevals = 0;
|
|
|
|
let err;
|
|
|
|
try {
|
2017-10-02 18:50:11 +03:00
|
|
|
await PollPromise(() => {
|
2016-11-22 01:41:20 +03:00
|
|
|
++nevals;
|
|
|
|
throw new Error();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
equal(1, nevals);
|
|
|
|
ok(err instanceof Error);
|
|
|
|
});
|
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
add_task(async function test_PollPromise_noTimeout() {
|
2016-11-22 01:41:20 +03:00
|
|
|
// run at least once when timeout is 0
|
|
|
|
let nevals = 0;
|
|
|
|
let start = new Date().getTime();
|
2017-10-02 18:50:11 +03:00
|
|
|
await new PollPromise((resolve, reject) => {
|
2016-11-22 01:41:20 +03:00
|
|
|
++nevals;
|
|
|
|
reject();
|
2017-10-09 17:22:59 +03:00
|
|
|
}, 0);
|
2016-11-22 01:41:20 +03:00
|
|
|
let end = new Date().getTime();
|
|
|
|
equal(1, nevals);
|
2017-10-02 18:38:33 +03:00
|
|
|
less((end - start), DEFAULT_TIMEOUT);
|
2016-11-22 01:41:20 +03:00
|
|
|
});
|
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
add_task(async function test_PollPromise_timeout() {
|
2016-11-22 01:41:20 +03:00
|
|
|
let nevals = 0;
|
|
|
|
let start = new Date().getTime();
|
2017-10-02 18:50:11 +03:00
|
|
|
await new PollPromise((resolve, reject) => {
|
2016-11-22 01:41:20 +03:00
|
|
|
++nevals;
|
|
|
|
reject();
|
2017-10-09 17:22:59 +03:00
|
|
|
}, 100);
|
2016-11-22 01:41:20 +03:00
|
|
|
let end = new Date().getTime();
|
|
|
|
greater(nevals, 1);
|
|
|
|
greaterOrEqual((end - start), 100);
|
|
|
|
});
|
|
|
|
|
2017-10-02 18:50:11 +03:00
|
|
|
add_task(async function test_PollPromise_interval() {
|
2016-11-22 01:41:20 +03:00
|
|
|
let nevals = 0;
|
2017-10-02 18:50:11 +03:00
|
|
|
await new PollPromise((resolve, reject) => {
|
2016-11-22 01:41:20 +03:00
|
|
|
++nevals;
|
|
|
|
reject();
|
2017-10-09 17:22:59 +03:00
|
|
|
}, 100, 100);
|
2016-11-22 01:41:20 +03:00
|
|
|
equal(2, nevals);
|
|
|
|
});
|