use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelForce.
@Test
public void testCancelForce() 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();
} catch (InterruptedException e) {
protocol.add("interrupted");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("cancelled-after");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
assertTrue(setupLatch.await());
// RUN THE TEST
assertTrue(future.cancel(true));
assertTrue(verifyLatch.await());
// VERIFY
assertTrue(future.isCancelled());
assertEquals(Arrays.asList("interrupted", "cancelled-after"), protocol);
future.awaitDone(5, TimeUnit.SECONDS);
assertTrue(future.isCancelled());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobListenerTest method testLocalListener5.
@Test
public void testLocalListener5() throws InterruptedException {
final BlockingCountDownLatch jobRunningLatch = new BlockingCountDownLatch(1);
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
jobRunningLatch.countDownAndBlock();
}
}, Jobs.newInput());
assertTrue(jobRunningLatch.await());
JobEventCaptureListener captureListener = new JobEventCaptureListener();
future.addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.DONE).toFilter(), captureListener);
jobRunningLatch.unblock();
future.awaitDone();
// verify events
int i = -1;
List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
i++;
assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
assertEquals(i + 1, capturedEvents.size());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobListenerTest method testLocalListener3.
@Test
public void testLocalListener3() throws InterruptedException {
IFuture<Void> future1 = Jobs.getJobManager().schedule(mock(IRunnable.class), Jobs.newInput());
future1.awaitDone();
final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
job2RunningLatch.countDownAndBlock();
}
}, Jobs.newInput());
assertTrue(job2RunningLatch.await());
JobEventCaptureListener captureListener = new JobEventCaptureListener();
Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchNotFuture(future2).toFilter(), captureListener);
job2RunningLatch.unblock();
future2.awaitDone();
// verify events
assertTrue(captureListener.getCapturedEvents().isEmpty());
assertTrue(captureListener.getCapturedFutureStates().isEmpty());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobManagerTest method testShutdown.
@Test
public void testShutdown() throws Exception {
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(3);
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("interrupted-1");
} finally {
verifyLatch.countDown();
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("interrupted-2");
} finally {
verifyLatch.countDown();
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("interrupted-3");
} finally {
verifyLatch.countDown();
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
assertTrue(setupLatch.await());
// RUN THE TEST
Jobs.getJobManager().shutdown();
// VERIFY
assertTrue(verifyLatch.await());
assertEquals(CollectionUtility.hashSet("interrupted-1", "interrupted-2", "interrupted-3"), protocol);
try {
Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
fail("AssertionError expected");
} catch (AssertionException e) {
// NOOP
}
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobScheduleTest method testParallelExecution.
@Test
public void testParallelExecution() throws Exception {
final BlockingCountDownLatch barrier = new BlockingCountDownLatch(3);
IFuture<Void> future1 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
barrier.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
barrier.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
IFuture<Void> future3 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
barrier.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
assertTrue(barrier.await());
barrier.unblock();
// wait for all jobs to complete
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future1, future2, future3).toFilter(), 10, TimeUnit.SECONDS);
}
Aggregations