Skip to content

Commit e2ae08b

Browse files
Eugene OstroukhovMylesBorins
Eugene Ostroukhov
authored andcommitted
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: #14797 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 7facfaa commit e2ae08b

22 files changed

+868
-1119
lines changed

test/common/README.md

+9
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

+42
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,32 @@ function restoreWritable(name) {
819819
delete process[name].writeTimes;
820820
}
821821

822+
function onResolvedOrRejected(promise, callback) {
823+
return promise.then((result) => {
824+
callback();
825+
return result;
826+
}, (error) => {
827+
callback();
828+
throw error;
829+
});
830+
}
831+
832+
function timeoutPromise(error, timeoutMs) {
833+
let clearCallback = null;
834+
let done = false;
835+
const promise = onResolvedOrRejected(new Promise((resolve, reject) => {
836+
const timeout = setTimeout(() => reject(error), timeoutMs);
837+
clearCallback = () => {
838+
if (done)
839+
return;
840+
clearTimeout(timeout);
841+
resolve();
842+
};
843+
}), () => done = true);
844+
promise.clear = clearCallback;
845+
return promise;
846+
}
847+
822848
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
823849
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
824850
exports.restoreStdout = restoreWritable.bind(null, 'stdout');
@@ -832,3 +858,19 @@ exports.firstInvalidFD = function firstInvalidFD() {
832858
} catch (e) {}
833859
return fd;
834860
};
861+
862+
exports.fires = function fires(promise, error, timeoutMs) {
863+
if (!timeoutMs && util.isNumber(error)) {
864+
timeoutMs = error;
865+
error = null;
866+
}
867+
if (!error)
868+
error = 'timeout';
869+
if (!timeoutMs)
870+
timeoutMs = 100;
871+
const timeout = timeoutPromise(error, timeoutMs);
872+
return Promise.race([
873+
onResolvedOrRejected(promise, () => timeout.clear()),
874+
timeout
875+
]);
876+
};

0 commit comments

Comments
 (0)