Search in sources :

Example 56 with BlockingCountDownLatch

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

the class WhenDoneScheduleTest method testCancellationOfFuture.

@Test
public void testCancellationOfFuture() 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);
    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());
    future.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) Test(org.junit.Test)

Example 57 with BlockingCountDownLatch

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

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

the class WhenDoneScheduleTest method testCancellationOfFunctionFuture.

@Test
public void testCancellationOfFunctionFuture() 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);
    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());
    functionFuture.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) Test(org.junit.Test)

Example 59 with BlockingCountDownLatch

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

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

the class WhenDoneTest method testSuccess.

@Test
public void testSuccess() 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)));
    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