Skip to content

Commit

Permalink
[Bug #314] Update tests for ThrottledFuture#then accepting the comple…
Browse files Browse the repository at this point in the history
…ted future
  • Loading branch information
lukaskabc authored and ledsoft committed Nov 20, 2024
1 parent 1462c05 commit 3f938ec
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ void getNowReturnsEmptyWhenCacheIsNull() {

@Test
void thenActionIsExecutedSynchronouslyWhenFutureIsAlreadyDoneAndNotCanceled() {
final Object result = new Object();
final ThrottledFuture<?> future = ThrottledFuture.of(() -> result);
final ThrottledFuture<?> future = ThrottledFuture.of(() -> null);
final AtomicBoolean completed = new AtomicBoolean(false);
final AtomicReference<Object> futureResult = new AtomicReference<>(null);
future.run(null);
Expand All @@ -97,25 +96,24 @@ void thenActionIsExecutedSynchronouslyWhenFutureIsAlreadyDoneAndNotCanceled() {
futureResult.set(fResult);
});
assertTrue(completed.get());
assertEquals(result, futureResult.get());
assertEquals(future, futureResult.get());
}

@Test
void thenActionIsNotExecutedWhenFutureIsAlreadyCancelled() {
void thenActionIsExecutedWhenFutureIsAlreadyCancelled() {
final ThrottledFuture<?> future = ThrottledFuture.of(Object::new);
final AtomicBoolean completed = new AtomicBoolean(false);
future.cancel(false);
assertTrue(future.isCancelled());
future.then(result -> completed.set(true));
assertFalse(completed.get());
assertTrue(completed.get());
}

@Test
void thenActionIsExecutedOnceFutureIsRun() {
final Object result = new Object();
final AtomicBoolean completed = new AtomicBoolean(false);
final AtomicReference<Object> fResult = new AtomicReference<>(null);
final ThrottledFuture<?> future = ThrottledFuture.of(() -> result);
final ThrottledFuture<?> future = ThrottledFuture.of(() -> null);
future.then(futureResult -> {
completed.set(true);
fResult.set(futureResult);
Expand All @@ -124,30 +122,30 @@ void thenActionIsExecutedOnceFutureIsRun() {
assertFalse(completed.get()); // action was not executed yet
future.run(null);
assertTrue(completed.get());
assertEquals(result, fResult.get());
assertEquals(future, fResult.get());
}

@Test
void thenActionIsNotExecutedOnceFutureIsCancelled() {
void thenActionIsExecutedOnceFutureIsCancelled() {
final Object result = new Object();
final AtomicBoolean completed = new AtomicBoolean(false);
final ThrottledFuture<?> future = ThrottledFuture.of(() -> result);
future.then(futureResult -> completed.set(true));
assertFalse(completed.get()); // action was not executed yet
future.cancel(false);
assertFalse(completed.get());
assertTrue(completed.get());
}

@Test
void thenActionIsNotExecutedWhenFutureCompletedExceptionally() {
void thenActionIsExecutedWhenFutureCompletedExceptionally() {
final AtomicBoolean completed = new AtomicBoolean(false);
final ThrottledFuture<?> future = ThrottledFuture.of(() -> {
throw new RuntimeException();
});
future.run(null);
assertFalse(completed.get());
future.then(futureResult -> completed.set(true));
assertFalse(completed.get());
assertTrue(completed.get());
}

@Test
Expand Down

0 comments on commit 3f938ec

Please sign in to comment.