Search in sources :

Example 16 with BlockingCountDownLatch

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

the class JobCancelTest method testCancelChildJob.

/**
 * Cancel of a job that has a child job.
 */
@Test
public void testCancelChildJob() throws Exception {
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(2);
    final BlockingCountDownLatch job2DoneLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(2);
    final AtomicReference<IFuture<?>> childFutureRef = new AtomicReference<>();
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            // attach to child runmonitor -> nested cancel
            IFuture<?> childFuture = 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)");
                    }
                    job2DoneLatch.countDown();
                    verifyLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-2").withExceptionHandling(null, false));
            childFutureRef.set(childFuture);
            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)");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-1"));
    assertTrue(setupLatch.await());
    childFutureRef.get().cancel(true);
    assertTrue(job2DoneLatch.await());
    setupLatch.unblock();
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.hashSet("job-2-interrupted", "job-2-cancelled (future)", "job-2-cancelled (monitor)"), protocol);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 17 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch 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 18 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch 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 19 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch 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 20 with BlockingCountDownLatch

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

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