Search in sources :

Example 66 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class FutureFinishedTest method testNotifyWaitingThreads.

@Test
public void testNotifyWaitingThreads() throws Throwable {
    final String jobIdentifier = UUID.randomUUID().toString();
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    IFuture<Void> controller = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            setupLatch.countDown();
            try {
                Jobs.getJobManager().awaitFinished(Jobs.newFutureFilterBuilder().andMatchExecutionHint(jobIdentifier).toFilter(), 10, TimeUnit.SECONDS);
            } catch (TimedOutError e) {
                fail("no timeout expected");
            }
        }
    }, Jobs.newInput());
    assertTrue(setupLatch.await());
    // Wait some time, so that the listener in controller is installed
    SleepUtil.sleepSafe(2, TimeUnit.SECONDS);
    // Run the test
    IFuture<Void> future = Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
    // verify
    // no exception expected
    future.awaitFinished(10, TimeUnit.SECONDS);
    // no exception expected
    controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
}
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)

Example 67 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobManagerTest method testUnregisterWhenCancelledDuringExecution.

@Test
public void testUnregisterWhenCancelledDuringExecution() throws InterruptedException {
    final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            latch.countDownAndBlock();
        }
    }, Jobs.newInput());
    latch.await();
    future.cancel(false);
    latch.unblock();
    future.awaitFinished(10, TimeUnit.SECONDS);
    // Assert no futures left
    JobManager jobManager = (JobManager) Jobs.getJobManager();
    assertEquals(0, jobManager.getFutures(null).size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) Test(org.junit.Test)

Example 68 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobCancelTest method testCancelledWithCurrentRunContext.

@Test
public void testCancelledWithCurrentRunContext() {
    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 {
            Jobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    setupLatch.countDownAndBlock();
                    cancelled.set(RunMonitor.CURRENT.get().isCancelled());
                    assertLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
            setupLatch.await();
            RunMonitor.CURRENT.get().cancel(false);
            setupLatch.unblock();
            assertLatch.await();
            assertTrue("nested cancellation expected", cancelled.get());
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 69 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobCancelTest method testCancelledWithEmptyRunContextAndCurrentMonitor.

@Test
public void testCancelledWithEmptyRunContextAndCurrentMonitor() {
    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 {
            Jobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    setupLatch.countDownAndBlock();
                    cancelled.set(RunMonitor.CURRENT.get().isCancelled());
                    assertLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.empty().withRunMonitor(RunMonitor.CURRENT.get())));
            setupLatch.await();
            RunMonitor.CURRENT.get().cancel(false);
            setupLatch.unblock();
            assertLatch.await();
            assertTrue("no nested cancellation expected", cancelled.get());
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 70 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobCancelTest method testCancelPeriodicAction.

@Test
public void testCancelPeriodicAction() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    final IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            if (count.incrementAndGet() == 3) {
                setupLatch.countDown();
                verifyLatch.await();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withExceptionHandling(null, false).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(10).repeatForever())));
    assertTrue(setupLatch.await());
    // RUN THE TEST
    future.cancel(false);
    verifyLatch.countDown();
    // VERIFY
    assertTrue(future.isCancelled());
    assertEquals(3, count.get());
    future.awaitDone(10, TimeUnit.SECONDS);
    assertTrue(future.isCancelled());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) 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