Search in sources :

Example 1 with JobManager

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

the class JobCancelTest method testShutdownJobManagerAndSchedule.

@Test
public void testShutdownJobManagerAndSchedule() throws InterruptedException {
    // Use dedicated job manager because job manager is shutdown in tests.
    IBean<IJobManager> jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
    });
    try {
        // synchronized because modified/read by different threads.
        final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
        final BlockingCountDownLatch latch = new BlockingCountDownLatch(2);
        Jobs.getJobManager().schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-1");
                try {
                    latch.countDownAndBlock();
                } catch (InterruptedException e) {
                    protocol.add("interrupted-1");
                } finally {
                    protocol.add("done-1");
                }
            }
        }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
        IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-2");
                try {
                    latch.countDownAndBlock();
                } catch (InterruptedException e) {
                    protocol.add("interrupted-2");
                } finally {
                    protocol.add("done-2");
                }
            }
        }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
        assertTrue(latch.await());
        // SHUTDOWN
        Jobs.getJobManager().shutdown();
        try {
            Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
            fail("AssertionError expected");
        } catch (AssertionException e) {
        // NOOP
        }
        // VERIFY
        assertEquals(CollectionUtility.hashSet("running-1", "running-2", "interrupted-1", "interrupted-2", "done-1", "done-2"), protocol);
        future2.awaitDone(1, TimeUnit.SECONDS);
        assertTrue(future2.isCancelled());
    } finally {
        JobTestUtil.unregisterAndShutdownJobManager(jobManagerBean);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) JobManager(org.eclipse.scout.rt.platform.job.internal.JobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 2 with JobManager

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

the class JobFutureVisitTest method before.

@Before
public void before() throws InterruptedException {
    m_jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
    });
    m_mutex1 = Jobs.newExecutionSemaphore(1);
    m_mutex2 = Jobs.newExecutionSemaphore(1);
    m_mutex3 = Jobs.newExecutionSemaphore(1);
    // prepare the test-case
    // synchronized because modified/read by different threads.
    protocol = Collections.synchronizedSet(new HashSet<String>());
    bc1 = Jobs.newBlockingCondition(true);
    bc2 = Jobs.newBlockingCondition(true);
    m_latch = new BlockingCountDownLatch(3);
    // SESSION 1 (JOB-1)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            bc1.waitFor();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job1").withExecutionSemaphore(m_mutex1).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 1 (JOB-2)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            m_latch.countDownAndBlock();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job2").withExecutionSemaphore(m_mutex1).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 1 (JOB-3)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job3").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex1));
    // =========
    // SESSION 2 (JOB-1)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job1").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex2));
    // SESSION 2 (JOB-2)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            bc2.waitFor();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job2").withExecutionSemaphore(m_mutex2).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 2  (JOB-3)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            bc2.setBlocking(false);
            // Wait until job 'mutex2_job2' is re-acquiring the mutex. [3=job-2, job-3, job-4]
            JobTestUtil.waitForPermitCompetitors(m_mutex2, 3);
            m_latch.countDownAndBlock();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job3").withExecutionSemaphore(m_mutex2).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 2  (JOB-4)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job4").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex2));
    // =========
    // SESSION 3 (JOB-1)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            m_latch.countDownAndBlock();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex3_job1").withExecutionSemaphore(m_mutex3).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    assertTrue(m_latch.await());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobManager(org.eclipse.scout.rt.platform.job.internal.JobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

JobManager (org.eclipse.scout.rt.platform.job.internal.JobManager)2 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)2 HashSet (java.util.HashSet)1 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)1 Before (org.junit.Before)1 Test (org.junit.Test)1