Search in sources :

Example 1 with JobFutureTask

use of org.eclipse.scout.rt.platform.job.internal.JobFutureTask in project scout.rt by eclipse.

the class MutualExclusionTest method testBlockedJobs.

/**
 * We have 5 jobs that get scheduled simultaneously. The first waits some time so that job2, job3, job4 and job5 get
 * queued. Job1 then enters a blocking condition, which allows job2 to run. But job2 gets rejected by the executor,
 * which allows job3 to run. After job3 completes, job1 is resumed and continues running. After job1 complete, job4
 * gets scheduled. Job4 in turn gets blocked, which prevents job5 from running.
 */
@Test
public void testBlockedJobs() throws java.lang.InterruptedException {
    P_JobManager jobManager = new P_JobManager();
    ExecutorService executorMock = jobManager.getExecutorMock();
    IBean<IJobManager> jobManagerBean = JobTestUtil.replaceCurrentJobManager(jobManager);
    try {
        // synchronized because modified/read by different threads.
        final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
        // Executor mock
        doAnswer(new Answer<Future>() {

            @Override
            public Future answer(InvocationOnMock invocation) throws Throwable {
                final Runnable runnable = (Runnable) invocation.getArguments()[0];
                // Reject job-2 from being scheduled
                if (runnable instanceof JobFutureTask) {
                    JobFutureTask<?> futureTask = (JobFutureTask<?>) runnable;
                    if ("job-2".equals(futureTask.getJobInput().getName())) {
                        futureTask.reject();
                        return null;
                    }
                }
                s_executor.execute(new NamedThreadRunnable(runnable));
                return null;
            }
        }).when(executorMock).execute(any(Runnable.class));
        final BlockingCountDownLatch job4RunningLatch = new BlockingCountDownLatch(1);
        final IBlockingCondition condition = Jobs.newBlockingCondition(true);
        // Job-1
        IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                // Wait until all 5 jobs are scheduled.
                JobTestUtil.waitForPermitCompetitors(m_clientSession.getModelJobSemaphore(), 5);
                try {
                    protocol.add("running-job-1 (a)");
                    condition.waitFor();
                    protocol.add("running-job-1 (b)");
                } catch (ProcessingException e) {
                    protocol.add("jobException");
                }
                if (ModelJobs.isModelThread()) {
                    protocol.add("running-job-1 (e) [model-thread]");
                }
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
        // Job-2
        IFuture<Void> future2 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-job-2");
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
        // Job-3
        IFuture<Void> future3 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-job-3 (a)");
                condition.setBlocking(false);
                // Wait until job-1 tried to re-acquire the mutex.
                // 4 = job1(re-acquiring), job3(owner), job4, job5
                JobTestUtil.waitForPermitCompetitors(m_clientSession.getModelJobSemaphore(), 4);
                protocol.add("running-job-3 (b)");
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-3").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
        // Job-4
        IFuture<Void> future4 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-job-4");
                try {
                    job4RunningLatch.countDownAndBlock();
                } catch (java.lang.InterruptedException e) {
                    protocol.add("job-4 [interrupted]");
                }
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-4").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
        // Job-5
        IFuture<Void> future5 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-job-5");
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-5").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
        assertTrue(job4RunningLatch.await());
        try {
            Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 1, TimeUnit.MILLISECONDS);
            // job-4 and job-5 are pending
            fail("timeout expected");
        } catch (TimedOutError e) {
        // NOOP
        }
        // job-4 and job-5 are pending
        assertFalse(Jobs.getJobManager().isDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter()));
        List<String> expectedProtocol = new ArrayList<>();
        expectedProtocol.add("running-job-1 (a)");
        expectedProtocol.add("running-job-3 (a)");
        expectedProtocol.add("running-job-3 (b)");
        expectedProtocol.add("running-job-1 (b)");
        expectedProtocol.add("running-job-1 (e) [model-thread]");
        expectedProtocol.add("running-job-4");
        assertEquals(expectedProtocol, protocol);
        assertFalse(future1.isCancelled());
        assertTrue(future1.isDone());
        assertTrue(future2.isCancelled());
        assertTrue(future2.isDone());
        assertFalse(future3.isCancelled());
        assertTrue(future3.isDone());
        assertFalse(future4.isCancelled());
        assertFalse(future4.isDone());
        assertFalse(future5.isCancelled());
        assertFalse(future5.isDone());
        // cancel job4
        future4.cancel(true);
        awaitDoneElseFail(JOB_IDENTIFIER);
        expectedProtocol.add("job-4 [interrupted]");
        expectedProtocol.add("running-job-5");
        assertEquals(expectedProtocol, protocol);
        assertTrue(Jobs.getJobManager().isDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter()));
        assertTrue(future4.isCancelled());
        assertTrue(future4.isDone());
        assertFalse(future5.isCancelled());
        assertTrue(future5.isDone());
    } finally {
        JobTestUtil.unregisterAndShutdownJobManager(jobManagerBean);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ArrayList(java.util.ArrayList) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) JobFutureTask(org.eclipse.scout.rt.platform.job.internal.JobFutureTask) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) IFuture(org.eclipse.scout.rt.platform.job.IFuture) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 2 with JobFutureTask

