Skip to content

Commit fdd26c7

Browse files
authored
Refactor awaitility (eugenp#2226)
* Awaitility refactor * Refactor
1 parent 98633dc commit fdd26c7

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

‎libraries/src/main/java/com/baeldung/awaitility/AsyncService.java‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,31 @@ public class AsyncService {
88
private final int DELAY = 1000;
99
private final int INIT_DELAY = 2000;
1010

11-
private AtomicLong value = new AtomicLong(0);
12-
private Executor executor = Executors.newFixedThreadPool(4);
11+
private final AtomicLong value = new AtomicLong(0);
12+
private final Executor executor = Executors.newFixedThreadPool(4);
1313
private volatile boolean initialized = false;
1414

15-
public void initialize() {
15+
void initialize() {
1616
executor.execute(() -> {
1717
sleep(INIT_DELAY);
1818
initialized = true;
1919
});
2020
}
2121

22-
public boolean isInitialized() {
22+
boolean isInitialized() {
2323
return initialized;
2424
}
2525

26-
public void addValue(long val) {
27-
if (!isInitialized()) {
28-
throw new IllegalStateException("Service is not initialized");
29-
}
26+
void addValue(long val) {
27+
throwIfNotInitialized();
3028
executor.execute(() -> {
3129
sleep(DELAY);
3230
value.addAndGet(val);
3331
});
3432
}
3533

3634
public long getValue() {
37-
if (!isInitialized()) {
38-
throw new IllegalStateException("Service is not initialized");
39-
}
35+
throwIfNotInitialized();
4036
return value.longValue();
4137
}
4238

@@ -47,4 +43,10 @@ private void sleep(int delay) {
4743
e.printStackTrace();
4844
}
4945
}
46+
47+
private void throwIfNotInitialized() {
48+
if (!initialized) {
49+
throw new IllegalStateException("Service is not initialized");
50+
}
51+
}
5052
}
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.baeldung.awaitility;
22

3+
import org.awaitility.Awaitility;
4+
import org.awaitility.Duration;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.concurrent.Callable;
10+
import java.util.concurrent.TimeUnit;
11+
312
import static org.awaitility.Awaitility.await;
413
import static org.awaitility.Awaitility.fieldIn;
514
import static org.awaitility.Awaitility.given;
615
import static org.awaitility.proxy.AwaitilityClassProxy.to;
716
import static org.hamcrest.Matchers.equalTo;
817

9-
import java.util.concurrent.Callable;
10-
import java.util.concurrent.TimeUnit;
11-
12-
import org.awaitility.Awaitility;
13-
import org.awaitility.Duration;
14-
import org.junit.Before;
15-
import org.junit.Test;
16-
1718
public class AsyncServiceUnitTest {
1819
private AsyncService asyncService;
1920

@@ -25,18 +26,18 @@ public void setUp() {
2526
@Test
2627
public void givenAsyncService_whenInitialize_thenInitOccurs1() {
2728
asyncService.initialize();
28-
Callable<Boolean> isInitialized = () -> asyncService.isInitialized();
29+
Callable<Boolean> isInitialized = asyncService::isInitialized;
2930
await().until(isInitialized);
3031
}
3132

3233
@Test
3334
public void givenAsyncService_whenInitialize_thenInitOccurs2() {
3435
asyncService.initialize();
35-
Callable<Boolean> isInitialized = () -> asyncService.isInitialized();
36+
Callable<Boolean> isInitialized = asyncService::isInitialized;
3637
await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS)
37-
.atMost(Duration.FIVE_SECONDS)
38-
.with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS)
39-
.until(isInitialized);
38+
.atMost(Duration.FIVE_SECONDS)
39+
.with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS)
40+
.until(isInitialized);
4041
}
4142

4243
@Test
@@ -46,7 +47,7 @@ public void givenAsyncService_whenInitialize_thenInitOccurs_withDefualts() {
4647
Awaitility.setDefaultTimeout(Duration.ONE_MINUTE);
4748

4849
asyncService.initialize();
49-
await().until(() -> asyncService.isInitialized());
50+
await().until(asyncService::isInitialized);
5051
}
5152

5253
@Test
@@ -59,14 +60,14 @@ public void givenAsyncService_whenInitialize_thenInitOccurs_withProxy() {
5960
public void givenAsyncService_whenInitialize_thenInitOccurs3() {
6061
asyncService.initialize();
6162
await().until(fieldIn(asyncService)
62-
.ofType(boolean.class)
63-
.andWithName("initialized"), equalTo(true));
63+
.ofType(boolean.class)
64+
.andWithName("initialized"), equalTo(true));
6465
}
6566

6667
@Test
6768
public void givenValue_whenAddValue_thenValueAdded() {
6869
asyncService.initialize();
69-
await().until(() -> asyncService.isInitialized());
70+
await().until(asyncService::isInitialized);
7071
long value = 5;
7172
asyncService.addValue(value);
7273
await().until(asyncService::getValue, equalTo(value));
@@ -76,9 +77,9 @@ public void givenValue_whenAddValue_thenValueAdded() {
7677
public void givenAsyncService_whenGetValue_thenExceptionIgnored() {
7778
asyncService.initialize();
7879
given().ignoreException(IllegalStateException.class)
79-
.await()
80-
.atMost(Duration.FIVE_SECONDS)
81-
.atLeast(Duration.FIVE_HUNDRED_MILLISECONDS)
82-
.until(asyncService::getValue, equalTo(0L));
80+
.await()
81+
.atMost(Duration.FIVE_SECONDS)
82+
.atLeast(Duration.FIVE_HUNDRED_MILLISECONDS)
83+
.until(asyncService::getValue, equalTo(0L));
8384
}
8485
}

0 commit comments

Comments
 (0)