Search in sources :

Example 1 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class WhenDoneScheduleTest method testCancellationOfSharedRunMonitor.

@Test
public void testCancellationOfSharedRunMonitor() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    RunMonitor sharedRunMonitor = BEANS.get(RunMonitor.class);
    // Schedule future
    IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "abc";
        }
    }, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
    true).withRunContext(RunContexts.empty().withRunMonitor(sharedRunMonitor)).withExecutionHint(JOB_MARKER));
    // Schedule function
    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().withRunContext(RunContexts.empty().withRunMonitor(sharedRunMonitor)).withExecutionHint(JOB_MARKER));
    assertTrue(setupLatch.await());
    sharedRunMonitor.cancel(true);
    // Wait until the job jobs are done
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), 5, TimeUnit.SECONDS);
    // Verify that the function is not executed
    assertFalse(functionExecuted.get());
    // Verify that both futures are cancelled
    assertFutureCancelled(future);
    assertFutureCancelled(functionFuture);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) Test(org.junit.Test)

Example 2 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class WhenDoneScheduleTest method testCancellationOfFutureRunMonitor.

@Test
public void testCancellationOfFutureRunMonitor() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    RunMonitor runMonitor = BEANS.get(RunMonitor.class);
    // Schedule future
    IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "abc";
        }
    }, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
    true).withRunContext(RunContexts.empty().withRunMonitor(runMonitor)).withExecutionHint(JOB_MARKER));
    // Schedule function
    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().withExecutionHint(JOB_MARKER));
    assertTrue(setupLatch.await());
    runMonitor.cancel(true);
    // Wait until the job jobs are done
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), 5, TimeUnit.SECONDS);
    // Verify that the function is not executed
    assertFalse(functionExecuted.get());
    // Verify that both futures are cancelled
    assertFutureCancelled(future);
    assertFutureCancelled(functionFuture);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) Test(org.junit.Test)

Example 3 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class WhenDoneScheduleTest method testCancellationOfFunctionRunMonitor.

@Test
public void testCancellationOfFunctionRunMonitor() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    // Schedule future
    IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "abc";
        }
    }, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
    true).withExecutionHint(JOB_MARKER));
    // Schedule function
    final AtomicBoolean functionExecuted = new AtomicBoolean(false);
    RunMonitor functionRunMonitor = BEANS.get(RunMonitor.class);
    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().withRunContext(RunContexts.empty().withRunMonitor(functionRunMonitor)).withExecutionHint(JOB_MARKER));
    assertTrue(setupLatch.await());
    functionRunMonitor.cancel(true);
    setupLatch.unblock();
    // Wait until the job jobs are done
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), 5, TimeUnit.SECONDS);
    // Verify that the function is not executed
    assertFalse(functionExecuted.get());
    // Verify that future is not cancelled
    assertEquals("abc", future.awaitDoneAndGet());
    assertFalse(future.isCancelled());
    assertTrue(future.isDone());
    // Verify that function future is cancelled
    assertFutureCancelled(functionFuture);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) Test(org.junit.Test)

Example 4 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor 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 5 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class AbstractServiceTunnel method checkAlreadyCancelled.

/**
 * Will throw a CancellationException if the future is already cancelled.
 *
 * @throws ThreadInterruptedError
 *           if the current thread is cancelled
 */
protected void checkAlreadyCancelled(ServiceTunnelRequest serviceRequest) {
    final RunMonitor monitor = RunMonitor.CURRENT.get();
    if (monitor != null && monitor.isCancelled()) {
        final StringBuilder cancellationExceptionText = new StringBuilder();
        cancellationExceptionText.append("RunMonitor is already cancelled.");
        if (serviceRequest != null) {
            cancellationExceptionText.append(" (Request was '");
            cancellationExceptionText.append(serviceRequest.getServiceInterfaceClassName());
            cancellationExceptionText.append(".");
            cancellationExceptionText.append(serviceRequest.getOperation());
            cancellationExceptionText.append("(..)')");
        }
        throw new ThreadInterruptedError(cancellationExceptionText.toString());
    }
}
Also used : RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)

Aggregations

RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)12 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)7 Test (org.junit.Test)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)2 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2 Callable (java.util.concurrent.Callable)1 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 JobInput (org.eclipse.scout.rt.platform.job.JobInput)1 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)1 IPingService (org.eclipse.scout.rt.shared.services.common.ping.IPingService)1 JSONObject (org.json.JSONObject)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1