mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -34,6 +34,14 @@
|
||||
</parent>
|
||||
<artifactId>async-method-invocation</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
||||
+12
-15
@@ -30,15 +30,15 @@ import lombok.extern.slf4j.Slf4j;
|
||||
/**
|
||||
* In this example, we are launching space rockets and deploying lunar rovers.
|
||||
*
|
||||
* <p>The application demonstrates the async method invocation pattern. The key parts of the
|
||||
* pattern are <code>AsyncResult</code> which is an intermediate container for an asynchronously
|
||||
* evaluated value, <code>AsyncCallback</code> which can be provided to be executed on task
|
||||
* completion and <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
||||
* <p>The application demonstrates the async method invocation pattern. The key parts of the pattern
|
||||
* are <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated
|
||||
* value, <code>AsyncCallback</code> which can be provided to be executed on task completion and
|
||||
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
||||
*
|
||||
* <p>The main method shows example flow of async invocations. The main thread starts multiple
|
||||
* tasks with variable durations and then continues its own work. When the main thread has done it's
|
||||
* job it collects the results of the async tasks. Two of the tasks are handled with callbacks,
|
||||
* meaning the callbacks are executed immediately when the tasks complete.
|
||||
* <p>The main method shows example flow of async invocations. The main thread starts multiple tasks
|
||||
* with variable durations and then continues its own work. When the main thread has done it's job
|
||||
* it collects the results of the async tasks. Two of the tasks are handled with callbacks, meaning
|
||||
* the callbacks are executed immediately when the tasks complete.
|
||||
*
|
||||
* <p>Noteworthy difference of thread usage between the async results and callbacks is that the
|
||||
* async results are collected in the main thread but the callbacks are executed within the worker
|
||||
@@ -62,10 +62,7 @@ public class App {
|
||||
|
||||
private static final String ROCKET_LAUNCH_LOG_PATTERN = "Space rocket <%s> launched successfully";
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*/
|
||||
|
||||
/** Program entry point. */
|
||||
public static void main(String[] args) throws Exception {
|
||||
// construct a new executor that will run async tasks
|
||||
var executor = new ThreadAsyncExecutor();
|
||||
@@ -74,8 +71,8 @@ public class App {
|
||||
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||
final var asyncResult4 = executor.startProcess(lazyval(20, 400),
|
||||
callback("Deploying lunar rover"));
|
||||
final var asyncResult4 =
|
||||
executor.startProcess(lazyval(20, 400), callback("Deploying lunar rover"));
|
||||
final var asyncResult5 =
|
||||
executor.startProcess(lazyval("callback", 600), callback("Deploying lunar rover"));
|
||||
|
||||
@@ -99,7 +96,7 @@ public class App {
|
||||
/**
|
||||
* Creates a callable that lazily evaluates to given value with artificial delay.
|
||||
*
|
||||
* @param value value to evaluate
|
||||
* @param value value to evaluate
|
||||
* @param delayMillis artificial delay in milliseconds
|
||||
* @return new callable for lazy evaluation
|
||||
*/
|
||||
|
||||
+3
-5
@@ -27,9 +27,7 @@ package com.iluwatar.async.method.invocation;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* AsyncExecutor interface.
|
||||
*/
|
||||
/** AsyncExecutor interface. */
|
||||
public interface AsyncExecutor {
|
||||
|
||||
/**
|
||||
@@ -44,7 +42,7 @@ public interface AsyncExecutor {
|
||||
* Starts processing of an async task. Returns immediately with async result. Executes callback
|
||||
* when the task is completed.
|
||||
*
|
||||
* @param task task to be executed asynchronously
|
||||
* @param task task to be executed asynchronously
|
||||
* @param callback callback to be executed on task completion
|
||||
* @return async result for the task
|
||||
*/
|
||||
@@ -56,7 +54,7 @@ public interface AsyncExecutor {
|
||||
*
|
||||
* @param asyncResult async result of a task
|
||||
* @return evaluated value of the completed task
|
||||
* @throws ExecutionException if execution has failed, containing the root cause
|
||||
* @throws ExecutionException if execution has failed, containing the root cause
|
||||
* @throws InterruptedException if the execution is interrupted
|
||||
*/
|
||||
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ public interface AsyncResult<T> {
|
||||
* Gets the value of completed async task.
|
||||
*
|
||||
* @return evaluated value or throws ExecutionException if execution has failed
|
||||
* @throws ExecutionException if execution has failed, containing the root cause
|
||||
* @throws ExecutionException if execution has failed, containing the root cause
|
||||
* @throws IllegalStateException if execution is not completed
|
||||
*/
|
||||
T getValue() throws ExecutionException;
|
||||
|
||||
+14
-16
@@ -24,19 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.async.method.invocation;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Implementation of async executor that creates a new thread for every task.
|
||||
*/
|
||||
/** Implementation of async executor that creates a new thread for every task. */
|
||||
public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
|
||||
/**
|
||||
* Index for thread naming.
|
||||
*/
|
||||
/** Index for thread naming. */
|
||||
private final AtomicInteger idx = new AtomicInteger(0);
|
||||
|
||||
@Override
|
||||
@@ -47,19 +42,22 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
@Override
|
||||
public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) {
|
||||
var result = new CompletableResult<>(callback);
|
||||
new Thread(() -> {
|
||||
try {
|
||||
result.setValue(task.call());
|
||||
} catch (Exception ex) {
|
||||
result.setException(ex);
|
||||
}
|
||||
}, "executor-" + idx.incrementAndGet()).start();
|
||||
new Thread(
|
||||
() -> {
|
||||
try {
|
||||
result.setValue(task.call());
|
||||
} catch (Exception ex) {
|
||||
result.setException(ex);
|
||||
}
|
||||
},
|
||||
"executor-" + idx.incrementAndGet())
|
||||
.start();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
|
||||
InterruptedException {
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult)
|
||||
throws ExecutionException, InterruptedException {
|
||||
if (!asyncResult.isCompleted()) {
|
||||
asyncResult.await();
|
||||
}
|
||||
|
||||
+6
-10
@@ -24,26 +24,22 @@
|
||||
*/
|
||||
package com.iluwatar.async.method.invocation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
+199
-184
@@ -33,7 +33,6 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -43,49 +42,43 @@ import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* ThreadAsyncExecutorTest
|
||||
*
|
||||
*/
|
||||
/** ThreadAsyncExecutorTest */
|
||||
class ThreadAsyncExecutorTest {
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Exception> exceptionCaptor;
|
||||
@Captor private ArgumentCaptor<Exception> exceptionCaptor;
|
||||
|
||||
@Mock
|
||||
private Callable<Object> task;
|
||||
@Mock private Callable<Object> task;
|
||||
|
||||
@Mock
|
||||
private AsyncCallback<Object> callback;
|
||||
@Mock private AsyncCallback<Object> callback;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)}
|
||||
*/
|
||||
/** Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} */
|
||||
@Test
|
||||
void testSuccessfulTaskWithoutCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,28 +87,30 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testSuccessfulTaskWithCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
|
||||
// ... same for the callback, we expect our object
|
||||
verify(callback, times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(exceptionCaptor.capture());
|
||||
// ... same for the callback, we expect our object
|
||||
verify(callback, times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(exceptionCaptor.capture());
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,38 +119,43 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testLongRunningTaskWithoutCallback() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
final var result = new Object();
|
||||
when(task.call())
|
||||
.thenAnswer(
|
||||
i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task);
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task);
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,42 +164,47 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testLongRunningTaskWithCallback() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
final var result = new Object();
|
||||
when(task.call())
|
||||
.thenAnswer(
|
||||
i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
|
||||
verifyNoMoreInteractions(callback);
|
||||
verifyNoMoreInteractions(callback);
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
verify(callback, timeout(3000).times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(isA(Exception.class));
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
verify(callback, timeout(3000).times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(isA(Exception.class));
|
||||
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task, callback);
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task, callback);
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,35 +214,40 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testEndProcess() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
final var result = new Object();
|
||||
when(task.call())
|
||||
.thenAnswer(
|
||||
i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verify(task, times(1)).call();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verify(task, times(1)).call();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
// Calling end process a second time while already finished should give the same result
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verifyNoMoreInteractions(task);
|
||||
});
|
||||
// Calling end process a second time while already finished should give the same result
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verifyNoMoreInteractions(task);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,25 +256,28 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testNullTask() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null);
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null);
|
||||
|
||||
assertNotNull(asyncResult, "The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,32 +286,35 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testNullTaskWithCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, callback);
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, callback);
|
||||
|
||||
assertNotNull(asyncResult, "The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verify(callback, times(0)).onComplete(any());
|
||||
verify(callback, times(1)).onError(exceptionCaptor.capture());
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verify(callback, times(0)).onComplete(any());
|
||||
verify(callback, times(1)).onError(exceptionCaptor.capture());
|
||||
|
||||
final var exception = exceptionCaptor.getValue();
|
||||
assertNotNull(exception);
|
||||
final var exception = exceptionCaptor.getValue();
|
||||
assertNotNull(exception);
|
||||
|
||||
assertEquals(NullPointerException.class, exception.getClass());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
assertEquals(NullPointerException.class, exception.getClass());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,28 +323,27 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testNullTaskWithNullCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, null);
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, null);
|
||||
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task and callback were 'null'."
|
||||
);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task and callback were 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user