Search in sources :

Example 16 with IRunnable

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

the class WhenDoneScheduleTest method testSemaphore.

@Test
@Times(20)
public void testSemaphore() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1).seal();
    // Schedule arbitrary job with same semaphore
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            setupLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withExecutionSemaphore(mutex));
    // Schedule future
    IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "abc";
        }
    }, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
    true));
    // Schedule function with same semaphore
    final AtomicBoolean functionExecuted = new AtomicBoolean(false);
    IFuture<Void> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, Void>() {

        @Override
        public Void apply(String result, Throwable error) {
            functionExecuted.set(true);
            return null;
        }
    }, Jobs.newInput().withExecutionSemaphore(mutex));
    assertEquals("abc", future.awaitDoneAndGet(5, TimeUnit.SECONDS));
    JobTestUtil.waitForPermitCompetitors(mutex, 2);
    // Function future must not have commenced execution yet.
    try {
        functionFuture.awaitDone(100, TimeUnit.MILLISECONDS);
        fail("timeout expected");
    } catch (TimedOutError e) {
    // NOOP
    }
    assertFalse(functionExecuted.get());
    setupLatch.unblock();
    functionFuture.awaitDone(5, TimeUnit.SECONDS);
    assertTrue(functionExecuted.get());
    assertFalse(future.isCancelled());
    assertTrue(future.isDone());
    assertFalse(functionFuture.isCancelled());
    assertTrue(functionFuture.isDone());
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 17 with IRunnable

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

the class WhenDoneTest method testCancelButStillRunning.

/**
 * Tests that 'Future.whenDone' returns once the Future is cancelled, even if that job is still runnning.
 */
@Test
public void testCancelButStillRunning() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch continueRunningLatch = new BlockingCountDownLatch(1);
    final IFuture<Void> future = Jobs.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().withRunContext(RunContexts.copyCurrent()));
    assertTrue(setupLatch.await());
    // run the test.
    future.cancel(true);
    // verify that whenDone immediately returns even if not completed yet.
    final AtomicBoolean onDone = new AtomicBoolean(false);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    future.whenDone(new IDoneHandler<Void>() {

        @Override
        public void onDone(DoneEvent<Void> event) {
            onDone.set(true);
            verifyLatch.countDown();
        }
    }, RunContexts.copyCurrent());
    assertTrue(verifyLatch.await());
    assertTrue(onDone.get());
    continueRunningLatch.release();
}
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) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Test(org.junit.Test)

Example 18 with IRunnable

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

the class JobCancelTest method testCancelledWithEmptyRunContextAndCancellationOnMonitor.

@Test
public void testCancelledWithEmptyRunContextAndCancellationOnMonitor() {
    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 {
            RunContext emptyRunContext = RunContexts.empty();
            Jobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    setupLatch.countDownAndBlock();
                    cancelled.set(RunMonitor.CURRENT.get().isCancelled());
                    assertLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(emptyRunContext));
            setupLatch.await();
            emptyRunContext.getRunMonitor().cancel(false);
            setupLatch.unblock();
            assertLatch.await();
            assertTrue("cancellation expected", cancelled.get());
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunContext(org.eclipse.scout.rt.platform.context.RunContext) 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 IRunnable

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

the class JobCancelTest method testCancelSoft.

@Test
public void testCancelSoft() 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(2, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                protocol.add("interrupted");
            }
            if (RunMonitor.CURRENT.get().isCancelled()) {
                protocol.add("cancelled-after");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    assertTrue(setupLatch.await());
    // RUN THE TEST
    assertTrue(future.cancel(false));
    assertTrue(verifyLatch.await());
    // VERIFY
    assertEquals(Arrays.asList("cancelled-after"), protocol);
    assertTrue(future.isCancelled());
    future.awaitDone(1, 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 20 with IRunnable

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

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