use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobListenerTest method testLocalListener2a.
@Test
public void testLocalListener2a() throws InterruptedException {
// Schedule job-1
IFuture<Void> future1 = Jobs.getJobManager().schedule(mock(IRunnable.class), Jobs.newInput());
future1.awaitDone();
// schedule job-2, and install listener once started running
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());
// install listener
JobEventCaptureListener captureListener = new JobEventCaptureListener();
Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchFuture(future1, future2).toFilter(), captureListener);
job2RunningLatch.unblock();
future2.awaitDone();
// verify events
int i = -1;
List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
i++;
assertStateChangedEvent(future2, 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 testLocalListener4.
@Test
public void testLocalListener4() 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(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 JobManagerTest method testVisit.
@Test
public void testVisit() throws Exception {
final BlockingCountDownLatch latch = new BlockingCountDownLatch(3);
IFuture<Void> future1 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
latch.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
latch.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
IFuture<Void> future3 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
latch.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
assertTrue(latch.await());
// RUN THE TEST
Set<IFuture<?>> futures = Jobs.getJobManager().getFutures(Jobs.newFutureFilterBuilder().andMatchFuture(future1, future2, future3).toFilter());
// VERIFY
assertEquals(CollectionUtility.hashSet(future1, future2, future3), futures);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class AwaitDoneTest method testAwaitDoneWithCancelledJob.
/**
* Tests that 'JobManager.awaitDone' returns once the Future is cancelled, even if that job is still runnning.
*/
@Test
public void testAwaitDoneWithCancelledJob() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch continueRunningLatch = new BlockingCountDownLatch(1);
final IFuture<Void> future = Jobs.getJobManager().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());
assertTrue(setupLatch.await());
// cancel the Future in 1 second
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
// run the test.
future.cancel(true);
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.SECONDS)));
// start waiting for the job to complete or until cancelled
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 5, TimeUnit.SECONDS);
continueRunningLatch.release();
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class AwaitDoneTest method testAwaitFutureDone2.
@Test
public void testAwaitFutureDone2() {
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch latchJob2 = new BlockingCountDownLatch(1);
final IFuture<Void> future1 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("run-1");
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
latchJob2.await();
protocol.add("run-2");
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future1).toFilter(), 30, TimeUnit.SECONDS);
assertTrue(Jobs.getJobManager().isDone(Jobs.newFutureFilterBuilder().andMatchFuture(future1).toFilter()));
assertFalse(Jobs.getJobManager().isDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter()));
try {
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 500, TimeUnit.MILLISECONDS);
fail("timeout expected");
} catch (TimedOutError e) {
// NOOP
}
assertEquals(CollectionUtility.hashSet("run-1"), protocol);
latchJob2.countDown();
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 30, TimeUnit.SECONDS);
assertTrue(Jobs.getJobManager().isDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter()));
assertEquals(CollectionUtility.hashSet("run-1", "run-2"), protocol);
}
Aggregations