Search in sources :

Example 21 with BlockingCountDownLatch

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());
}
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 22 with BlockingCountDownLatch

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());
}
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 23 with BlockingCountDownLatch

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());
}
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 24 with BlockingCountDownLatch

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

Example 25 with BlockingCountDownLatch

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