use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testSemaphore.
@Test
@Times(20)
public void testSemaphore() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1).seal();
// Schedule arbitrary job with same semaphore
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
}
}, Jobs.newInput().withExecutionSemaphore(mutex));
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return "abc";
}
}, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
true));
// Schedule function with same semaphore
final AtomicBoolean functionExecuted = new AtomicBoolean(false);
IFuture<Void> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, Void>() {
@Override
public Void apply(String result, Throwable error) {
functionExecuted.set(true);
return null;
}
}, Jobs.newInput().withExecutionSemaphore(mutex));
assertEquals("abc", future.awaitDoneAndGet(5, TimeUnit.SECONDS));
JobTestUtil.waitForPermitCompetitors(mutex, 2);
// Function future must not have commenced execution yet.
try {
functionFuture.awaitDone(100, TimeUnit.MILLISECONDS);
fail("timeout expected");
} catch (TimedOutError e) {
// NOOP
}
assertFalse(functionExecuted.get());
setupLatch.unblock();
functionFuture.awaitDone(5, TimeUnit.SECONDS);
assertTrue(functionExecuted.get());
assertFalse(future.isCancelled());
assertTrue(future.isDone());
assertFalse(functionFuture.isCancelled());
assertTrue(functionFuture.isDone());
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class WhenDoneTest method testCancelButStillRunning.
/**
* Tests that 'Future.whenDone' returns once the Future is cancelled, even if that job is still runnning.
*/
@Test
public void testCancelButStillRunning() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch continueRunningLatch = new BlockingCountDownLatch(1);
final IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
// ensure the thread's interrupted status to be cleared in order to continue the test.
Thread.interrupted();
// continue running
continueRunningLatch.countDownAndBlock();
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
assertTrue(setupLatch.await());
// run the test.
future.cancel(true);
// verify that whenDone immediately returns even if not completed yet.
final AtomicBoolean onDone = new AtomicBoolean(false);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
future.whenDone(new IDoneHandler<Void>() {
@Override
public void onDone(DoneEvent<Void> event) {
onDone.set(true);
verifyLatch.countDown();
}
}, RunContexts.copyCurrent());
assertTrue(verifyLatch.await());
assertTrue(onDone.get());
continueRunningLatch.release();
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class JobCancelTest method testCancelledWithEmptyRunContextAndCancellationOnMonitor.
@Test
public void testCancelledWithEmptyRunContextAndCancellationOnMonitor() {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch assertLatch = new BlockingCountDownLatch(1);
final AtomicBoolean cancelled = new AtomicBoolean();
// Run test within RunContext to ensure a current RunMonitor
RunContexts.empty().run(new IRunnable() {
@Override
public void run() throws Exception {
RunContext emptyRunContext = RunContexts.empty();
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
cancelled.set(RunMonitor.CURRENT.get().isCancelled());
assertLatch.countDown();
}
}, Jobs.newInput().withRunContext(emptyRunContext));
setupLatch.await();
emptyRunContext.getRunMonitor().cancel(false);
setupLatch.unblock();
assertLatch.await();
assertTrue("cancellation expected", cancelled.get());
}
});
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class JobCancelTest method testCancelSoft.
@Test
public void testCancelSoft() throws InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("cancelled-before");
}
try {
setupLatch.countDownAndBlock(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
protocol.add("interrupted");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("cancelled-after");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
assertTrue(setupLatch.await());
// RUN THE TEST
assertTrue(future.cancel(false));
assertTrue(verifyLatch.await());
// VERIFY
assertEquals(Arrays.asList("cancelled-after"), protocol);
assertTrue(future.isCancelled());
future.awaitDone(1, TimeUnit.SECONDS);
assertTrue(future.isCancelled());
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class JobCancelTest method testCancelChildJob.
/**
* Cancel of a job that has a child job.
*/
@Test
public void testCancelChildJob() throws Exception {
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(2);
final BlockingCountDownLatch job2DoneLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(2);
final AtomicReference<IFuture<?>> childFutureRef = new AtomicReference<>();
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
// attach to child runmonitor -> nested cancel
IFuture<?> childFuture = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-2-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-2-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-2-cancelled (monitor)");
}
job2DoneLatch.countDown();
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-2").withExceptionHandling(null, false));
childFutureRef.set(childFuture);
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-1-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-1-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-1-cancelled (monitor)");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-1"));
assertTrue(setupLatch.await());
childFutureRef.get().cancel(true);
assertTrue(job2DoneLatch.await());
setupLatch.unblock();
assertTrue(verifyLatch.await());
assertEquals(CollectionUtility.hashSet("job-2-interrupted", "job-2-cancelled (future)", "job-2-cancelled (monitor)"), protocol);
}
Aggregations