I want to be able to junit test the following:
private ExecutorService executor = Executors.newSingleThreadExecutor();
public void foo() {
final EntityManagerFactory entityManagerFactory =
entityManager.getEntityManagerFactory();
executor.execute(new Runnable() {
#Override
public void run() {
EntityManager entityManager =
entityManagerFactory.createEntityManager();
Session session = (Session) entityManager.getDelegate();
try {
SQLQuery query =
session.createSQLQuery("SELECT * from foo_function()");
} catch (HibernateException exception) {
LOGGER.error("Exception: " + exception);
}
}
});
}
I am using Mockito. I tried to get it to throw an exception, for example:
Mockito.doThrow(SQLException.class).when(mockSession).createSQLQuery(any(String.class));
It would throw an exception in the spawned thread, but it doesn't fail when I run it as a Junit test.
You'll want to replace the Executor or ExecutorService, either in the class under test:
public class YourClass {
private Executor executorService = Executor.newSingleThreadExecutor();
public YourClass() {}
/** package-visible for testing */
YourClass(Executor executor) {
/* ... */
}
public void foo() { /* ... */ }
}
...or just in the method you're testing:
public class YourClass {
public void foo() {
foo(executorService);
}
/** package-visible for testing */
void foo(Executor executor) {
/* ... */
}
}
After that, you can either use a manual or Guava-based direct executor, as in this SO question and its answers:
#Test public void fooShouldHandlException() {
// ...
systemUnderTest.foo(new Executor() {
#Override public void execute(Runnable r) { r.run(); }
});
// ...
}
Or replace it with a Mockito mock to test both before and after the Runnable is run:
#Test public void fooShouldHandlException() {
Executor mockExecutor = Mockito.mock(Executor.class);
ArgumentCaptor<Runnable> runnableCaptor =
ArgumentCaptor.forClass(Runnable.class);
systemUnderTest.foo(mockExecutor);
// assert "before Runnable is run" state, if applicable
verify(mockExecutor).execute(runnableCaptor.capture());
runnableCaptor.getValue().run();
// assert "after Runnable is run" state
}
Related
As Brian Goetz states: "TrackingExecutor has an unavoidable race condition that could make it yield false positives: tasks that are identified as cancelled but actually completed. This arises because the thread pool could be shut down between when the last instruction of the task executes and when the pool records the task as complete."
TrackingExecutor:
/**
* TrackingExecutor
* <p/>
* ExecutorService that keeps track of cancelled tasks after shutdown
*
* #author Brian Goetz and Tim Peierls
*/
public class TrackingExecutor extends AbstractExecutorService {
private final ExecutorService exec;
private final Set<Runnable> tasksCancelledAtShutdown =
Collections.synchronizedSet(new HashSet<Runnable>());
public TrackingExecutor(ExecutorService exec) {
this.exec = exec;
}
public void shutdown() {
exec.shutdown();
}
public List<Runnable> shutdownNow() {
return exec.shutdownNow();
}
public boolean isShutdown() {
return exec.isShutdown();
}
public boolean isTerminated() {
return exec.isTerminated();
}
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
return exec.awaitTermination(timeout, unit);
}
public List<Runnable> getCancelledTasks() {
if (!exec.isTerminated())
throw new IllegalStateException(/*...*/);
return new ArrayList<Runnable>(tasksCancelledAtShutdown);
}
public void execute(final Runnable runnable) {
exec.execute(new Runnable() {
public void run() {
try {
runnable.run();
} finally {
if (isShutdown()
&& Thread.currentThread().isInterrupted())
tasksCancelledAtShutdown.add(runnable);
}
}
});
}
}
Then he creates Crawler which uses TrackingExecutor:
crawler:
/**
* WebCrawler
* <p/>
* Using TrackingExecutorService to save unfinished tasks for later execution
*
* #author Brian Goetz and Tim Peierls
*/
public abstract class WebCrawler {
private volatile TrackingExecutor exec;
#GuardedBy("this") private final Set<URL> urlsToCrawl = new HashSet<URL>();
private final ConcurrentMap<URL, Boolean> seen = new ConcurrentHashMap<URL, Boolean>();
private static final long TIMEOUT = 500;
private static final TimeUnit UNIT = MILLISECONDS;
public WebCrawler(URL startUrl) {
urlsToCrawl.add(startUrl);
}
public synchronized void start() {
exec = new TrackingExecutor(Executors.newCachedThreadPool());
for (URL url : urlsToCrawl) submitCrawlTask(url);
urlsToCrawl.clear();
}
public synchronized void stop() throws InterruptedException {
try {
saveUncrawled(exec.shutdownNow());
if (exec.awaitTermination(TIMEOUT, UNIT))
saveUncrawled(exec.getCancelledTasks());
} finally {
exec = null;
}
}
protected abstract List<URL> processPage(URL url);
private void saveUncrawled(List<Runnable> uncrawled) {
for (Runnable task : uncrawled)
urlsToCrawl.add(((CrawlTask) task).getPage());
}
private void submitCrawlTask(URL u) {
exec.execute(new CrawlTask(u));
}
private class CrawlTask implements Runnable {
private final URL url;
CrawlTask(URL url) {
this.url = url;
}
private int count = 1;
boolean alreadyCrawled() {
return seen.putIfAbsent(url, true) != null;
}
void markUncrawled() {
seen.remove(url);
System.out.printf("marking %s uncrawled%n", url);
}
public void run() {
for (URL link : processPage(url)) {
if (Thread.currentThread().isInterrupted())
return;
submitCrawlTask(link);
}
}
public URL getPage() {
return url;
}
}
}
But I don't understand what is the exact chronology of calls of runnable.run(), exec.shutdownNow(), exec.awaitTermination(...), exec.getCancelledTasks(), tasksCancelledAtShutdown.add(runnable), the runnable completion and thread-interleaving, which leads to a race condition.
This is how I understand it. For example,TrackingExecutor is shutting down before CrawlTask exit, this task may be also recorded as a taskCancelledAtShutdown, because if (isShutdown() && Thread.currentThread().isInterrupted()) in TrackingExecutor#execute may be true , but in fact this task has completed.
private class CrawlTask implements Runnable {
public void run() {
for (URL link : processPage(url)) {
if (Thread.currentThread().isInterrupted())
return;
submitCrawlTask(link);
}
// May be here, trackingExecutor is shutting down.
// Actually this task has completed now.But this method did not exit.
}
}
public void execute(final Runnable runnable) {
exec.execute(new Runnable() {
public void run() {
try {
runnable.run();
} finally {
// isShutdown() && Thread.currentThread().isInterrupted() may be true
if (isShutdown()
&& Thread.currentThread().isInterrupted())
tasksCancelledAtShutdown.add(runnable);
}
}
});
}
I'm trying to write something using reactor which I know how to write using completable futures. I'm getting "Calling subscribe in non-blocking scope" warning in it.
My goal is to call turnOn() with a timeout which should call turnOff() after the timeout. If turnOn() is called again it should cancel the old timeout and wait for a new timeout.
How should I do this? I could do a hibrate and use CompletableFuture for the timeout but reactor's api is just a bit easier.
this test works as expected:
public class TimeoutTest {
Service service;
#BeforeEach
public void setUp() {
service = mock(Service.class);
}
CompletableFuture<Void> turnOffFuture = null;
#DisplayName("Should timeout on turnOn with timeout")
#Test
public void timeoutCompletableFuture() throws InterruptedException {
turnOn(Duration.ofMillis(100)).join();
verify(service).turnOn();
verify(service,never()).turnOff();
Thread.sleep(1000);
verify(service).turnOff();
}
private interface Service{
void turnOn();
void turnOff();
}
public void cancelTimeout() {
if (turnOffFuture != null)
turnOffFuture.cancel(false);
turnOffFuture = null;
}
public CompletableFuture<Void> turnOn(Duration timeout) {
CompletableFuture<Void> turnOnFuture = turnOn();
cancelTimeout();
turnOffFuture = turnOnFuture.thenRun(() -> delay(timeout))
.thenRun(this::turnOff);
return turnOnFuture;
}
private void delay(Duration duration) {
try {
Thread.sleep(BigDecimal.valueOf(duration.getSeconds())
.scaleByPowerOfTen(3)
.add(BigDecimal.valueOf(duration.getNano(), 6))
.intValue());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private CompletableFuture<Void> turnOn() {
return CompletableFuture.runAsync(() -> service.turnOn());
}
private CompletableFuture<Void> turnOff() {
return CompletableFuture.runAsync(() -> service.turnOff());
}
}
but my reactor code does not.
public class TimeoutMonoTest {
Service service;
#BeforeEach
public void setUp() {
service = mock(Service.class);
}
Disposable turnOffDisposable = null;
#DisplayName("Should timeout on turnOn with timeout")
#Test
public void timeoutMono() throws InterruptedException {
turnOn(Duration.ofMillis(100)).block(Duration.ofMillis(10));
verify(service).turnOn();
verify(service, never()).turnOff();
Thread.sleep(1000);
verify(service).turnOff();
}
private interface Service {
void turnOn();
void turnOff();
}
public void cancelTimeout() {
if (turnOffDisposable != null)
turnOffDisposable.dispose();
turnOffDisposable = null;
}
public Mono<Void> turnOn(Duration timeout) {
Mono<Void> turnOnFuture = turnOn();
cancelTimeout();
turnOffDisposable = turnOnFuture.delayElement(timeout)
.subscribe(it -> this.turnOff());
return turnOnFuture;
}
private Mono<Void> turnOn() {
service.turnOn();
return Mono.just("not empty but mapped to void").then();
}
private Mono<Void> turnOff() {
service.turnOff();
return Mono.just("not empty but mapped to void").then();
}
}
The problem lies in the mapping to void mono's in the turnOn() and turnOff() methods. They do not actually get a "next" signal, just a "success" signal.
The fix is simply to change the turnOn method to:
public Mono<Void> turnOn(Duration timeout) {
cancelTimeout();
Mono<Void> turnOnMono = turnOn();
turnOffDisposable = turnOnMono.delayElement(timeout)
.then(turnOff())
.subscribe();
return turnOn();
}
I have the below code
public final class JoinableTaskPool<T> extends ABC {
private int taskCounter;
private final Object monitor;
private final ExtendedThreadPoolExecutor service;
private final CompletionService<T> compService;
public Future<T> submit(final Callable<T> task) {
final Future<T> result = compService.submit(task);
service.submit(new Runnable() {
public void run() {
try {
final Future<T> result = compService.take();
try {
handler.processResult(result);
} catch (final Throwable t) {
throw new SearchException("Task has an error", t);
}
} catch (InterruptedException e) {
throw new SearchException("Task has an error", e);
}
}
}
return result;
}
public void join() {
synchronized (monitor) {
while (taskCounter != 0) {
try {
monitor.wait();
} catch (InterruptedException e) {
error(e, "Interrupted in join");
}
}
}
}
The ExtendedThreadPoolExecutor class is defined as follows
public class ExtendedThreadPoolExecutor extends ThreadPoolExecutor {
public ExtendedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
#Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if(t != null) {
throw new SearchException("Error while executing the task", t);
}
}
}
I am trying to write a unit test for this method. Below is the method
#RunWith(MockitoJUnitRunner.class)
public class TestJoinableTaskPool<T> {
private JoinableTaskPool<T> pool;
#Before
public void setUp() {
pool = new JoinableTaskPool<T>(1);
}
#After
public void tearDown() {
pool.shutdown();
}
#Test (expected = SearchException.class)
public void testSubmit() throws Exception{
Callable task = (Callable<T>) () -> null;
Mockito.when(pool.getFinishHandler().processResult(result))
.thenThrow(RuntimeException.class);
pool.submit(task);
}
}
Since the SearchException exception is thrown in runnable, there is no way to access it outside the submit method. If I would have returned the Future returned by executorService.submit, I could have done a future.get() to get an exception, but I am returning another future (result variable).
So while writing the unit test I am not able to get the exception thrown.
Also I have overriden the afterExecute() in an effort to catch exception from unit test,but couldnt find a way to call it.
How do I test this exception thrown from the runnable from a unit test.
Any help would be appreciated.
Putting aside that his code smells a mile away, what you can do is to create additional interface eg
public interface MyErrorHandler {
handleError(Exception e)
implement it in your executor pools and call it on exception. Then you can use use Mockito.spy to see if tha method have been called or not on MyErrorHandler (cglib should allow you to do that even withount additional intrerface.)
alternatively you can define that MyExceptionHandler instance should be passed to an executor (via constructor or setter) so you will be able to provide use case dependent implementation of such callback
I am trying to execute a runnable object using the Java concurrency package's, ExecutorService SingleThreadExecutor. When I call the execute a command on the new Runnable object it simply steps over it. i.e. the run() method isn't called.
I have stepped through my lines of code using the debugger and can see my SingleThreadExecutor has been created and my Runnable is initialised.
public class RunnableDemo {
public ExecutorService executor;
public RunnableDemo () {
this.executor = Executors.newSingleThreadExecutor();
}
public void start(){
executor.execute(new MyRunnable("Hello World"));
}
public static void main(String[] args){
RunnableDemo app = new RunnableDemo();
app.start();
}
}
public class MyRunnable implements Runnable {
private String strToPrint;
public MyRunnable(String parameter) {
this.strToPrint = parameter;
}
public void run() {
System.out.println(strToPrint);
}
}
And probably self-explanatory but in this scenario, I would expect to see "Hello World" printed to screen. However, the execute/run method doesn't seem to be invoked after the runnable is created.
Your program is terminating before the executer starts the task.
You have to wait for the executor to finish like this:
public class RunnableDemo {
public ExecutorService executor;
public RunnableDemo () {
this.executor = Executors.newSingleThreadExecutor();
}
public void start(){
executor.execute(new MyRunnable("Hello World"));
}
public void awaitTermination(){
try {
service.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
RunnableDemo app = new RunnableDemo();
app.start();
app.awaitTermination();
}
}
public class MyRunnable implements Runnable {
private String strToPrint;
public MyRunnable(String parameter) {
this.strToPrint = parameter;
}
public void run() {
System.out.println(strToPrint);
}
}
I have following method for test:
public class classToTest{
#Autowired
private Alternator alternator;
public void methodToTest(){
Thread t = new Thread(new Runnable() {
public void run() {
while(true) {
if(alternator.get()) {
System.out.print("Hello");
alternator.set(false);
}
}
}
};
t.start()
}
}
I need to check that was invoked method
alternator.set(false);
How can I do it?
Instead of starting a thread directly, can you pass in an "Executor" instance?
For example...
public class ClassToTest{
#Autowired
private Alternator alternator;
#Autowired #Qualifier("myExecutor")
private java.util.concurrent.Executor executor;
public void methodToTest() {
Runnable runnable = new Runnable() {
public void run() {
while(true) {
if(alternator.get()) {
System.out.print("Hello");
alternator.set(false);
}
}
};
executor.execute(runnable);
}
}
Now you can test this easier...
public class ClassToTestTest {
...
#Before
public void setup() {
alternator = mock(Alternator.class);
executor = mock(Executor.class);
obj = new ClassToTest();
ReflectionTestUtils.setField(obj, "alternator", alternator);
ReflectionTestUtils.setField(obj, "executor", executor);
}
#Test
public void shouldStartRunnable() {
obj.methodToTest();
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
verify(executor).execute(runnableCaptor.capture());
Runnable runnable = runnableCaptor.getValue();
// Now test your actual "runnable"...
when(alternator.get()).thenReturn(true);
runnable.run();
verify(alternator).set(false);
}
}
(Have not tried to compile this, so I apologise if there are any mistakes!!)
Though Bret's post of passing in an executor is very much recommended, you can use the timeout() mock verification setting to test for asynchronous conditions.
verify(alternator, timeout(500)).set(false);
Note that this will necessarily increase the flakiness of your test (i.e. the likelihood that the test fails when the code passes). With a sensible timeout value, that flakiness should be negligible, but if you're making this a part of your core test infrastructure you may consider refactoring to allow for synchronous execution in the test.