parent
98633dce67
commit
fdd26c7c52
|
@ -8,25 +8,23 @@ public class AsyncService {
|
|||
private final int DELAY = 1000;
|
||||
private final int INIT_DELAY = 2000;
|
||||
|
||||
private AtomicLong value = new AtomicLong(0);
|
||||
private Executor executor = Executors.newFixedThreadPool(4);
|
||||
private final AtomicLong value = new AtomicLong(0);
|
||||
private final Executor executor = Executors.newFixedThreadPool(4);
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
public void initialize() {
|
||||
void initialize() {
|
||||
executor.execute(() -> {
|
||||
sleep(INIT_DELAY);
|
||||
initialized = true;
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
boolean isInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public void addValue(long val) {
|
||||
if (!isInitialized()) {
|
||||
throw new IllegalStateException("Service is not initialized");
|
||||
}
|
||||
void addValue(long val) {
|
||||
throwIfNotInitialized();
|
||||
executor.execute(() -> {
|
||||
sleep(DELAY);
|
||||
value.addAndGet(val);
|
||||
|
@ -34,9 +32,7 @@ public class AsyncService {
|
|||
}
|
||||
|
||||
public long getValue() {
|
||||
if (!isInitialized()) {
|
||||
throw new IllegalStateException("Service is not initialized");
|
||||
}
|
||||
throwIfNotInitialized();
|
||||
return value.longValue();
|
||||
}
|
||||
|
||||
|
@ -47,4 +43,10 @@ public class AsyncService {
|
|||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void throwIfNotInitialized() {
|
||||
if (!initialized) {
|
||||
throw new IllegalStateException("Service is not initialized");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,20 @@
|
|||
package com.baeldung.awaitility;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Duration;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.awaitility.Awaitility.fieldIn;
|
||||
import static org.awaitility.Awaitility.given;
|
||||
import static org.awaitility.proxy.AwaitilityClassProxy.to;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Duration;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AsyncServiceUnitTest {
|
||||
private AsyncService asyncService;
|
||||
|
||||
|
@ -25,18 +26,18 @@ public class AsyncServiceUnitTest {
|
|||
@Test
|
||||
public void givenAsyncService_whenInitialize_thenInitOccurs1() {
|
||||
asyncService.initialize();
|
||||
Callable<Boolean> isInitialized = () -> asyncService.isInitialized();
|
||||
Callable<Boolean> isInitialized = asyncService::isInitialized;
|
||||
await().until(isInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAsyncService_whenInitialize_thenInitOccurs2() {
|
||||
asyncService.initialize();
|
||||
Callable<Boolean> isInitialized = () -> asyncService.isInitialized();
|
||||
Callable<Boolean> isInitialized = asyncService::isInitialized;
|
||||
await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS)
|
||||
.atMost(Duration.FIVE_SECONDS)
|
||||
.with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS)
|
||||
.until(isInitialized);
|
||||
.atMost(Duration.FIVE_SECONDS)
|
||||
.with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS)
|
||||
.until(isInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -46,7 +47,7 @@ public class AsyncServiceUnitTest {
|
|||
Awaitility.setDefaultTimeout(Duration.ONE_MINUTE);
|
||||
|
||||
asyncService.initialize();
|
||||
await().until(() -> asyncService.isInitialized());
|
||||
await().until(asyncService::isInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -59,14 +60,14 @@ public class AsyncServiceUnitTest {
|
|||
public void givenAsyncService_whenInitialize_thenInitOccurs3() {
|
||||
asyncService.initialize();
|
||||
await().until(fieldIn(asyncService)
|
||||
.ofType(boolean.class)
|
||||
.andWithName("initialized"), equalTo(true));
|
||||
.ofType(boolean.class)
|
||||
.andWithName("initialized"), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValue_whenAddValue_thenValueAdded() {
|
||||
asyncService.initialize();
|
||||
await().until(() -> asyncService.isInitialized());
|
||||
await().until(asyncService::isInitialized);
|
||||
long value = 5;
|
||||
asyncService.addValue(value);
|
||||
await().until(asyncService::getValue, equalTo(value));
|
||||
|
@ -76,9 +77,9 @@ public class AsyncServiceUnitTest {
|
|||
public void givenAsyncService_whenGetValue_thenExceptionIgnored() {
|
||||
asyncService.initialize();
|
||||
given().ignoreException(IllegalStateException.class)
|
||||
.await()
|
||||
.atMost(Duration.FIVE_SECONDS)
|
||||
.atLeast(Duration.FIVE_HUNDRED_MILLISECONDS)
|
||||
.until(asyncService::getValue, equalTo(0L));
|
||||
.await()
|
||||
.atMost(Duration.FIVE_SECONDS)
|
||||
.atLeast(Duration.FIVE_HUNDRED_MILLISECONDS)
|
||||
.until(asyncService::getValue, equalTo(0L));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue