java - How do I force a ExecutorService.awaitTermination() timeout? -


the code-under-test executes set of callable objects via executorservice.invokeall(). executorservice thread pool shutdown() , awaittermination() called.

executorservice threadpool = executors.newfixedthreadpool(healthchecks.size()); list<future <healthstatus> > checkresults = threadpool.invokeall(healthchecks); threadpool.shutdown();  // wait threads finish part of shutdown. if (!threadpool.awaittermination(status_check_timeout_seconds, timeunit.seconds)) {     throw new exception("health checks did not complete in time."); } 

i want unit test awaittermination() timeout.

now, can inject own set of callable objects run , created 1 cause timeout. class object looks (using concurrentunit.waiter object):

public static final class timeouthealthcheck implements healthchecker {     public final long sleepintervalmillis;     string healthname;     boolean result;     waiter waiter;      public timeouthealthcheck(long timeoutinmillis, boolean result, string healthname, waiter waiter) {         sleepintervalmillis = timeoutinmillis;         this.healthname = healthname;         this.result = result;         this.waiter = waiter;     }     public healthstatus call() throws exception {         thread.sleep(sleepintervalmillis);         if (waiter != null) {             waiter.resume();         }         return new testhealthstatus(healthname,result);     } } 

however, when run junit test completes (and cannot confirm timeout path taken in code-under-test).

    @test(expected = java.lang.exception.class) public void teststatustimeout() throws exception {      final waiter waiter = new waiter();      healthchecker checker = new timeouthealthcheck(             serverstatuscheck.status_check_timeout_seconds*1000*2,             true,             "teststatustimeout",             waiter);      collection<healthchecker> testcollect = new arraylist<healthchecker>(1);     testcollect.add(checker);      serverstatuscheck checkundertest =             new serverstatuscheck(testcollect, new defaultdatetimeprovider());      checkundertest.status();     waiter.await(serverstatuscheck.status_check_timeout_seconds*1000*3); } 

i using concurrentunit maven package (0.4.1) either misunderstanding waiter or isn't right direction.

if matters, running junit under eclipse , java 8.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -