Refactor awaitility (#2226)

* Awaitility refactor

* Refactor
This commit is contained in:
Grzegorz Piwowarek 2017-07-08 07:58:42 +02:00 committed by GitHub
parent 98633dce67
commit fdd26c7c52
2 changed files with 35 additions and 32 deletions

View File

@ -8,25 +8,23 @@ public class AsyncService {
private final int DELAY = 1000; private final int DELAY = 1000;
private final int INIT_DELAY = 2000; private final int INIT_DELAY = 2000;
private AtomicLong value = new AtomicLong(0); private final AtomicLong value = new AtomicLong(0);
private Executor executor = Executors.newFixedThreadPool(4); private final Executor executor = Executors.newFixedThreadPool(4);
private volatile boolean initialized = false; private volatile boolean initialized = false;
public void initialize() { void initialize() {
executor.execute(() -> { executor.execute(() -> {
sleep(INIT_DELAY); sleep(INIT_DELAY);
initialized = true; initialized = true;
}); });
} }
public boolean isInitialized() { boolean isInitialized() {
return initialized; return initialized;
} }
public void addValue(long val) { void addValue(long val) {
if (!isInitialized()) { throwIfNotInitialized();
throw new IllegalStateException("Service is not initialized");
}
executor.execute(() -> { executor.execute(() -> {
sleep(DELAY); sleep(DELAY);
value.addAndGet(val); value.addAndGet(val);
@ -34,9 +32,7 @@ public class AsyncService {
} }
public long getValue() { public long getValue() {
if (!isInitialized()) { throwIfNotInitialized();
throw new IllegalStateException("Service is not initialized");
}
return value.longValue(); return value.longValue();
} }
@ -47,4 +43,10 @@ public class AsyncService {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void throwIfNotInitialized() {
if (!initialized) {
throw new IllegalStateException("Service is not initialized");
}
}
} }

View File

@ -1,19 +1,20 @@
package com.baeldung.awaitility; 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.await;
import static org.awaitility.Awaitility.fieldIn; import static org.awaitility.Awaitility.fieldIn;
import static org.awaitility.Awaitility.given; import static org.awaitility.Awaitility.given;
import static org.awaitility.proxy.AwaitilityClassProxy.to; import static org.awaitility.proxy.AwaitilityClassProxy.to;
import static org.hamcrest.Matchers.equalTo; 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 { public class AsyncServiceUnitTest {
private AsyncService asyncService; private AsyncService asyncService;
@ -25,18 +26,18 @@ public class AsyncServiceUnitTest {
@Test @Test
public void givenAsyncService_whenInitialize_thenInitOccurs1() { public void givenAsyncService_whenInitialize_thenInitOccurs1() {
asyncService.initialize(); asyncService.initialize();
Callable<Boolean> isInitialized = () -> asyncService.isInitialized(); Callable<Boolean> isInitialized = asyncService::isInitialized;
await().until(isInitialized); await().until(isInitialized);
} }
@Test @Test
public void givenAsyncService_whenInitialize_thenInitOccurs2() { public void givenAsyncService_whenInitialize_thenInitOccurs2() {
asyncService.initialize(); asyncService.initialize();
Callable<Boolean> isInitialized = () -> asyncService.isInitialized(); Callable<Boolean> isInitialized = asyncService::isInitialized;
await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS) await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS)
.atMost(Duration.FIVE_SECONDS) .atMost(Duration.FIVE_SECONDS)
.with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS)
.until(isInitialized); .until(isInitialized);
} }
@Test @Test
@ -46,7 +47,7 @@ public class AsyncServiceUnitTest {
Awaitility.setDefaultTimeout(Duration.ONE_MINUTE); Awaitility.setDefaultTimeout(Duration.ONE_MINUTE);
asyncService.initialize(); asyncService.initialize();
await().until(() -> asyncService.isInitialized()); await().until(asyncService::isInitialized);
} }
@Test @Test
@ -59,14 +60,14 @@ public class AsyncServiceUnitTest {
public void givenAsyncService_whenInitialize_thenInitOccurs3() { public void givenAsyncService_whenInitialize_thenInitOccurs3() {
asyncService.initialize(); asyncService.initialize();
await().until(fieldIn(asyncService) await().until(fieldIn(asyncService)
.ofType(boolean.class) .ofType(boolean.class)
.andWithName("initialized"), equalTo(true)); .andWithName("initialized"), equalTo(true));
} }
@Test @Test
public void givenValue_whenAddValue_thenValueAdded() { public void givenValue_whenAddValue_thenValueAdded() {
asyncService.initialize(); asyncService.initialize();
await().until(() -> asyncService.isInitialized()); await().until(asyncService::isInitialized);
long value = 5; long value = 5;
asyncService.addValue(value); asyncService.addValue(value);
await().until(asyncService::getValue, equalTo(value)); await().until(asyncService::getValue, equalTo(value));
@ -76,9 +77,9 @@ public class AsyncServiceUnitTest {
public void givenAsyncService_whenGetValue_thenExceptionIgnored() { public void givenAsyncService_whenGetValue_thenExceptionIgnored() {
asyncService.initialize(); asyncService.initialize();
given().ignoreException(IllegalStateException.class) given().ignoreException(IllegalStateException.class)
.await() .await()
.atMost(Duration.FIVE_SECONDS) .atMost(Duration.FIVE_SECONDS)
.atLeast(Duration.FIVE_HUNDRED_MILLISECONDS) .atLeast(Duration.FIVE_HUNDRED_MILLISECONDS)
.until(asyncService::getValue, equalTo(0L)); .until(asyncService::getValue, equalTo(0L));
} }
} }