Permalink
Browse files

inspector: rewrite inspector test helper

Helper was rewritten to rely on promises instead of manually written
queue and callbacks. This simplifies the code and makes it easier to
maintain and extend.

PR-URL: #14460
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
Eugene Ostroukhov
Eugene Ostroukhov committed Jul 24, 2017
1 parent 340b3be commit 2296b677fb1e2ea71e86a899b028ba9e817e86ad
View
@@ -99,6 +99,15 @@ Tests whether `name` and `expected` are part of a raised warning.
Checks if `pathname` exists
### fires(promise, [error], [timeoutMs])
* promise [&lt;Promise]
* error [&lt;String] default = 'timeout'
* timeoutMs [&lt;Number] default = 100
Returns a new promise that will propagate `promise` resolution or rejection if
that happens within the `timeoutMs` timespan, or rejects with `error` as
a reason otherwise.
### fixturesDir
* return [&lt;String>]
View
@@ -814,6 +814,32 @@ function restoreWritable(name) {
delete process[name].writeTimes;
}
function onResolvedOrRejected(promise, callback) {
return promise.then((result) => {
callback();
return result;
}, (error) => {
callback();
throw error;
});
}
function timeoutPromise(error, timeoutMs) {
let clearCallback = null;
let done = false;
const promise = onResolvedOrRejected(new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(error), timeoutMs);
clearCallback = () => {
if (done)
return;
clearTimeout(timeout);
resolve();
};
}), () => done = true);
promise.clear = clearCallback;
return promise;
}
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
exports.restoreStdout = restoreWritable.bind(null, 'stdout');
@@ -827,3 +853,19 @@ exports.firstInvalidFD = function firstInvalidFD() {
} catch (e) {}
return fd;
};
exports.fires = function fires(promise, error, timeoutMs) {
if (!timeoutMs && util.isNumber(error)) {
timeoutMs = error;
error = null;
}
if (!error)
error = 'timeout';
if (!timeoutMs)
timeoutMs = 100;
const timeout = timeoutPromise(error, timeoutMs);
return Promise.race([
onResolvedOrRejected(promise, () => timeout.clear()),
timeout
]);
};
Oops, something went wrong.

0 comments on commit 2296b67

Please sign in to comment.