Skip to content

Commit

Permalink
Fix waitUntil not working with async/await. (#4123)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Priyansh Garg <priyanshgarg30@gmail.com>
  • Loading branch information
chikara1608 and garg3133 authored Mar 21, 2024
1 parent b4e273a commit b08981d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/api/_loaders/_command-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class BaseCommandLoader extends BaseLoader {
const redact = this.module.RedactParams || false;
const alwaysAsync = this.module.alwaysAsync || false;
const isTraceable = this.module.isTraceable;
const avoidPrematureParentNodeResolution = !!this.module.avoidPrematureParentNodeResolution;

return {redact, alwaysAsync, isTraceable};
return {redact, alwaysAsync, isTraceable, avoidPrematureParentNodeResolution};
}

validateMethod(parent) {
Expand Down Expand Up @@ -75,7 +76,7 @@ class BaseCommandLoader extends BaseLoader {
}

getTargetNamespace(parent) {
let namespace = this.namespace;
const namespace = this.namespace;

if (!parent) {
return namespace;
Expand Down
4 changes: 4 additions & 0 deletions lib/api/protocol/waitUntil.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ module.exports = class WaitUntil extends ProtocolAction {
return true;
}

static get avoidPrematureParentNodeResolution() {
return true;
}

//timeMs, retryInterval, message, callback = function(r) {return r}
async command(conditionFn, ...args) {
let callback = function(r) {return r};
Expand Down
15 changes: 14 additions & 1 deletion lib/core/asynctree.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ class AsyncTree extends EventEmitter{
return parentNode.childNodes.filter(item => item !== node && !item.done).length > 0;
}

shouldAvoidParentNodeResolution(node) {
const parentNode = node.parent;

if (!parentNode || parentNode.isRootNode) {
return true;
}

const result = parentNode.options?.avoidPrematureParentNodeResolution;

return Boolean(result);
}

shouldRejectNodePromise(err, node = this.currentNode) {
if ((err.isExpect || node.namespace === 'assert') && this.currentNode.isES6Async) {
return true;
Expand Down Expand Up @@ -183,13 +195,14 @@ class AsyncTree extends EventEmitter{

setTimeout(() => {
const stillInProgress = this.otherChildNodesInProgress(node);
const avoidPrematureParentNodeResolution = this.shouldAvoidParentNodeResolution(node);

if (node.context && node.context instanceof EventEmitter) {
node.context.emit('complete');
}

const isAssertion = node.parent && (node.parent.namespace === 'assert' || node.parent.namespace === 'verify');
if (node.parent && !node.parent.isRootNode && !isAssertion && !stillInProgress) {
if (node.parent && !node.parent.isRootNode && !isAssertion && !stillInProgress && !avoidPrematureParentNodeResolution) {
node.done = true;
this.resolveNode(node.parent, result, ++times);
}
Expand Down

0 comments on commit b08981d

Please sign in to comment.