Skip to content

Commit 180a38e

Browse files
TrottMylesBorins
authored andcommitted
test: move common.fires() to inspector-helper
common.fires() is specific to the inspector tests so move it to inspector-helper.js. The one REPL test that used common.fires() does not seem to need it. It provided a 1 second timeout for operations, but that timeout appears both arbitrary and ineffective as the test passes if it is reduced to even 1 millisecond. Backport-PR-URL: #18096 PR-URL: #17401 Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 92defcc commit 180a38e

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

test/common/README.md

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

118118
Checks if `pathname` exists
119119

120-
### fires(promise, [error], [timeoutMs])
121-
* promise [&lt;Promise]
122-
* error [&lt;String] default = 'timeout'
123-
* timeoutMs [&lt;Number] default = 100
124-
125-
Returns a new promise that will propagate `promise` resolution or rejection if
126-
that happens within the `timeoutMs` timespan, or rejects with `error` as
127-
a reason otherwise.
128-
129120
### getArrayBufferViews(buf)
130121
* `buf` [&lt;Buffer>]
131122
* return [&lt;ArrayBufferView&#91;&#93;>]

test/common/index.js

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

875-
function onResolvedOrRejected(promise, callback) {
876-
return promise.then((result) => {
877-
callback();
878-
return result;
879-
}, (error) => {
880-
callback();
881-
throw error;
882-
});
883-
}
884-
885-
function timeoutPromise(error, timeoutMs) {
886-
let clearCallback = null;
887-
let done = false;
888-
const promise = onResolvedOrRejected(new Promise((resolve, reject) => {
889-
const timeout = setTimeout(() => reject(error), timeoutMs);
890-
clearCallback = () => {
891-
if (done)
892-
return;
893-
clearTimeout(timeout);
894-
resolve();
895-
};
896-
}), () => done = true);
897-
promise.clear = clearCallback;
898-
return promise;
899-
}
900-
901875
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
902876
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
903877
exports.restoreStdout = restoreWritable.bind(null, 'stdout');
@@ -911,19 +885,3 @@ exports.firstInvalidFD = function firstInvalidFD() {
911885
} catch (e) {}
912886
return fd;
913887
};
914-
915-
exports.fires = function fires(promise, error, timeoutMs) {
916-
if (!timeoutMs && util.isNumber(error)) {
917-
timeoutMs = error;
918-
error = null;
919-
}
920-
if (!error)
921-
error = 'timeout';
922-
if (!timeoutMs)
923-
timeoutMs = 100;
924-
const timeout = timeoutPromise(error, timeoutMs);
925-
return Promise.race([
926-
onResolvedOrRejected(promise, () => timeout.clear()),
927-
timeout
928-
]);
929-
};

test/common/inspector-helper.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class InspectorSession {
216216
waitForNotification(methodOrPredicate, description) {
217217
const desc = description || methodOrPredicate;
218218
const message = `Timed out waiting for matching notification (${desc}))`;
219-
return common.fires(
219+
return fires(
220220
this._asyncWaitForNotification(methodOrPredicate), message, TIMEOUT);
221221
}
222222

@@ -323,7 +323,7 @@ class NodeInstance {
323323
const instance = new NodeInstance(
324324
[], `${scriptContents}\nprocess._rawDebug('started');`, undefined);
325325
const msg = 'Timed out waiting for process to start';
326-
while (await common.fires(instance.nextStderrString(), msg, TIMEOUT) !==
326+
while (await fires(instance.nextStderrString(), msg, TIMEOUT) !==
327327
'started') {}
328328
process._debugProcess(instance._process.pid);
329329
return instance;
@@ -412,6 +412,43 @@ function readMainScriptSource() {
412412
return fs.readFileSync(_MAINSCRIPT, 'utf8');
413413
}
414414

415+
function onResolvedOrRejected(promise, callback) {
416+
return promise.then((result) => {
417+
callback();
418+
return result;
419+
}, (error) => {
420+
callback();
421+
throw error;
422+
});
423+
}
424+
425+
function timeoutPromise(error, timeoutMs) {
426+
let clearCallback = null;
427+
let done = false;
428+
const promise = onResolvedOrRejected(new Promise((resolve, reject) => {
429+
const timeout = setTimeout(() => reject(error), timeoutMs);
430+
clearCallback = () => {
431+
if (done)
432+
return;
433+
clearTimeout(timeout);
434+
resolve();
435+
};
436+
}), () => done = true);
437+
promise.clear = clearCallback;
438+
return promise;
439+
}
440+
441+
// Returns a new promise that will propagate `promise` resolution or rejection
442+
// if that happens within the `timeoutMs` timespan, or rejects with `error` as
443+
// a reason otherwise.
444+
function fires(promise, error, timeoutMs) {
445+
const timeout = timeoutPromise(error, timeoutMs);
446+
return Promise.race([
447+
onResolvedOrRejected(promise, () => timeout.clear()),
448+
timeout
449+
]);
450+
}
451+
415452
module.exports = {
416453
mainScriptPath: _MAINSCRIPT,
417454
readMainScriptSource,

0 commit comments

Comments
 (0)