Skip to content

Commit 5f31d54

Browse files
author
Eugene Ostroukhov
committed
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: nodejs#14797 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent cde272a commit 5f31d54

22 files changed

Lines changed: 863 additions & 1114 deletions

test/common/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ Tests whether `name` and `expected` are part of a raised warning.
9999

100100
Checks if `pathname` exists
101101

102+
### fires(promise, [error], [timeoutMs])
103+
* promise [&lt;Promise]
104+
* error [&lt;String] default = 'timeout'
105+
* timeoutMs [&lt;Number] default = 100
106+
107+
Returns a new promise that will propagate `promise` resolution or rejection if
108+
that happens within the `timeoutMs` timespan, or rejects with `error` as
109+
a reason otherwise.
110+
102111
### fixturesDir
103112
* return [&lt;String>]
104113

test/common/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,32 @@ function restoreWritable(name) {
814814
delete process[name].writeTimes;
815815
}
816816

817+
function onResolvedOrRejected(promise, callback) {
818+
return promise.then((result) => {
819+
callback();
820+
return result;
821+
}, (error) => {
822+
callback();
823+
throw error;
824+
});
825+
}
826+
827+
function timeoutPromise(error, timeoutMs) {
828+
let clearCallback = null;
829+
let done = false;
830+
const promise = onResolvedOrRejected(new Promise((resolve, reject) => {
831+
const timeout = setTimeout(() => reject(error), timeoutMs);
832+
clearCallback = () => {
833+
if (done)
834+
return;
835+
clearTimeout(timeout);
836+
resolve();
837+
};
838+
}), () => done = true);
839+
promise.clear = clearCallback;
840+
return promise;
841+
}
842+
817843
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
818844
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
819845
exports.restoreStdout = restoreWritable.bind(null, 'stdout');
@@ -827,3 +853,19 @@ exports.firstInvalidFD = function firstInvalidFD() {
827853
} catch (e) {}
828854
return fd;
829855
};
856+
857+
exports.fires = function fires(promise, error, timeoutMs) {
858+
if (!timeoutMs && util.isNumber(error)) {
859+
timeoutMs = error;
860+
error = null;
861+
}
862+
if (!error)
863+
error = 'timeout';
864+
if (!timeoutMs)
865+
timeoutMs = 100;
866+
const timeout = timeoutPromise(error, timeoutMs);
867+
return Promise.race([
868+
onResolvedOrRejected(promise, () => timeout.clear()),
869+
timeout
870+
]);
871+
};

0 commit comments

Comments
 (0)