Skip to content

Commit f44cf97

Browse files
committed
Misc test improvements
- Change some tests to use ExecutorService instead of ScheduledExecutorService for scheduling - Eagerly shutdown threadpools after test cases
1 parent da3d4d0 commit f44cf97

14 files changed

+76
-39
lines changed

src/test/java/net/jodah/failsafe/AbstractFailsafeTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727

2828
import java.lang.reflect.Method;
2929
import java.time.Duration;
30-
import java.util.concurrent.ScheduledExecutorService;
30+
import java.util.concurrent.ExecutorService;
3131
import java.util.concurrent.TimeUnit;
3232
import java.util.concurrent.atomic.AtomicInteger;
3333

3434
import static net.jodah.failsafe.Asserts.assertThrows;
35-
import static net.jodah.failsafe.Testing.*;
35+
import static net.jodah.failsafe.Testing.failures;
36+
import static net.jodah.failsafe.Testing.unwrapExceptions;
3637
import static org.mockito.Mockito.*;
3738
import static org.testng.Assert.*;
3839

@@ -48,7 +49,7 @@ public abstract class AbstractFailsafeTest {
4849
public interface FastService extends Service {
4950
}
5051

51-
abstract ScheduledExecutorService getExecutor();
52+
abstract ExecutorService getExecutor();
5253

5354
@BeforeMethod
5455
void beforeMethod(Method method) {
@@ -89,7 +90,7 @@ <T> T get(FailsafeExecutor<T> failsafe, ContextualSupplier<T> supplier) {
8990
*/
9091
<T> T failsafeGetWithFallback(Policy<T> policy, CheckedFunction<ExecutionAttemptedEvent<? extends T>, T> fallback,
9192
CheckedSupplier<T> supplier) {
92-
ScheduledExecutorService executor = getExecutor();
93+
ExecutorService executor = getExecutor();
9394
return unwrapExceptions(() -> executor == null ?
9495
Failsafe.with(Fallback.of(fallback), policy).get(supplier) :
9596
Failsafe.with(Fallback.ofAsync(fallback), policy).with(executor).getAsync(supplier).get());
@@ -99,7 +100,7 @@ <T> T failsafeGetWithFallback(Policy<T> policy, CheckedFunction<ExecutionAttempt
99100
* Does a failsafe run with an optional executor.
100101
*/
101102
void failsafeRun(Policy<?> policy, CheckedRunnable runnable) {
102-
ScheduledExecutorService executor = getExecutor();
103+
ExecutorService executor = getExecutor();
103104
if (executor == null)
104105
Failsafe.with(policy).run(runnable);
105106
else

src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import net.jodah.failsafe.Testing.ConnectException;
1919
import net.jodah.failsafe.Testing.Service;
2020
import net.jodah.failsafe.function.*;
21+
import net.jodah.failsafe.util.concurrent.Scheduler;
22+
import org.testng.annotations.AfterClass;
2123
import org.testng.annotations.BeforeMethod;
2224
import org.testng.annotations.Test;
2325

@@ -38,7 +40,7 @@
3840

3941
@Test
4042
public class AsyncFailsafeTest extends AbstractFailsafeTest {
41-
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
43+
private ExecutorService executor = Executors.newFixedThreadPool(5);
4244

4345
// Results from a getAsync against a future that wraps an asynchronous Failsafe call
4446
private @SuppressWarnings("unchecked") Class<? extends Throwable>[] futureAsyncThrowables = new Class[] {
@@ -50,8 +52,13 @@ protected void beforeMethod() {
5052
counter = new AtomicInteger();
5153
}
5254

55+
@AfterClass
56+
protected void afterClass() {
57+
executor.shutdownNow();
58+
}
59+
5360
@Override
54-
ScheduledExecutorService getExecutor() {
61+
ExecutorService getExecutor() {
5562
return executor;
5663
}
5764

@@ -476,13 +483,14 @@ public void shouldHandleCompletedExceptionallyGetStageAsync() {
476483
* Asserts that asynchronous completion via an execution is supported.
477484
*/
478485
public void shouldCompleteAsync() throws Throwable {
479-
Failsafe.with(retryAlways).runAsyncExecution(exec -> executor.schedule(() -> {
486+
Failsafe.with(retryAlways).runAsyncExecution(exec -> Scheduler.DEFAULT.schedule(() -> {
480487
try {
481488
exec.complete();
482489
waiter.resume();
483490
} catch (Exception e) {
484491
waiter.fail(e);
485492
}
493+
return null;
486494
}, 100, TimeUnit.MILLISECONDS));
487495

488496
waiter.await(5000);
@@ -573,12 +581,13 @@ public void shouldInterruptExecutionOnCancelWithForkJoinPool() throws Throwable
573581
}
574582

575583
public void shouldInterruptExecutionOnCancelWithScheduledExecutorService() throws Throwable {
576-
assertInterruptedExceptionOnCancel(Failsafe.with(retryAlways).with(executor));
584+
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
585+
assertInterruptedExceptionOnCancel(Failsafe.with(retryAlways).with(executorService));
586+
executorService.shutdownNow();
577587
}
578588

579589
public void shouldInterruptExecutionOnCancelWithExecutorService() throws Throwable {
580-
ExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
581-
assertInterruptedExceptionOnCancel(Failsafe.with(retryAlways).with(executorService));
590+
assertInterruptedExceptionOnCancel(Failsafe.with(retryAlways).with(executor));
582591
}
583592

584593
@SuppressWarnings("unused")

src/test/java/net/jodah/failsafe/FailsafeFutureTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@
1616
package net.jodah.failsafe;
1717

1818
import net.jodah.concurrentunit.Waiter;
19+
import org.testng.annotations.AfterClass;
1920
import org.testng.annotations.Test;
2021

21-
import java.util.concurrent.*;
22+
import java.util.concurrent.CancellationException;
23+
import java.util.concurrent.CompletableFuture;
24+
import java.util.concurrent.ExecutorService;
25+
import java.util.concurrent.Executors;
2226

2327
import static org.testng.Assert.*;
2428

2529
@Test
2630
public class FailsafeFutureTest {
27-
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
31+
ExecutorService executor = Executors.newFixedThreadPool(2);
32+
33+
@AfterClass
34+
protected void afterClass() {
35+
executor.shutdownNow();
36+
}
2837

2938
/**
3039
* Asserts that retries are stopped and completion handlers are called on cancel.
@@ -77,13 +86,13 @@ public void shouldNotCancelCompletedFuture() throws Throwable {
7786
* Asserts that a cancelled future ignores subsequent completion attempts.
7887
*/
7988
public void shouldNotCompleteCancelledFuture() {
80-
CompletableFuture<String> future = Failsafe.with(new RetryPolicy<String>()).with(executor).getAsync(() -> {
89+
CompletableFuture<String> future = Failsafe.with(new RetryPolicy<>()).with(executor).getAsync(() -> {
8190
Thread.sleep(1000);
8291
throw new IllegalStateException();
8392
});
8493

8594
future.cancel(true);
86-
future.complete("unexpected2");
95+
future.complete("unexpected");
8796
Asserts.assertThrows(future::get, CancellationException.class);
8897
}
8998
}

src/test/java/net/jodah/failsafe/functional/ShutdownExecutorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected void beforeMethod() {
3030
*/
3131
public void shouldHandleInitialSchedulingFailure() {
3232
// Given
33-
ScheduledExecutorService executor = Executors.newScheduledThreadPool(0);
33+
ExecutorService executor = Executors.newFixedThreadPool(1);
3434
executor.shutdownNow();
3535

3636
// When
@@ -46,7 +46,7 @@ public void shouldHandleInitialSchedulingFailure() {
4646
*/
4747
public void shouldHandleShutdown() throws Throwable {
4848
// Given
49-
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
49+
ExecutorService executor = Executors.newSingleThreadExecutor();
5050
AtomicInteger counter = new AtomicInteger();
5151

5252
// When
@@ -70,7 +70,7 @@ public void shouldHandleShutdown() throws Throwable {
7070
*/
7171
public void shouldHandleShutdownNow() throws Throwable {
7272
// Given
73-
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
73+
ExecutorService executor = Executors.newSingleThreadExecutor();
7474
AtomicInteger counter = new AtomicInteger();
7575

7676
// When
@@ -91,7 +91,7 @@ public void shouldHandleShutdownNow() throws Throwable {
9191
*/
9292
public void testShutdownDoesNotPreventTimeoutSync() throws Throwable {
9393
// Given
94-
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
94+
ExecutorService executor = Executors.newSingleThreadExecutor();
9595
Timeout<Object> timeout = Timeout.of(Duration.ofMillis(200)).withInterrupt(true);
9696
AtomicInteger counter = new AtomicInteger();
9797

@@ -113,7 +113,7 @@ public void testShutdownDoesNotPreventTimeoutSync() throws Throwable {
113113
*/
114114
public void testShutdownDoesNotPreventTimeoutAsync() throws Throwable {
115115
// Given
116-
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
116+
ExecutorService executor = Executors.newSingleThreadExecutor();
117117
Timeout<Object> timeout = Timeout.of(Duration.ofMillis(200)).withInterrupt(true);
118118
AtomicInteger counter = new AtomicInteger();
119119

src/test/java/net/jodah/failsafe/issues/Issue131Test.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import java.io.IOException;
2626
import java.util.concurrent.CompletableFuture;
2727
import java.util.concurrent.Executors;
28+
import java.util.concurrent.ScheduledExecutorService;
2829
import java.util.function.Predicate;
2930

3031
@Test
3132
public class Issue131Test {
32-
3333
/**
3434
* This predicate is invoked in failure scenarios with an arg of null,
3535
* producing a {@link NullPointerException} yielding surprising results.
@@ -60,7 +60,8 @@ public void syncShouldThrowTheUnderlyingIOException() {
6060
*/
6161
public void asyncShouldCompleteTheFuture() throws Throwable {
6262
CircuitBreaker<String> circuitBreaker = new CircuitBreaker<String>().handleResultIf(handleIfEqualsIgnoreCaseFoo);
63-
FailsafeExecutor<String> failsafe = Failsafe.with(circuitBreaker).with(Executors.newSingleThreadScheduledExecutor());
63+
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
64+
FailsafeExecutor<String> failsafe = Failsafe.with(circuitBreaker).with(executor);
6465

6566
Waiter waiter = new Waiter();
6667

@@ -73,5 +74,6 @@ public void asyncShouldCompleteTheFuture() throws Throwable {
7374
.whenComplete((s, t) -> waiter.resume()); // Never invoked!
7475

7576
waiter.await(1000);
77+
executor.shutdownNow();
7678
}
7779
}

src/test/java/net/jodah/failsafe/issues/Issue190Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import net.jodah.failsafe.RetryPolicy;
66
import net.jodah.failsafe.Testing;
77
import org.testng.Assert;
8-
import org.testng.annotations.AfterTest;
9-
import org.testng.annotations.BeforeTest;
8+
import org.testng.annotations.AfterClass;
9+
import org.testng.annotations.BeforeClass;
1010
import org.testng.annotations.Test;
1111

1212
import java.util.concurrent.Executors;
@@ -17,13 +17,13 @@
1717
public class Issue190Test {
1818
ScheduledExecutorService executor;
1919

20-
@BeforeTest
21-
protected void beforeTest() {
20+
@BeforeClass
21+
protected void beforeClass() {
2222
executor = Executors.newSingleThreadScheduledExecutor();
2323
}
2424

25-
@AfterTest
26-
protected void afterTest() {
25+
@AfterClass
26+
protected void afterClass() {
2727
executor.shutdownNow();
2828
}
2929

src/test/java/net/jodah/failsafe/issues/Issue192Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import net.jodah.failsafe.RetryPolicy;
66
import net.jodah.failsafe.Testing;
77
import org.testng.Assert;
8-
import org.testng.annotations.AfterTest;
9-
import org.testng.annotations.BeforeTest;
8+
import org.testng.annotations.AfterClass;
9+
import org.testng.annotations.BeforeClass;
1010
import org.testng.annotations.Test;
1111

1212
import java.util.concurrent.ExecutionException;
@@ -27,13 +27,13 @@ static class ExceptionB extends Exception {
2727
static class ExceptionC extends Exception {
2828
}
2929

30-
@BeforeTest
31-
protected void beforeTest() {
30+
@BeforeClass
31+
protected void beforeClass() {
3232
executor = Executors.newSingleThreadScheduledExecutor();
3333
}
3434

35-
@AfterTest
36-
protected void afterTest() {
35+
@AfterClass
36+
protected void afterClass() {
3737
executor.shutdownNow();
3838
}
3939

src/test/java/net/jodah/failsafe/issues/Issue52Test.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.jodah.failsafe.Asserts;
1919
import net.jodah.failsafe.Failsafe;
2020
import net.jodah.failsafe.RetryPolicy;
21+
import org.testng.annotations.AfterClass;
2122
import org.testng.annotations.Test;
2223

2324
import java.time.Duration;
@@ -29,9 +30,15 @@
2930

3031
@Test
3132
public class Issue52Test {
33+
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
34+
35+
@AfterClass
36+
protected void afterClass() {
37+
scheduler.shutdownNow();
38+
}
39+
3240
@Test(expectedExceptions = CancellationException.class)
3341
public void shouldCancelExecutionViaFuture() throws Throwable {
34-
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
3542
Future<Object> proxyFuture = Failsafe.with(new RetryPolicy<>().withDelay(Duration.ofMillis(10)))
3643
.with(scheduler)
3744
.getAsync(exec -> {
@@ -43,7 +50,6 @@ public void shouldCancelExecutionViaFuture() throws Throwable {
4350
}
4451

4552
public void shouldCancelExecutionViaCompletableFuture() throws Throwable {
46-
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
4753
AtomicInteger counter = new AtomicInteger();
4854
CompletableFuture<String> proxyFuture = Failsafe.with(new RetryPolicy<>().withDelay(Duration.ofMillis(10)))
4955
.with(scheduler)

src/test/java/net/jodah/failsafe/issues/Issue55Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ public void shouldOnlyFallbackOnFailure() throws Throwable {
4545

4646
Thread.sleep(100);
4747
assertEquals(counter.get(), 1);
48+
executor.shutdownNow();
4849
}
4950
}

src/test/java/net/jodah/failsafe/issues/Issue5Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ public void test() throws Throwable {
4343
}).getAsync(() -> null);
4444

4545
waiter.await(1000);
46+
executor.shutdownNow();
4647
}
4748
}

0 commit comments

Comments
 (0)