Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: move common.fires() to inspector-helper #17401

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ Tests whether `name` and `expected` are part of a raised warning.

Checks if `pathname` exists

### fires(promise, [error], [timeoutMs])
* promise [<Promise]
* error [<String] default = 'timeout'
* timeoutMs [<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.

### getArrayBufferViews(buf)
* `buf` [<Buffer>]
* return [<ArrayBufferView[]>]
Expand Down
42 changes: 0 additions & 42 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,32 +836,6 @@ 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');
Expand All @@ -875,19 +849,3 @@ 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
]);
};
41 changes: 39 additions & 2 deletions test/common/inspector-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class InspectorSession {
waitForNotification(methodOrPredicate, description) {
const desc = description || methodOrPredicate;
const message = `Timed out waiting for matching notification (${desc}))`;
return common.fires(
return fires(
this._asyncWaitForNotification(methodOrPredicate), message, TIMEOUT);
}

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

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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this code for timeoutPromise is a little convoluted IMO - any reason we can't do.

const promisify = require('util').promisify;
const delay = promisify(setTimeout);
const timeoutPromise = async (error, timeoutMs, cancelToken) => {
  await delay(ms);
  if(cancelToken.cancelled) { 
    return;
  }
  throw error;
};

Turns fires to:

function fires(promise, error, timeoutMs) {
  var token = {};
  return Promise.race([
     onResolvedOrRejected(promise, () => token.cancelled = true),
     timeoutPromise(error, timeoutMs, token)
   ]);
 }

If we want to preserve the onResolvedOnRejected behavior (which I'm not sure I understand) we can just use Promise.prototype.finally which is in V8 6.3

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues with further refactoring, but I'd prefer to do that in a separate PR.

const timeout = setTimeout(() => reject(error), timeoutMs);
clearCallback = () => {
if (done)
return;
clearTimeout(timeout);
resolve();
};
}), () => done = true);
promise.clear = clearCallback;
return promise;
}

// 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.
function fires(promise, error, timeoutMs) {
const timeout = timeoutPromise(error, timeoutMs);
return Promise.race([
onResolvedOrRejected(promise, () => timeout.clear()),
timeout
]);
}

module.exports = {
mainScriptPath: _MAINSCRIPT,
readMainScriptSource,
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-repl-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class REPLStream extends common.ArrayStream {
throw new Error('Currently waiting for response to another command');
}
this.lines = [''];
return common.fires(new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const onError = (err) => {
this.removeListener('line', onLine);
reject(err);
Expand All @@ -50,7 +50,7 @@ class REPLStream extends common.ArrayStream {
};
this.once('error', onError);
this.on('line', onLine);
}), new Error(), 1000);
});
}
}

Expand Down