Search in sources :

Example 26 with IBlockingCondition

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

the class NotificationDispatcherTest method testStringNotification.

@Test
public void testStringNotification() {
    final IBlockingCondition cond = Jobs.newBlockingCondition(true);
    final String stringNotification = "A simple string notification";
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            final ClientNotificationDispatcher dispatcher = BEANS.get(ClientNotificationDispatcher.class);
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), stringNotification, mock(ClientNotificationAddress.class));
            waitForPendingNotifications(dispatcher);
        }
    }, Jobs.newInput().withRunContext(ClientRunContexts.copyCurrent())).whenDone(new IDoneHandler<Void>() {

        @Override
        public void onDone(DoneEvent<Void> event) {
            cond.setBlocking(false);
        }
    }, null);
    cond.waitFor();
    Mockito.verify(m_globalNotificationHanlder, Mockito.times(1)).handleNotification(Mockito.any(Serializable.class));
    Mockito.verify(m_groupNotificationHanlder, Mockito.times(0)).handleNotification(Mockito.any(INotificationGroup.class));
}
Also used : Serializable(java.io.Serializable) ClientNotificationAddress(org.eclipse.scout.rt.shared.clientnotification.ClientNotificationAddress) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher) Test(org.junit.Test)

Example 27 with IBlockingCondition

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

the class NotificationDispatcherTest method testSuperClassNotification.

@Test
public void testSuperClassNotification() {
    final IBlockingCondition cond = Jobs.newBlockingCondition(true);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            ClientNotificationDispatcher dispatcher = BEANS.get(ClientNotificationDispatcher.class);
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), new Notification01(), mock(ClientNotificationAddress.class));
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), new Notification02(), mock(ClientNotificationAddress.class));
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), new Notification02(), mock(ClientNotificationAddress.class));
            waitForPendingNotifications(dispatcher);
        }
    }, Jobs.newInput().withRunContext(ClientRunContexts.copyCurrent())).whenDone(new IDoneHandler<Void>() {

        @Override
        public void onDone(DoneEvent<Void> event) {
            cond.setBlocking(false);
        }
    }, null);
    cond.waitFor();
    Mockito.verify(m_globalNotificationHanlder, Mockito.times(3)).handleNotification(Mockito.any(Serializable.class));
    Mockito.verify(m_groupNotificationHanlder, Mockito.times(2)).handleNotification(Mockito.any(INotificationGroup.class));
}
Also used : Serializable(java.io.Serializable) ClientNotificationAddress(org.eclipse.scout.rt.shared.clientnotification.ClientNotificationAddress) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher) Test(org.junit.Test)

Example 28 with IBlockingCondition

use of org.eclipse.scout.rt.platform.job.IBlockingCondition 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 29 with IBlockingCondition

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

the class MutualExclusionTest method testReuseUnblockedBlockingCondition.

/**
 * Job1 gets scheduled and enters blocking condition. If waiting for the condition to fall, the main thread
 * invalidates the condition. Afterwards, another job gets scheduled which should not wait for the blocking condition
 * to fall because still invalidated.
 *
 * @throws Throwable
 */
@Test
public void testReuseUnblockedBlockingCondition() {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("1: beforeWaitFor");
            condition.waitFor();
            protocol.add("3: afterWaitFor");
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
    // Wait until job1 is blocked
    JobTestUtil.waitForPermitCompetitors(m_clientSession.getModelJobSemaphore(), 0);
    assertTrue(condition.isBlocking());
    protocol.add("2: setBlocking=false");
    condition.setBlocking(false);
    assertFalse(condition.isBlocking());
    awaitDoneElseFail(JOB_IDENTIFIER);
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("4: beforeWaitFor");
            condition.waitFor();
            protocol.add("5: afterWaitFor");
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent())).awaitDoneAndGet();
    List<String> expected = new ArrayList<>();
    expected.add("1: beforeWaitFor");
    expected.add("2: setBlocking=false");
    expected.add("3: afterWaitFor");
    expected.add("4: beforeWaitFor");
    expected.add("5: afterWaitFor");
}
Also used : ArrayList(java.util.ArrayList) 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)

Example 30 with IBlockingCondition

use of org.eclipse.scout.rt.platform.job.IBlockingCondition 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)

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