Search in sources :

Example 26 with ThreadInterruptedError

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

the class WhenDoneScheduleTest method testExecutionHints.

@Test
public void testExecutionHints() throws InterruptedException {
    // Schedule future
    IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "abc";
        }
    }, Jobs.newInput().withExecutionHint(JOB_MARKER));
    final BlockingCountDownLatch hint1AddedLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch hint1RemovedLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch hint2AddedLach = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch hint2RemovedLatch = new BlockingCountDownLatch(1);
    // Schedule function
    IFuture<String> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, String>() {

        @Override
        public String apply(String result, Throwable error) {
            assertTrue(IFuture.CURRENT.get().containsExecutionHint(JOB_MARKER));
            try {
                // Make hint changes to the future
                IFuture.CURRENT.get().addExecutionHint("HINT1");
                hint1AddedLatch.countDownAndBlock();
                IFuture.CURRENT.get().removeExecutionHint("HINT1");
                hint1RemovedLatch.countDownAndBlock();
                // Verify that external hint changes are reflected
                assertTrue(hint2AddedLach.await());
                assertTrue(IFuture.CURRENT.get().containsExecutionHint("HINT2"));
                hint2AddedLach.unblock();
                assertTrue(hint2RemovedLatch.await());
                assertFalse(IFuture.CURRENT.get().containsExecutionHint("HINT2"));
                hint2RemovedLatch.unblock();
            } catch (InterruptedException e) {
                throw new ThreadInterruptedError("", e);
            }
            return result.toUpperCase();
        }
    }, Jobs.newInput().withExecutionHint(JOB_MARKER));
    try {
        assertTrue(functionFuture.containsExecutionHint(JOB_MARKER));
        assertTrue(hint1AddedLatch.await());
        // Verify that internal hint changes are reflected
        assertTrue(functionFuture.containsExecutionHint("HINT1"));
        hint1AddedLatch.unblock();
        assertTrue(hint1RemovedLatch.await());
        assertFalse(functionFuture.containsExecutionHint("HINT1"));
        hint1RemovedLatch.unblock();
        // Make hint changes to the future
        functionFuture.addExecutionHint("HINT2");
        assertTrue(hint2AddedLach.countDownAndBlock());
        functionFuture.removeExecutionHint("HINT2");
        assertTrue(hint2RemovedLatch.countDownAndBlock());
        assertEquals("abc", future.awaitDoneAndGet());
        assertEquals("ABC", functionFuture.awaitDoneAndGet());
    } finally {
        Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), true);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) Test(org.junit.Test)

Example 27 with ThreadInterruptedError

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

the class WhenDoneScheduleTest method testPostCancellation.

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

        @Override
        public String call() throws Exception {
            return "abc";
        }
    }, Jobs.newInput().withExecutionHint(JOB_MARKER));
    // Schedule function
    IFuture<String> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, String>() {

        @Override
        public String apply(String result, Throwable error) {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                throw new ThreadInterruptedError("", e);
            }
            return result.toUpperCase();
        }
    }, Jobs.newInput().withExecutionHint(JOB_MARKER));
    assertTrue(setupLatch.await());
    // Cancel future which already completed
    future.cancel(true);
    setupLatch.unblock();
    assertEquals("abc", future.awaitDoneAndGet());
    assertEquals("ABC", functionFuture.awaitDoneAndGet());
    assertFalse(future.isCancelled());
    assertFalse(functionFuture.isCancelled());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) Test(org.junit.Test)

Example 28 with ThreadInterruptedError

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

the class FutureAwaitTest method testAwaitDoneAndGet_Interrupted.

@Test(timeout = 5000)
public void testAwaitDoneAndGet_Interrupted() throws java.lang.InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    // Init
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Run the test in a separate thread
    IFuture<Void> controller = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            Thread.currentThread().interrupt();
            try {
                future.awaitDoneAndGet();
                fail("interruption expected");
            } catch (ThreadInterruptedError e) {
                assertTrue(Thread.currentThread().isInterrupted());
            }
        }
    }, Jobs.newInput());
    controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
    setupLatch.unblock();
    future.awaitDone(10, TimeUnit.SECONDS);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 29 with ThreadInterruptedError

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

the class FutureAwaitTest method testJobManagerAwaitDone_Interrupted.

// (timeout = 5000)
@Test
public void testJobManagerAwaitDone_Interrupted() throws java.lang.InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    // Init
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Run the test in a separate thread
    IFuture<Void> controller = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            Thread.currentThread().interrupt();
            try {
                Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 10, TimeUnit.SECONDS);
                fail("interruption expected");
            } catch (ThreadInterruptedError e) {
                assertTrue(Thread.currentThread().isInterrupted());
            }
        }
    }, Jobs.newInput());
    controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
    setupLatch.unblock();
    future.awaitDone(10, TimeUnit.SECONDS);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 30 with ThreadInterruptedError

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

the class FutureAwaitTest method testAwaitDone_Interrupted.

@Test(timeout = 5000)
public void testAwaitDone_Interrupted() throws java.lang.InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    // Init
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Run the test in a separate thread
    IFuture<Void> controller = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            Thread.currentThread().interrupt();
            try {
                future.awaitDone();
                fail("interruption expected");
            } catch (ThreadInterruptedError e) {
                assertTrue(Thread.currentThread().isInterrupted());
            }
        }
    }, Jobs.newInput());
    controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
    setupLatch.unblock();
    future.awaitDone(10, TimeUnit.SECONDS);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Aggregations

ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)36 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)20 Test (org.junit.Test)14 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)13 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)8 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)7 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)5 ArrayList (java.util.ArrayList)4 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)4 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)3 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)3 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 JMSException (javax.jms.JMSException)2 NamingException (javax.naming.NamingException)2 IClientSession (org.eclipse.scout.rt.client.IClientSession)2 IMessageBox (org.eclipse.scout.rt.client.ui.messagebox.IMessageBox)2 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)2 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)2