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