java - Running multiple threads from within a unit test -
below unit test uses scheduledexecutorservice execute scheduled runnable every second :
import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; import org.junit.test; public class concurrentrequestsimulator { private static final int number_requests = 4; @test public void testgettodolist() { try { scheduledexecutorservice scheduler = executors.newscheduledthreadpool(10); scheduler.scheduleatfixedrate(new requestthreadinvoker(), 0, 1, timeunit.seconds); } catch (exception e) { e.printstacktrace(); } } private final class requestthreadinvoker implements runnable { public void run() { executorservice es = executors.newcachedthreadpool(); (int = 1; <= number_requests; i++) { es.execute(new requestthread()); } es.shutdown(); while (!es.isterminated()) { } } } private final class requestthread implements runnable { public void run() { try { system.out.println("in requestthread"); thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } } } }
the line system.out.println("in requestthread");
in requestthread
not appear invoked no output displayed console. think issue since test runs completion, causes scheduler
stop scheduling? can test updated scheduler
not terminated , requestthreadinvoker
invoked repeatedly once per second ?
in requestthreadinvoker
create new instance of executorservice
. causing issue ?
adding thread.sleep(99000);
end of test causes test wait 99 seconds enough time test run.
Comments
Post a Comment