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));
}
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));
}
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");
}
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");
}
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();
}
Aggregations