Skip to content

Commit

Permalink
more lambdarising
Browse files Browse the repository at this point in the history
  • Loading branch information
toby-weston-db committed Jan 22, 2014
1 parent 2beb984 commit cb9be20
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.TimeoutException;

import static com.google.code.tempusfugit.concurrency.ThreadUtils.resetInterruptFlagWhen;
import static com.google.code.tempusfugit.condition.Conditions.*;
import static com.google.code.tempusfugit.temporal.WaitFor.waitOrTimeout;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

Expand All @@ -39,6 +40,7 @@ public static ExecutorServiceShutdown shutdown(ExecutorService executor) {
return new ExecutorServiceShutdown(executor);
}

/* @return {@code true} if the executor terminated and {@code false} if the timeout elapsed before termination */
public Boolean waitingForCompletion(final Duration duration) {
if (executor == null)
return false;
Expand All @@ -56,11 +58,7 @@ public Boolean waitingForShutdown(Timeout timeout) throws TimeoutException, Inte
}

private Interruptible<Boolean> awaitingTerminationIsInterrupted(final Duration timeout) {
return new Interruptible<Boolean>(){
public Boolean call() throws InterruptedException {
return executor.awaitTermination(timeout.inMillis(), MILLISECONDS);
}
};
return () -> executor.awaitTermination(timeout.inMillis(), MILLISECONDS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@

public final class ThreadUtils {

public static void sleep(final Duration duration) {
public static void sleep(Duration duration) {
resetInterruptFlagWhen(sleepingIsInterrupted(duration));
}

private static Interruptible<Void> sleepingIsInterrupted(final Duration duration) {
return new Interruptible<Void>() {
public Void call() throws InterruptedException {
Thread.sleep(duration.inMillis());
return null;
}
private static Interruptible<Void> sleepingIsInterrupted(Duration duration) {
return () -> {
Thread.sleep(duration.inMillis());
return null;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public static Condition isAlive(Thread thread) {
}

public static Condition isWaiting(Thread thread) {
return () -> {
return (thread.getState() == TIMED_WAITING) || (thread.getState() == WAITING);
};
return () -> (thread.getState() == TIMED_WAITING) || (thread.getState() == WAITING);
}

public static Condition is(Thread thread, State state) {
Expand All @@ -71,7 +69,7 @@ public static void assertThat(String message, Condition condition, Matcher<Boole
*/
@Deprecated
public static <T> Condition assertion(T actual, Matcher<T> matcher) {
return new MatcherCondition<T>(actual, matcher);
return new MatcherCondition<>(actual, matcher);
}

public static <T> SelfDescribingCondition assertion(Callable<T, RuntimeException> actual, Matcher<T> matcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ private void logCurrentThread() throws TimeoutException, InterruptedException {
}

private void waitToForceCachedThreadPoolToCreateNewThread() throws InterruptedException, TimeoutException {
waitOrTimeout(new Condition() {
public boolean isSatisfied() {
return THREADS.size() == getConcurrentCount();
}
}, timeout(seconds(1)));
waitOrTimeout(() -> THREADS.size() == getConcurrentCount(), timeout(seconds(1)));
}

@Test(expected = AssertionFailedError.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ private Condition forDeadlockDetected() {
}

private Condition forDeadlockDetected(final int stackDepth) {
return new Condition() {
@Override
public boolean isSatisfied() {
DeadlockDetector.printDeadlocks(deadlocks, stackDepth);
return deadlocks.detected();
}
return () -> {
DeadlockDetector.printDeadlocks(deadlocks, stackDepth);
return deadlocks.detected();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,18 @@ public void noInterruptOccursIfCompletionServiceFinishes() throws Exception {
@Test
public void incompleteTasksAreInterrupted() throws Exception {
final AtomicBoolean interrupted = new AtomicBoolean(false);
Callable<Void> callable = new Callable<Void>() {
public Void call() throws Exception {
while (!Thread.currentThread().isInterrupted())
Thread.yield();
interrupted.set(true);
return null;
}
Callable<Void> callable = () -> {
while (!Thread.currentThread().isInterrupted())
Thread.yield();
interrupted.set(true);
return null;
};

try {
new DefaultTimeoutableCompletionService(new ExecutorCompletionService(newSingleThreadExecutor()), millis(1), new RealClock()).submit(asList(callable));
fail("didn't timeout");
} catch (TimeoutException e) {
waitOrTimeout(new Condition() {
public boolean isSatisfied() {
return interrupted.get();
}
}, timeout(seconds(10)));
waitOrTimeout(interrupted::get, timeout(seconds(10)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ public void shouldNotBeAbleToSetStopwatchAfterStopwatchHasStarted() {
private void assertInterruptedWithin(Duration duration) throws TimeoutException {
assertDurationIsAlotBiggerThanWaitForSleepPeriod(duration);
try {
waitOrTimeout(new Condition() {
public boolean isSatisfied() {
return interrupted;
}
}, timeout(duration));
waitOrTimeout(() -> interrupted, timeout(duration));
} catch (InterruptedException e) {
fail();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ public void release() {
}

private static Interruptible<Void> locking(final Lock lock) {
return new Interruptible<Void>() {
@Override
public Void call() throws InterruptedException {
lock.lockInterruptibly();
return null;
}
return () -> {
lock.lockInterruptibly();
return null;
};
}

Expand All @@ -73,11 +70,9 @@ void countdownAndAwait(CountDownLatch latch) {
}

private Interruptible<Void> waitingFor(final CountDownLatch latch) {
return new Interruptible<Void>() {
public Void call() throws InterruptedException {
latch.await();
return null;
}
return () -> {
latch.await();
return null;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public void timeoutBehaviourThrowsException() throws InterruptedException {

@Test (expected = InterruptedException.class, timeout = 500)
public void waitForCanBeInterrupted() throws TimeoutException, InterruptedException {
waitOrTimeout(InterruptWaitFor(), timeout(seconds(10)));
waitOrTimeout(() -> {
currentThread().interrupt();
return false;
}, timeout(seconds(10)));
}

@Test (timeout = 500)
Expand Down Expand Up @@ -142,15 +145,6 @@ private void waitForShutdown(final Thread thread) throws TimeoutException, Inter
waitOrTimeout(not(isAlive(thread)), timeout(seconds(1)));
}

private Condition InterruptWaitFor() {
return new Condition() {
public boolean isSatisfied() {
currentThread().interrupt();
return false;
}
};
}

private class ForceTimeout implements SelfDescribingCondition {
private final Duration timeout;

Expand Down

0 comments on commit cb9be20

Please sign in to comment.