use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class FutureAwaitTest method testAwaitDoneAndGetWithTimeout_Cancelled.
@Test(timeout = 5000)
public void testAwaitDoneAndGetWithTimeout_Cancelled() throws java.lang.InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Init
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Wait until ready
assertTrue(setupLatch.await());
// Run the test and verify
future.cancel(false);
try {
future.awaitDoneAndGet(10, TimeUnit.SECONDS);
fail("cancellation expected");
} catch (FutureCancelledError e) {
assertTrue(future.isCancelled());
}
setupLatch.unblock();
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class RunContextTest method testHardCancellation.
@Test
public void testHardCancellation() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
final AtomicBoolean interrupted = new AtomicBoolean();
final RunMonitor monitor = BEANS.get(RunMonitor.class);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
RunContexts.empty().withRunMonitor(monitor).run(new IRunnable() {
@Override
public void run() throws Exception {
try {
assertTrue(setupLatch.countDownAndBlock(10, TimeUnit.SECONDS));
} catch (InterruptedException e) {
interrupted.set(true);
} finally {
verifyLatch.countDown();
}
}
});
}
}, Jobs.newInput());
assertTrue(setupLatch.await());
monitor.cancel(true);
assertTrue(verifyLatch.await());
assertTrue(interrupted.get());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class InvocationContextTest method testCancel.
@Test(timeout = 5000)
public void testCancel() {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final IBlockingCondition processingCondition = Jobs.newBlockingCondition(true);
final InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
invocationContext.withEndpointUrl("http://localhost");
// Make Stub.webMethod to block until cancelled.
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
setupLatch.countDown();
processingCondition.waitForUninterruptibly(10, TimeUnit.SECONDS);
return null;
}
}).when(m_port).webMethod();
final RunMonitor runMonitor = new RunMonitor();
// Cancel the 'webMethod' once blocking.
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.await();
runMonitor.cancel(true);
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Run the test by invoking the web service with a specific RunMonitor to test cancellation.
try {
RunContexts.empty().withRunMonitor(runMonitor).run(new IRunnable() {
@Override
public void run() throws Exception {
try {
// this method blocks until cancelled.
invocationContext.getPort().webMethod();
fail("WebServiceRequestCancelledException expected");
} catch (WebServiceRequestCancelledException e) {
verify(m_implementorSpecifics).closeSocket(same(m_port), anyString());
}
}
});
} finally {
processingCondition.setBlocking(false);
}
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class MultipleSessionTest method testCancel.
@Test
public void testCancel() throws InterruptedException {
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch setupLatch1 = new BlockingCountDownLatch(2);
final BlockingCountDownLatch setupLatch2 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch interruptedJob1_S1_Latch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch awaitAllCancelledLatch = new BlockingCountDownLatch(1);
// Session 1 (job1)
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job1-S1");
try {
setupLatch1.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job1-S1-interrupted");
} finally {
interruptedJob1_S1_Latch.countDown();
}
// ensure the thread's interrupted status to be cleared in order to continue the test.
Thread.interrupted();
awaitAllCancelledLatch.await();
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-1-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// Session 1 (job2) --> never starts running because cancelled while job1 is mutex-owner
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job2-S1");
try {
setupLatch2.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job2-S1-interrupted");
}
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-2-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// Session 2 (job1)
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job1-S2");
try {
setupLatch1.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job1-S2-interrupted");
}
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-1-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// Session 2 (job2)
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job2-S2");
try {
setupLatch2.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job2-S2-interrupted");
}
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-2-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
assertTrue(setupLatch1.await());
assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S2"), protocol);
Jobs.getJobManager().cancel(ModelJobs.newFutureFilterBuilder().andMatch(new SessionFutureFilter(m_clientSession1)).toFilter(), // cancels job-1-S1 and job-2-S1, meaning that job-2-S1 never starts running.
true);
awaitAllCancelledLatch.unblock();
assertTrue(interruptedJob1_S1_Latch.await());
setupLatch1.unblock();
assertTrue(setupLatch2.await());
assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S1-interrupted", "job1-S2", "job2-S2"), protocol);
setupLatch2.unblock();
// Wait until all jobs completed
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class MultipleSessionTest method testMutalExclusion.
@Test
public void testMutalExclusion() throws InterruptedException {
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch latch1 = new BlockingCountDownLatch(2);
final BlockingCountDownLatch latch2 = new BlockingCountDownLatch(2);
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job1-S1");
latch1.countDownAndBlock();
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-1-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job2-S1");
latch2.countDownAndBlock();
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-2-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job1-S2");
latch1.countDownAndBlock();
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-1-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job2-S2");
latch2.countDownAndBlock();
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-2-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
assertTrue(latch1.await());
assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S2"), protocol);
latch1.unblock();
assertTrue(latch2.await());
assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S2", "job2-S1", "job2-S2"), protocol);
latch2.unblock();
// Wait until all jobs completed
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
}
Aggregations