Skip to content

Commit

Permalink
fix: waitForCondition should not call predicate after it returned true (
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s authored Dec 19, 2024
1 parent c591a14 commit 42d0203
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@

class WaitableRace<T> implements Waitable<T> {
private final Collection<Waitable<T>> waitables;
private Waitable<T> firstReady;

WaitableRace(Collection<Waitable<T>> waitables) {
this.waitables = waitables;
}

@Override
public boolean isDone() {
for (Waitable w : waitables) {
if (firstReady != null) {
return true;
}
for (Waitable<T> w : waitables) {
if (w.isDone()) {
firstReady = w;
return true;
}
}
Expand All @@ -37,14 +42,11 @@ public boolean isDone() {

@Override
public T get() {
assert isDone();
dispose();
for (Waitable<T> w : waitables) {
if (w.isDone()) {
return w.get();
}
try {
return firstReady.get();
} finally {
dispose();
}
throw new IllegalStateException("At least one element must be ready");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ void waitForConditionPageClosed(BrowserContext context) {
assertTrue(e.getMessage().contains("Target page, context or browser has been closed"), e.getMessage());
}

@Test
void waitForConditionThatMayChangeToFalse(BrowserContext context) {
int[] var = {0};
context.waitForCondition(() -> ++var[0] == 1);
assertEquals(1, var[0], "The predicate should be called only once.");
}

@Test
void shouldPropagateCloseReasonToPendingActions(Browser browser) {
BrowserContext context = browser.newContext();
Expand Down

0 comments on commit 42d0203

Please sign in to comment.