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
@@ -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");
}
}
}