Search in sources :

Example 21 with IBlockingCondition

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

the class DevelopmentThreadNameDecoratorTest method testThreadName.

@Test
// regression; do no remove
@Times(100)
public void testThreadName() throws InterruptedException {
    final AtomicReference<Thread> workerThread = new AtomicReference<>();
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final BlockingCountDownLatch latch1 = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch latch2 = new BlockingCountDownLatch(1);
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            workerThread.set(Thread.currentThread());
            latch1.countDownAndBlock();
            condition.waitFor(10, TimeUnit.SECONDS);
            latch2.countDownAndBlock();
        }
    }, Jobs.newInput().withExecutionSemaphore(semaphore).withThreadName("test-thread").withName("job-1"));
    // Test while running
    assertTrue(latch1.await());
    assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ job-1"));
    latch1.unblock();
    // Test while blocked
    JobTestUtil.waitForPermitCompetitors(semaphore, 0);
    assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ \\(WAITING_FOR_BLOCKING_CONDITION\\) job-1"));
    // Test while waiting for permit
    semaphore.withPermits(0);
    condition.setBlocking(false);
    JobTestUtil.waitForPermitCompetitors(semaphore, 1);
    assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ \\(WAITING_FOR_PERMIT\\) job-1"));
    // Test while running
    semaphore.withPermits(1);
    assertTrue(latch2.await());
    assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ job-1"));
    latch2.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 22 with IBlockingCondition

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

the class ThreadNameDecoratorTest method testThreadName.

@Test
public void testThreadName() throws InterruptedException {
    final AtomicReference<Thread> workerThread = new AtomicReference<>();
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final BlockingCountDownLatch latch1 = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch latch2 = new BlockingCountDownLatch(1);
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            workerThread.set(Thread.currentThread());
            latch1.countDownAndBlock();
            condition.waitFor(10, TimeUnit.SECONDS);
            latch2.countDownAndBlock();
        }
    }, Jobs.newInput().withExecutionSemaphore(semaphore).withThreadName("test-thread").withName("job-1"));
    // Test while running
    assertTrue(latch1.await());
    assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
    latch1.unblock();
    // Test while blocked
    JobTestUtil.waitForPermitCompetitors(semaphore, 0);
    assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
    // Test while waiting for permit
    semaphore.withPermits(0);
    condition.setBlocking(false);
    JobTestUtil.waitForPermitCompetitors(semaphore, 1);
    assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
    // Test while running
    semaphore.withPermits(1);
    assertTrue(latch2.await());
    assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
    latch2.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) Test(org.junit.Test)

Example 23 with IBlockingCondition

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

the class LookupJobHelper method awaitDone.

/**
 * await result while freeing model thread
 */
public static <T> void awaitDone(IFuture<T> futureRes) {
    final IBlockingCondition bc = Jobs.newBlockingCondition(true);
    futureRes.whenDone(new IDoneHandler<T>() {

        @Override
        public void onDone(DoneEvent<T> event) {
            bc.setBlocking(false);
        }
    }, ClientRunContexts.copyCurrent());
    bc.waitFor();
    futureRes.awaitDone();
}
Also used : IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition)

Example 24 with IBlockingCondition

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

the class LookupJobHelper method await.

/**
 * await result while freeing model thread
 */
public static <T> T await(IFuture<T> futureRes) {
    final IBlockingCondition bc = Jobs.newBlockingCondition(true);
    futureRes.whenDone(new IDoneHandler<T>() {

        @Override
        public void onDone(DoneEvent<T> event) {
            bc.setBlocking(false);
        }
    }, ClientRunContexts.copyCurrent());
    bc.waitFor();
    return futureRes.awaitDoneAndGet();
}
Also used : IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition)

Example 25 with IBlockingCondition

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

the class ClientRunContextFutureFilterTest method testBlocked.

@Test
public void testBlocked() {
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    // Client Job
    m_clientJobFuture = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            condition.waitFor(10, TimeUnit.SECONDS);
        }
    }, Jobs.newInput().withRunContext(ClientRunContexts.empty().withSession(m_clientSession1, true)));
    // Model Job
    m_modelJobFuture = ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            condition.waitFor(10, TimeUnit.SECONDS);
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)));
    JobTestUtil.waitForState(m_clientJobFuture, JobState.WAITING_FOR_BLOCKING_CONDITION);
    JobTestUtil.waitForState(m_modelJobFuture, JobState.WAITING_FOR_BLOCKING_CONDITION);
    assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).toFilter().accept(m_clientJobFuture));
    assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter().accept(m_clientJobFuture));
    assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ClientRunContext.class).andMatchNotState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter().accept(m_clientJobFuture));
    assertTrue(ModelJobs.newFutureFilterBuilder().toFilter().accept(m_modelJobFuture));
    assertTrue(ModelJobs.newFutureFilterBuilder().andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter().accept(m_modelJobFuture));
    assertFalse(ModelJobs.newFutureFilterBuilder().andMatchNotState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter().accept(m_modelJobFuture));
    // Release threads
    condition.setBlocking(false);
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Aggregations

IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)32 Test (org.junit.Test)24 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)20 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)12 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)10 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)9 ArrayList (java.util.ArrayList)8 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)4 IFuture (org.eclipse.scout.rt.platform.job.IFuture)4 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)4 Times (org.eclipse.scout.rt.testing.platform.runner.Times)4 ClientNotificationDispatcher (org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)3 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)3 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 Serializable (java.io.Serializable)2 List (java.util.List)2 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)2