Search in sources :

Example 91 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class ModelJobTest method testYield.

/**
 * We have 2 model jobs scheduled in sequence. Due to the mutex, the second model job only commences execution once
 * the first model job completed. However, job 1 yields its permit, so that job-2 can commence execution.
 */
@Test
public void testYield() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch finishLatch = new BlockingCountDownLatch(1);
    final ClientRunContext runContext = ClientRunContexts.empty().withSession(m_clientSession1, true);
    // Schedule first model job
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-1-running");
            setupLatch.await();
            protocol.add("job-1-before-yield");
            ModelJobs.yield();
            protocol.add("job-1-after-yield");
            finishLatch.countDown();
        }
    }, ModelJobs.newInput(runContext.copy()).withName("job-1"));
    // Schedule second model job
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-2-running");
        }
    }, ModelJobs.newInput(runContext.copy()).withName("job-2"));
    setupLatch.countDown();
    finishLatch.await();
    List<String> expectedProtocol = new ArrayList<>();
    expectedProtocol.add("job-1-running");
    expectedProtocol.add("job-1-before-yield");
    expectedProtocol.add("job-2-running");
    expectedProtocol.add("job-1-after-yield");
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) ArrayList(java.util.ArrayList) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 92 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class MutualExclusionTest method testEnterUnblockedBlockingCondition.

/**
 * A job enters a blocking condition, which in the meantime was invalidated. This test verifies, that the job is not
 * blocked when calling waitFor.
 */
@Test
public void testEnterUnblockedBlockingCondition() throws Throwable {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final BlockingCountDownLatch unblockedLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch done = new BlockingCountDownLatch(1);
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final AtomicReference<Throwable> throwableHolder = new AtomicReference<>();
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            s_executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        condition.setBlocking(false);
                        protocol.add("1: afterUnblock [inner]");
                        unblockedLatch.countDown();
                        done.await();
                        protocol.add("4: done");
                    } catch (final Throwable t) {
                        throwableHolder.set(t);
                    }
                }
            });
            // wait until the condition in unblocked
            assertTrue(unblockedLatch.await());
            protocol.add("2: beforeWaitFor [outer]");
            condition.waitFor();
            protocol.add("3: afterWaitFor [outer]");
            done.release();
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
    awaitDoneElseFail(JOB_IDENTIFIER);
    Throwable throwable = throwableHolder.get();
    if (throwable != null) {
        throw throwable;
    }
    List<String> expected = new ArrayList<>();
    expected.add("1: afterUnblock [inner]");
    expected.add("2: beforeWaitFor [outer]");
    expected.add("3: afterWaitFor [outer]");
    expected.add("4: done");
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 93 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch 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

BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)93 Test (org.junit.Test)89 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)66 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)30 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)14 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)12 ArrayList (java.util.ArrayList)10 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)10 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)9 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)9 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)7 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)7 Times (org.eclipse.scout.rt.testing.platform.runner.Times)7 Holder (org.eclipse.scout.rt.platform.holders.Holder)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 IMessage (org.eclipse.scout.rt.mom.api.IMessage)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)3 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)3