Search in sources :

Example 21 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobCancelTest method testCancelledWithDetachedRunMonitor.

@Test
public void testCancelledWithDetachedRunMonitor() {
    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(BEANS.get(RunMonitor.class))));
            setupLatch.await();
            RunMonitor.CURRENT.get().cancel(false);
            setupLatch.unblock();
            assertLatch.await();
            assertFalse(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 22 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobCancelTest method testCancelParentJob.

/**
 * Cancel of a job that has a nested job.
 */
@Test
public void testCancelParentJob() throws Exception {
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
    final BlockingCountDownLatch job1DoneLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(3);
    IFuture<Void> future1 = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            // re-use runmonitor -> nested cancel
            Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        setupLatch.countDownAndBlock();
                    } catch (InterruptedException e) {
                        protocol.add("job-2-interrupted");
                    }
                    if (IFuture.CURRENT.get().isCancelled()) {
                        protocol.add("job-2-cancelled (future)");
                    }
                    if (RunMonitor.CURRENT.get().isCancelled()) {
                        protocol.add("job-2-cancelled (monitor)");
                    }
                    verifyLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-2").withExceptionHandling(null, false));
            // does not re-use runmonitor -> no nested cancel
            Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        setupLatch.countDownAndBlock();
                    } catch (InterruptedException e) {
                        protocol.add("job-3-interrupted");
                    }
                    if (IFuture.CURRENT.get().isCancelled()) {
                        protocol.add("job-3-cancelled (future)");
                    }
                    if (RunMonitor.CURRENT.get().isCancelled()) {
                        protocol.add("job-3-cancelled (monitor)");
                    }
                    verifyLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent().withRunMonitor(new RunMonitor())).withName("job-3").withExceptionHandling(null, false));
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-1-interrupted");
            }
            if (IFuture.CURRENT.get().isCancelled()) {
                protocol.add("job-1-cancelled (future)");
            }
            if (RunMonitor.CURRENT.get().isCancelled()) {
                protocol.add("job-1-cancelled (monitor)");
            }
            job1DoneLatch.countDown();
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-1"));
    assertTrue(setupLatch.await());
    future1.cancel(true);
    assertTrue(job1DoneLatch.await());
    setupLatch.unblock();
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.hashSet("job-1-interrupted", "job-1-cancelled (future)", "job-1-cancelled (monitor)", "job-2-interrupted", "job-2-cancelled (future)", "job-2-cancelled (monitor)"), protocol);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 23 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobCancelTest method testShutdownJobManagerAndSchedule.

@Test
public void testShutdownJobManagerAndSchedule() throws InterruptedException {
    // Use dedicated job manager because job manager is shutdown in tests.
    IBean<IJobManager> jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
    });
    try {
        // synchronized because modified/read by different threads.
        final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
        final BlockingCountDownLatch latch = new BlockingCountDownLatch(2);
        Jobs.getJobManager().schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-1");
                try {
                    latch.countDownAndBlock();
                } catch (InterruptedException e) {
                    protocol.add("interrupted-1");
                } finally {
                    protocol.add("done-1");
                }
            }
        }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
        IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-2");
                try {
                    latch.countDownAndBlock();
                } catch (InterruptedException e) {
                    protocol.add("interrupted-2");
                } finally {
                    protocol.add("done-2");
                }
            }
        }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
        assertTrue(latch.await());
        // SHUTDOWN
        Jobs.getJobManager().shutdown();
        try {
            Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
            fail("AssertionError expected");
        } catch (AssertionException e) {
        // NOOP
        }
        // VERIFY
        assertEquals(CollectionUtility.hashSet("running-1", "running-2", "interrupted-1", "interrupted-2", "done-1", "done-2"), protocol);
        future2.awaitDone(1, TimeUnit.SECONDS);
        assertTrue(future2.isCancelled());
    } finally {
        JobTestUtil.unregisterAndShutdownJobManager(jobManagerBean);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) JobManager(org.eclipse.scout.rt.platform.job.internal.JobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 24 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobCancelTest method testCancelledWithNullRunContext.

@Test
public void testCancelledWithNullRunContext() {
    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(null));
            setupLatch.await();
            RunMonitor.CURRENT.get().cancel(false);
            setupLatch.unblock();
            assertLatch.await();
            assertFalse("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 25 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable 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)

Aggregations

IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)260 Test (org.junit.Test)210 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)82 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)68 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)40 ArrayList (java.util.ArrayList)36 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)26 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)21 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)20 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)20 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)17 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)13 Times (org.eclipse.scout.rt.testing.platform.runner.Times)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 IFuture (org.eclipse.scout.rt.platform.job.IFuture)10 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)10 JMSException (javax.jms.JMSException)9