use of org.eclipse.scout.rt.platform.job.internal.JobFutureTask in project scout.rt by eclipse.

the class MutualExclusionTest method testRejection.

/**
 * We have 3 jobs that get scheduled simultaneously. The first waits some time so that job2 and job3 get queued. When
 * job2 gets scheduled, it is rejected by the executor. This test verifies, that job2 still gets scheduled.
 */
@Test
public void testRejection() {
    P_JobManager jobManager = new P_JobManager();
    ExecutorService executorMock = jobManager.getExecutorMock();
    IBean<IJobManager> jobManagerBean = JobTestUtil.replaceCurrentJobManager(jobManager);
    try {
        // synchronized because modified/read by different threads.
        final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
        // Executor mock
        doAnswer(new Answer<Future>() {

            @Override
            public Future answer(InvocationOnMock invocation) throws Throwable {
                final Runnable runnable = (Runnable) invocation.getArguments()[0];
                // Reject job-2 from being scheduled
                if (runnable instanceof JobFutureTask) {
                    JobFutureTask<?> futureTask = (JobFutureTask<?>) runnable;
                    if ("job-2".equals(futureTask.getJobInput().getName())) {
                        futureTask.reject();
                        return null;
                    }
                }
                s_executor.execute(new NamedThreadRunnable(runnable));
                return null;
            }
        }).when(executorMock).execute(any(Runnable.class));
        // Job-1
        final IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                // Wait until all 3 jobs are scheduled.
                JobTestUtil.waitForPermitCompetitors(m_clientSession.getModelJobSemaphore(), 3);
                protocol.add("running-job-1");
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-1").withExecutionHint(JOB_IDENTIFIER));
        // Job-2
        final IFuture<Void> future2 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-job-2");
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-2").withExecutionHint(JOB_IDENTIFIER));
        // Job-3
        IFuture<Void> future3 = Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-job-3");
            }
        }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-3").withExecutionHint(JOB_IDENTIFIER));
        awaitDoneElseFail(JOB_IDENTIFIER);
        assertEquals(Arrays.asList("running-job-1", "running-job-3"), protocol);
        assertTrue(Jobs.getJobManager().isDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter()));
        assertFalse(future1.isCancelled());
        assertTrue(future2.isCancelled());
        assertFalse(future3.isCancelled());
    } finally {
        JobTestUtil.unregisterAndShutdownJobManager(jobManagerBean);
    }
}
Also used : JobFutureTask(org.eclipse.scout.rt.platform.job.internal.JobFutureTask) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) IFuture(org.eclipse.scout.rt.platform.job.IFuture) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)2 JobFutureTask (org.eclipse.scout.rt.platform.job.internal.JobFutureTask)2 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)2 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ArrayList (java.util.ArrayList)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)1 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)1