summaryrefslogtreecommitdiff
path: root/test/common/inspector-helper.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-11-29 15:59:04 -0500
committerRich Trott <rtrott@gmail.com>2017-12-04 14:10:10 -0800
commite1054abea0344f08bb6bba0b9aef342c690de8fd (patch)
treec0562d4bed6c2a55de128cb5ac9ccddac94e1132 /test/common/inspector-helper.js
parenta322b8e31671a1e6b1a4581e76dd696e00a393e6 (diff)
downloadandroid-node-v8-e1054abea0344f08bb6bba0b9aef342c690de8fd.tar.gz
android-node-v8-e1054abea0344f08bb6bba0b9aef342c690de8fd.tar.bz2
android-node-v8-e1054abea0344f08bb6bba0b9aef342c690de8fd.zip
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: https://github.com/nodejs/node/pull/17401 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test/common/inspector-helper.js')
-rw-r--r--test/common/inspector-helper.js41
1 files changed, 39 insertions, 2 deletions
diff --git a/test/common/inspector-helper.js b/test/common/inspector-helper.js
index 454eef4c5e..8fc778555d 100644
--- a/test/common/inspector-helper.js
+++ b/test/common/inspector-helper.js
@@ -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);
}
@@ -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;
@@ -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) => {
+ 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,