Skip to content

Commit 54dbe41

Browse files
committed
js: In promise.ControlFlow#wait(), if the timeout is omitted or 0, wait
indefinitely for the condition to be satisfied. Fixes #337
1 parent 93732a7 commit 54dbe41

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## v2.46.0-dev
22

3+
* FIXED: `promise.ControlFlow#wait()` now has consistent semantics for an
4+
omitted or 0-timeout: it will wait indefinitely.
35
* FIXED: `remote.DriverService#start()` will now fail if the child process dies
46
while waiting for the server to start accepting requests. Previously, start
57
would continue to poll the server address until the timeout expired.

javascript/webdriver/promise.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,15 +1544,18 @@ promise.ControlFlow.prototype.timeout = function(ms, opt_description) {
15441544
* If the condition function throws, or returns a rejected promise, the
15451545
* wait task will fail.
15461546
*
1547-
* If the condition is defined as a promise, the flow will block on that
1548-
* promise's resolution, up to {@code timeout} milliseconds. If
1549-
* {@code timeout === 0}, the flow will block indefinitely on the promise's
1550-
* resolution.
1547+
* If the condition is defined as a promise, the flow will wait for it to
1548+
* settle. If the timeout expires before the promise settles, the promise
1549+
* returned by this function will be rejected.
1550+
*
1551+
* If this function is invoked with `timeout === 0`, or the timeout is omitted,
1552+
* the flow will wait indefinitely for the condition to be satisfied.
15511553
*
15521554
* @param {(!promise.Promise<T>|function())} condition The condition to poll,
15531555
* or a promise to wait on.
15541556
* @param {number=} opt_timeout How long to wait, in milliseconds, for the
1555-
* condition to hold before timing out; defaults to 0.
1557+
* condition to hold before timing out. If omitted, the flow will wait
1558+
* indefinitely.
15561559
* @param {string=} opt_message An optional error message to include if the
15571560
* wait times out; defaults to the empty string.
15581561
* @return {!promise.Promise<T>} A promise that will be fulfilled
@@ -1618,7 +1621,7 @@ promise.ControlFlow.prototype.wait = function(
16181621
var elapsed = goog.now() - startTime;
16191622
if (!!value) {
16201623
fulfill(value);
1621-
} else if (elapsed >= timeout) {
1624+
} else if (timeout && elapsed >= timeout) {
16221625
reject(new Error((opt_message ? opt_message + '\n' : '') +
16231626
'Wait timed out after ' + elapsed + 'ms'));
16241627
} else {

javascript/webdriver/test/promise_flow_test.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,44 @@ function testWaiting_pollingLoopWaitsForAllScheduledTasksInCondition() {
11141114
}
11151115

11161116

1117-
function testWaiting_timesOut_zeroTimeout() {
1118-
scheduleWait(function() { return false; }, 0, 'always false');
1119-
return waitForAbort().then(function(e) {
1120-
assertRegExp(/^always false\nWait timed out after \d+ms$/, e.message);
1117+
function testWaiting_waitsForeverOnAZeroTimeout() {
1118+
var done = false;
1119+
setTimeout(function() {
1120+
done = true;
1121+
}, 500);
1122+
var waitResult = scheduleWait(function() {
1123+
return done;
1124+
}, 0);
1125+
1126+
return timeout(250).then(function() {
1127+
assertFalse(done);
1128+
return timeout(300);
1129+
}).then(function() {
1130+
assertTrue(done);
1131+
return waitResult;
11211132
});
11221133
}
11231134

1135+
1136+
function testWaiting_waitsForeverIfTimeoutOmitted() {
1137+
var done = false;
1138+
setTimeout(function() {
1139+
done = true;
1140+
}, 500);
1141+
var waitResult = scheduleWait(function() {
1142+
return done;
1143+
});
1144+
1145+
return timeout(250).then(function() {
1146+
assertFalse(done);
1147+
return timeout(300);
1148+
}).then(function() {
1149+
assertTrue(done);
1150+
return waitResult;
1151+
});
1152+
}
1153+
1154+
11241155
function testWaiting_timesOut_nonZeroTimeout() {
11251156
var count = 0;
11261157
scheduleWait(function() {

0 commit comments

Comments
 (0)