Skip to content

Commit e1054ab

Browse files
committed
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. PR-URL: #17401 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent a322b8e commit e1054ab

File tree

4 files changed

+41
-55
lines changed

4 files changed

+41
-55
lines changed

test/common/README.md

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

119119
Checks if `pathname` exists
120120

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

test/common/index.js

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

853-
function onResolvedOrRejected(promise, callback) {
854-
return promise.then((result) => {
855-
callback();
856-
return result;
857-
}, (error) => {
858-
callback();
859-
throw error;
860-
});
861-
}
862-
863-
function timeoutPromise(error, timeoutMs) {
864-
let clearCallback = null;
865-
let done = false;
866-
const promise = onResolvedOrRejected(new Promise((resolve, reject) => {
867-
const timeout = setTimeout(() => reject(error), timeoutMs);
868-
clearCallback = () => {
869-
if (done)
870-
return;
871-
clearTimeout(timeout);
872-
resolve();
873-
};
874-
}), () => done = true);
875-
promise.clear = clearCallback;
876-
return promise;
877-
}
878-
879853
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
880854
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
881855
exports.restoreStdout = restoreWritable.bind(null, 'stdout');
@@ -889,19 +863,3 @@ exports.firstInvalidFD = function firstInvalidFD() {
889863
} catch (e) {}
890864
return fd;
891865
};
892-
893-
exports.fires = function fires(promise, error, timeoutMs) {
894-
if (!timeoutMs && util.isNumber(error)) {
895-
timeoutMs = error;
896-
error = null;
897-
}
898-
if (!error)
899-
error = 'timeout';
900-
if (!timeoutMs)
901-
timeoutMs = 100;
902-
const timeout = timeoutPromise(error, timeoutMs);
903-
return Promise.race([
904-
onResolvedOrRejected(promise, () => timeout.clear()),
905-
timeout
906-
]);
907-
};

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,

test/parallel/test-repl-top-level-await.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class REPLStream extends common.ArrayStream {
3636
throw new Error('Currently waiting for response to another command');
3737
}
3838
this.lines = [''];
39-
return common.fires(new Promise((resolve, reject) => {
39+
return new Promise((resolve, reject) => {
4040
const onError = (err) => {
4141
this.removeListener('line', onLine);
4242
reject(err);
@@ -50,7 +50,7 @@ class REPLStream extends common.ArrayStream {
5050
};
5151
this.once('error', onError);
5252
this.on('line', onLine);
53-
}), new Error(), 1000);
53+
});
5454
}
5555
}
5656

0 commit comments

Comments
 (0)