Search in sources :

Example 6 with BlockingCountDownLatch

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

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

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

the class WhenDoneTest method testError.

@Test
public void testError() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final Holder<DoneEvent<String>> eventHolder = new Holder<>();
    final ProcessingException pe = new ProcessingException("expected JUnit test exception");
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            protocol.add("1");
            throw pe;
        }
    }, Jobs.newInput().withExceptionHandling(null, false).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.SECONDS)));
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    future.whenDone(new IDoneHandler<String>() {

        @Override
        public void onDone(DoneEvent<String> event) {
            protocol.add("2");
            if (future.isDone()) {
                protocol.add("done");
            }
            if (future.isCancelled()) {
                protocol.add("cancelled");
            }
            eventHolder.setValue(event);
            verifyLatch.countDown();
        }
    }, RunContexts.copyCurrent());
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.arrayList("1", "2", "done"), protocol);
    assertSame(pe, eventHolder.getValue().getException());
    assertNull(eventHolder.getValue().getResult());
    assertFalse(eventHolder.getValue().isCancelled());
    assertTrue(eventHolder.getValue().isFailed());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) Holder(org.eclipse.scout.rt.platform.holders.Holder) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Test(org.junit.Test)

Example 9 with BlockingCountDownLatch

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

the class WhenDoneTest method testCancel.

@Test
public void testCancel() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final Holder<DoneEvent<String>> eventHolder = new Holder<>();
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            protocol.add("1");
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.SECONDS)));
    future.cancel(true);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    future.whenDone(new IDoneHandler<String>() {

        @Override
        public void onDone(DoneEvent<String> event) {
            protocol.add("2");
            if (future.isDone()) {
                protocol.add("done");
            }
            if (future.isCancelled()) {
                protocol.add("cancelled");
            }
            eventHolder.setValue(event);
            verifyLatch.countDown();
        }
    }, RunContexts.copyCurrent());
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.arrayList("2", "done", "cancelled"), protocol);
    assertNull(eventHolder.getValue().getException());
    assertNull(eventHolder.getValue().getResult());
    assertTrue(eventHolder.getValue().isCancelled());
    assertFalse(eventHolder.getValue().isFailed());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) Holder(org.eclipse.scout.rt.platform.holders.Holder) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Test(org.junit.Test)

Example 10 with BlockingCountDownLatch

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

the class WhenDoneTest method testCancelWithJobAlreadyCompleted.

@Test
public void testCancelWithJobAlreadyCompleted() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final Holder<DoneEvent<String>> eventHolder = new Holder<>();
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            protocol.add("1");
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    future.awaitDoneAndGet();
    future.cancel(true);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    future.whenDone(new IDoneHandler<String>() {

        @Override
        public void onDone(DoneEvent<String> event) {
            protocol.add("2");
            if (future.isDone()) {
                protocol.add("done");
            }
            if (future.isCancelled()) {
                protocol.add("cancelled");
            }
            eventHolder.setValue(event);
            verifyLatch.countDown();
        }
    }, RunContexts.copyCurrent());
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.arrayList("1", "2", "done"), protocol);
    assertNull(eventHolder.getValue().getException());
    assertEquals("result", eventHolder.getValue().getResult());
    assertFalse(eventHolder.getValue().isCancelled());
    assertFalse(eventHolder.getValue().isFailed());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) Holder(org.eclipse.scout.rt.platform.holders.Holder) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) 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