Search in sources :

Example 26 with IExecutionSemaphore

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

the class ClientRunContextFutureFilterTest method testMutualExclusion.

@Test
public void testMutualExclusion() {
    IExecutionSemaphore mutex1 = Jobs.newExecutionSemaphore(1);
    IExecutionSemaphore mutex2 = Jobs.newExecutionSemaphore(1);
    m_clientJobFuture.getJobInput().withExecutionSemaphore(mutex1);
    assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).toFilter().accept(m_clientJobFuture));
    assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchExecutionSemaphore(null).toFilter().accept(m_clientJobFuture));
    assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchExecutionSemaphore(mutex1).toFilter().accept(m_clientJobFuture));
    assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchExecutionSemaphore(mutex2).toFilter().accept(m_clientJobFuture));
    m_clientJobFuture.getJobInput().withExecutionSemaphore(null);
    assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).toFilter().accept(m_clientJobFuture));
    assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchExecutionSemaphore(null).toFilter().accept(m_clientJobFuture));
    assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchExecutionSemaphore(mutex1).toFilter().accept(m_clientJobFuture));
    assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchExecutionSemaphore(mutex2).toFilter().accept(m_clientJobFuture));
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) Test(org.junit.Test)

Example 27 with IExecutionSemaphore

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

the class MutualExclusionTest method testAwaitDoneWithSameMutexButNotMutexOwner.

/**
 * A mutual exclusive job is running, and passes the mutex via BlockingCondition.waitFor() to the next task. But the
 * blocking condition is never unblocked, which results in a timeout. However, the job re-acquires the mutex anew
 * before continuing.<br/>
 * Tests, that the job is the mutex owner after the timeout, and that it cannot wait for another model job to
 * complete.
 */
@Test(timeout = 5000)
public void testAwaitDoneWithSameMutexButNotMutexOwner() {
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            IBlockingCondition bc = Jobs.newBlockingCondition(true);
            try {
                bc.waitFor(1, TimeUnit.SECONDS);
                fail("timeout expected");
            } catch (TimedOutError e) {
                assertTrue(IFuture.CURRENT.get().getExecutionSemaphore().isPermitOwner(IFuture.CURRENT.get()));
                try {
                    final AtomicBoolean run = new AtomicBoolean(false);
                    Jobs.schedule(new IRunnable() {

                        @Override
                        public void run() throws Exception {
                            run.set(true);
                        }
                    }, Jobs.newInput().withExecutionSemaphore(mutex)).awaitDone(1, TimeUnit.SECONDS);
                    fail("AssertionException expected, because the current job is the mutex owner");
                } catch (TimedOutError e1) {
                    fail("no timeout expected");
                } catch (AssertionException e1) {
                // NOOP: OK
                }
            }
        }
    }, Jobs.newInput().withExecutionSemaphore(mutex)).awaitDoneAndGet();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 28 with IExecutionSemaphore

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

the class MutualExclusionTest method testBlockingCondition_InterruptedWhileBeingBlocked.

/**
 * We have 3 jobs that are scheduled simultaneously. Thereby, job1 enters a blocking condition which in turn lets
 * job-2 run. Job-2 goes to sleep until released, meaning that job-3 will not start running. The test verifies, that
 * when job-1 is interrupted, job-1 competes for the mutex anew and continues execution upon job-2 terminates, but
 * before job-3 commence execution.
 */
@Test
public void testBlockingCondition_InterruptedWhileBeingBlocked() throws java.lang.InterruptedException {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    final IExecutionSemaphore semaphore = ClientRunContexts.copyCurrent().getSession().getModelJobSemaphore();
    final IFuture<Void> future1 = ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("running-1");
            try {
                condition.waitFor();
            } catch (ThreadInterruptedError e) {
                protocol.add("interrupted-1 (a)");
                if (Thread.interrupted()) {
                    protocol.add("interrupted-1 (b)");
                    // Restore the interruption status
                    Thread.currentThread().interrupt();
                }
                if (ModelJobs.isModelThread()) {
                    protocol.add("model-thread-1");
                }
            }
            verifyLatch.countDown();
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("running-2");
            job2RunningLatch.countDownAndBlock();
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExceptionHandling(null, false).withExecutionHint(JOB_IDENTIFIER));
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("running-3");
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
    assertTrue(job2RunningLatch.await());
    assertEquals(future1.getState(), JobState.WAITING_FOR_BLOCKING_CONDITION);
    // RUN THE TEST
    // job-2 and job-3
    assertEquals(2, semaphore.getCompetitorCount());
    future1.cancel(true);
    // wait until job-1 is re-acquiring the mutex
    JobTestUtil.waitForPermitCompetitors(semaphore, 3);
    // Release job-2
    job2RunningLatch.unblock();
    // VERIFY
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
    assertEquals(Arrays.asList("running-1", "running-2", "interrupted-1 (a)", "interrupted-1 (b)", "model-thread-1", "running-3"), protocol);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Aggregations

IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)28 Test (org.junit.Test)26 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)23 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)11 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)8 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)6 IFuture (org.eclipse.scout.rt.platform.job.IFuture)5 Times (org.eclipse.scout.rt.testing.platform.runner.Times)5 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JMSException (javax.jms.JMSException)1 NamingException (javax.naming.NamingException)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1