Search in sources :

Example 81 with BlockingCountDownLatch

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

the class ExecutionHintTest method testHintChangeWhileRunning.

@Test
public void testHintChangeWhileRunning() {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
    final String hint = "USER-INTERACTION-REQUIRED";
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("a");
            IFuture.CURRENT.get().addExecutionHint(hint);
            latch.countDownAndBlock();
            protocol.add("c");
        }
    }, Jobs.newInput());
    // Wait until the job completes, or until tagged as 'USER-INTERACTION-REQUIRED'
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).andMatchNotExecutionHint(hint).toFilter(), 10, TimeUnit.SECONDS);
    protocol.add("b");
    latch.unblock();
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).andMatchExecutionHint(hint).toFilter(), 10, TimeUnit.SECONDS);
    assertEquals(Arrays.asList("a", "b", "c"), protocol);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 82 with BlockingCountDownLatch

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

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

the class FutureAwaitTest method testAwaitDoneAndGet_Cancelled.

@Test(timeout = 5000)
public void testAwaitDoneAndGet_Cancelled() 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()));
    // Wait until ready
    assertTrue(setupLatch.await());
    // Run the test and verify
    future.cancel(false);
    try {
        future.awaitDoneAndGet();
        fail("cancellation expected");
    } catch (FutureCancelledError e) {
        assertTrue(future.isCancelled());
    }
    setupLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) Test(org.junit.Test)

Example 84 with BlockingCountDownLatch

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

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

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