Search in sources :

Example 26 with BlockingCountDownLatch

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

the class JobScheduleTest method testScheduleWithTimeoutWithMutex.

@Test
public void testScheduleWithTimeoutWithMutex() {
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
    IFuture<String> future1 = Jobs.getJobManager().schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            latch.countDownAndBlock();
            return "job-1";
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withExecutionSemaphore(mutex).withExceptionHandling(null, false));
    IFuture<String> future2 = Jobs.getJobManager().schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "job-2";
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withExecutionSemaphore(mutex));
    try {
        assertEquals("job-2", future2.awaitDoneAndGet(2, TimeUnit.SECONDS));
        fail("TimeoutException expected");
    } catch (TimedOutError e) {
    // NOOP
    }
    latch.unblock();
    // wait for all jobs to complete
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future1, future2).toFilter(), 10, TimeUnit.SECONDS);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 27 with BlockingCountDownLatch

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

the class BlockingConditionTest method testHintsUponInterruption.

@Test
public void testHintsUponInterruption() throws Throwable {
    final IBlockingCondition blockingCondition = Jobs.newBlockingCondition(true);
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final P_ExceptionCapturer exceptionCapturer = new P_ExceptionCapturer();
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            setupLatch.countDown();
            try {
                blockingCondition.waitFor(10, TimeUnit.SECONDS, "hint-blocking");
                fail("ThreadInterruptedError expected");
            } catch (ThreadInterruptedError e) {
                assertFalse("hint not unset", IFuture.CURRENT.get().containsExecutionHint("hint-blocking"));
            }
        }
    }, Jobs.newInput().withExceptionHandling(exceptionCapturer, true));
    setupLatch.await();
    future.cancel(true);
    future.awaitFinished(10, TimeUnit.SECONDS);
    exceptionCapturer.throwOnError();
}
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 28 with BlockingCountDownLatch

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

the class ExecutionHintTest method testWaitingForTaggedJobs.

@Test
public void testWaitingForTaggedJobs() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(4);
    final BlockingCountDownLatch finishLatch = new BlockingCountDownLatch(3);
    // job-1
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-1-interrupted");
                finishLatch.countDown();
            }
        }
    }, Jobs.newInput().withName("job-1").withExecutionHint("UI-JOB"));
    // job-2
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-2-interrupted");
                finishLatch.countDown();
            }
        }
    }, Jobs.newInput().withName("job-2").withExecutionHint("UI-JOB"));
    // job-3
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-3-interrupted");
            }
        }
    }, Jobs.newInput().withName("job-3").withExecutionHint("COMPUTATION-JOB"));
    // job-4
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                IFuture.CURRENT.get().addExecutionHint("UI-JOB");
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-4-interrupted");
                finishLatch.countDown();
            }
        }
    }, Jobs.newInput().withName("job-4").withExecutionHint("COMPUTATION-JOB"));
    assertTrue(setupLatch.await());
    // cancel all jobs tagged as 'UI-JOB'. That should be job1, job2, and job3
    Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchExecutionHint("UI-JOB").toFilter(), true);
    assertTrue(finishLatch.await());
    Set<String> expected = new HashSet<>();
    expected.add("job-1-interrupted");
    expected.add("job-2-interrupted");
    expected.add("job-4-interrupted");
    assertEquals(expected, protocol);
    setupLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with BlockingCountDownLatch

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

the class FutureAwaitTest method testAwaitDoneWithTimeout_Interrupted.

@Test(timeout = 5000)
public void testAwaitDoneWithTimeout_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(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 BlockingCountDownLatch

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

the class FutureAwaitTest method testAwaitDoneWithTimeout_Timeout.

@Test(timeout = 5000)
public void testAwaitDoneWithTimeout_Timeout() 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
    try {
        future.awaitDone(5, TimeUnit.MILLISECONDS);
        fail("timeout expected");
    } catch (TimedOutError e) {
    // NOOP
    }
    setupLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) 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