Search in sources :

Example 76 with BlockingCountDownLatch

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());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 77 with BlockingCountDownLatch

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());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 78 with BlockingCountDownLatch

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);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 79 with BlockingCountDownLatch

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();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 80 with BlockingCountDownLatch

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);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) Test(org.junit.Test)

Aggregations

BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)93 Test (org.junit.Test)89 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)66 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)30 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)14 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)12 ArrayList (java.util.ArrayList)10 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)10 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)9 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)9 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)7 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)7 Times (org.eclipse.scout.rt.testing.platform.runner.Times)7 Holder (org.eclipse.scout.rt.platform.holders.Holder)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 IMessage (org.eclipse.scout.rt.mom.api.IMessage)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)3 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)3