Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 97d193e

Browse files
committed
Renamed Waiter to DefaultWaiter and extracted interface to fit better in the naming scheme for single implementation interfaces
1 parent 92f862c commit 97d193e

File tree

5 files changed

+16
-122
lines changed

5 files changed

+16
-122
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/ConfiguredWait.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818
@Getter(AccessLevel.PACKAGE)
1919
public class ConfiguredWait {
2020

21-
/**
22-
* The {@link Waiter} to use when executing wait operations.
23-
*/
21+
/** The {@link Waiter} to use when executing wait operations. */
2422
private final Waiter waiter;
25-
/**
26-
* The {@link WaitConfig} to use when deciding how long to wait.
27-
*/
23+
/** The {@link WaitConfig} to use when deciding how long to wait. */
2824
private final WaitConfig config;
2925

3026
/**

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/Wait.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
*/
2626
public final class Wait {
2727

28-
/** The default {@link Waiter} supplier. Generates a new {@link Waiter} for each call. */
29-
public static final Supplier<Waiter> DEFAULT_WAITER = Waiter::new;
28+
/** The default {@link Waiter} supplier. Generates a new {@link DefaultWaiter} for each call. */
29+
public static final Supplier<Waiter> DEFAULT_WAITER = DefaultWaiter::new;
3030

3131
/**
3232
* A supplier used to get a {@link Waiter} instance to use when executing any wait operations.

webtester-core/src/main/java/info/novatec/testit/webtester/waiting/WaitUntil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import info.novatec.testit.webtester.conditions.Condition;
77
import info.novatec.testit.webtester.conditions.Conditions;
8-
import info.novatec.testit.webtester.pagefragments.PageFragment;
98

109

1110
/**
@@ -22,7 +21,7 @@ public class WaitUntil<T> {
2221
private final T object;
2322

2423
/**
25-
* Creates a new {@link WaitUntil} instance for the given {@link PageFragment} and {@link WaitConfig}.
24+
* Creates a new {@link WaitUntil} instance for the given object and {@link WaitConfig}.
2625
*
2726
* @param config the configuration to use
2827
* @param object the object to use
Lines changed: 5 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,16 @@
11
package info.novatec.testit.webtester.waiting;
22

3-
import java.time.Clock;
43
import java.util.concurrent.TimeUnit;
54
import java.util.function.Supplier;
65

7-
import lombok.AccessLevel;
8-
import lombok.Getter;
9-
import lombok.extern.slf4j.Slf4j;
10-
116

127
/**
13-
* Provides different kinds of wait operations. The two basic types are 'wait exactly X amount of time' and 'wait until X
14-
* passes a certain condition check'.
8+
* Implementations of this interface provide different kinds of wait operations.
9+
* The two basic types are 'wait exactly X amount of time' and 'wait until X passes a certain condition check'.
1510
*
1611
* @since 2.0
1712
*/
18-
@Slf4j
19-
@Getter(AccessLevel.PROTECTED)
20-
public class Waiter {
21-
22-
/**
23-
* The {@link Sleeper} to use when actually needing to pass time.
24-
*/
25-
private final Sleeper sleeper;
26-
/**
27-
* The {@link Clock} to use for tracking the passing of time.
28-
*/
29-
private final Clock clock;
30-
31-
/**
32-
* Creates a new default {@link Waiter}. It has a {@link CurrentThreadSleeper} as a sleeper and a {@link
33-
* Clock#systemDefaultZone()} clock.
34-
*
35-
* @see Sleeper
36-
* @see Clock
37-
* @since 2.0
38-
*/
39-
public Waiter() {
40-
this(new CurrentThreadSleeper());
41-
}
42-
43-
/**
44-
* Creates a new custom {@link Waiter}. It will use the given {@link Sleeper} and a {@link Clock#systemDefaultZone()}
45-
* clock.
46-
*
47-
* @see Sleeper
48-
* @see Clock
49-
* @since 2.0
50-
*/
51-
public Waiter(Sleeper sleeper) {
52-
this(sleeper, Clock.systemDefaultZone());
53-
}
54-
55-
/**
56-
* Creates a new custom {@link Waiter}. It will use the given {@link Sleeper} and a {@link Clock}.
57-
*
58-
* @see Sleeper
59-
* @see Clock
60-
* @since 2.0
61-
*/
62-
public Waiter(Sleeper sleeper, Clock clock) {
63-
this.sleeper = sleeper;
64-
this.clock = clock;
65-
}
13+
public interface Waiter {
6614

6715
/**
6816
* Waits the given amount of time using the {@link TimeUnit time unit}.
@@ -74,16 +22,7 @@ public Waiter(Sleeper sleeper, Clock clock) {
7422
* @param timeUnit the time unit to use
7523
* @since 2.0
7624
*/
77-
public void waitExactly(long duration, TimeUnit timeUnit) {
78-
try {
79-
long millisToSleep = timeUnit.toMillis(duration);
80-
if (millisToSleep > 0) {
81-
sleeper.sleep(millisToSleep);
82-
}
83-
} catch (InterruptionException e) {
84-
log.debug("wait was interrupted", e);
85-
}
86-
}
25+
void waitExactly(long duration, TimeUnit timeUnit);
8726

8827
/**
8928
* Waits until the given boolean {@link Supplier} returns <code>true</code> or the {@link WaitConfig configured} timeout
@@ -94,46 +33,6 @@ public void waitExactly(long duration, TimeUnit timeUnit) {
9433
* @see WaitConfig
9534
* @since 2.0
9635
*/
97-
@SuppressWarnings("PMD.AvoidCatchingGenericException")
98-
public void waitUntil(WaitConfig config, Supplier<Boolean> condition) {
99-
100-
long effectiveTimeout = config.getTimeoutInMillis();
101-
long start = now();
102-
103-
boolean conditionMet = false;
104-
RuntimeException lastException = null;
105-
106-
do {
107-
try {
108-
conditionMet = condition.get();
109-
} catch (RuntimeException e) {
110-
lastException = e;
111-
}
112-
log.trace("condition '{}' met: {}", condition, conditionMet);
113-
if (!conditionMet) {
114-
waitExactly(config.getInterval(), TimeUnit.MILLISECONDS);
115-
}
116-
} while (!conditionMet && timeSince(start) < effectiveTimeout);
117-
118-
if (!conditionMet) {
119-
String message = "condition not met within the given timeout";
120-
log.debug("condition not met: {}", condition);
121-
if (lastException != null) {
122-
throw new TimeoutException(message, lastException);
123-
}
124-
throw new TimeoutException(message);
125-
} else {
126-
log.debug("condition met: {}", condition);
127-
}
128-
129-
}
130-
131-
private long timeSince(long start) {
132-
return now() - start;
133-
}
134-
135-
private long now() {
136-
return clock.millis();
137-
}
36+
void waitUntil(WaitConfig config, Supplier<Boolean> condition);
13837

13938
}

webtester-core/src/test/java/info/novatec/testit/webtester/waiting/WaiterTest.java renamed to webtester-core/src/test/java/info/novatec/testit/webtester/waiting/DefaultWaiterTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424

2525

2626
@RunWith(Enclosed.class)
27-
public class WaiterTest {
27+
public class DefaultWaiterTest {
2828

2929
public static class Construction {
3030

3131
@Test
3232
public void defaultWaiterCanBeInitialized() {
33-
Waiter waiter = new Waiter();
33+
DefaultWaiter waiter = new DefaultWaiter();
3434
assertThat(waiter.getSleeper()).isInstanceOf(CurrentThreadSleeper.class);
3535
assertThat(waiter.getClock()).isNotNull();
3636
}
3737

3838
@Test
3939
public void sleeperCanBeCustomized() {
4040
Sleeper sleeper = mock(Sleeper.class);
41-
Waiter waiter = new Waiter(sleeper);
41+
DefaultWaiter waiter = new DefaultWaiter(sleeper);
4242
assertThat(waiter.getSleeper()).isSameAs(sleeper);
4343
assertThat(waiter.getClock()).isNotNull();
4444
}
@@ -47,7 +47,7 @@ public void sleeperCanBeCustomized() {
4747
public void sleeperAndClockCanBeCustomized() {
4848
Sleeper sleeper = mock(Sleeper.class);
4949
Clock clock = mock(Clock.class);
50-
Waiter waiter = new Waiter(sleeper, clock);
50+
DefaultWaiter waiter = new DefaultWaiter(sleeper, clock);
5151
assertThat(waiter.getSleeper()).isSameAs(sleeper);
5252
assertThat(waiter.getClock()).isSameAs(clock);
5353
}
@@ -60,7 +60,7 @@ public static class WaitExactly {
6060
@Mock
6161
Sleeper sleeper;
6262
@InjectMocks
63-
Waiter cut;
63+
DefaultWaiter cut;
6464

6565
@Test
6666
public void waitingOneSecondsSleepsForOneThousandMilliseconds() {
@@ -105,7 +105,7 @@ public static class WaitUntil {
105105
@Mock
106106
Clock clock;
107107
@InjectMocks
108-
Waiter cut;
108+
DefaultWaiter cut;
109109

110110
@Mock
111111
Supplier<Boolean> condition;

0 commit comments

Comments
 (0)