Search in sources :

Example 36 with BlockingCountDownLatch

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

the class FutureAwaitTest method testAwaitDoneAndGetWithTimeout_Cancelled.

@Test(timeout = 5000)
public void testAwaitDoneAndGetWithTimeout_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(10, TimeUnit.SECONDS);
        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 37 with BlockingCountDownLatch

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

the class RunContextTest method testHardCancellation.

@Test
public void testHardCancellation() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    final AtomicBoolean interrupted = new AtomicBoolean();
    final RunMonitor monitor = BEANS.get(RunMonitor.class);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            RunContexts.empty().withRunMonitor(monitor).run(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        assertTrue(setupLatch.countDownAndBlock(10, TimeUnit.SECONDS));
                    } catch (InterruptedException e) {
                        interrupted.set(true);
                    } finally {
                        verifyLatch.countDown();
                    }
                }
            });
        }
    }, Jobs.newInput());
    assertTrue(setupLatch.await());
    monitor.cancel(true);
    assertTrue(verifyLatch.await());
    assertTrue(interrupted.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 38 with BlockingCountDownLatch

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

the class InvocationContextTest method testCancel.

@Test(timeout = 5000)
public void testCancel() {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final IBlockingCondition processingCondition = Jobs.newBlockingCondition(true);
    final InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
    invocationContext.withEndpointUrl("http://localhost");
    // Make Stub.webMethod to block until cancelled.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            setupLatch.countDown();
            processingCondition.waitForUninterruptibly(10, TimeUnit.SECONDS);
            return null;
        }
    }).when(m_port).webMethod();
    final RunMonitor runMonitor = new RunMonitor();
    // Cancel the 'webMethod' once blocking.
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            setupLatch.await();
            runMonitor.cancel(true);
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Run the test by invoking the web service with a specific RunMonitor to test cancellation.
    try {
        RunContexts.empty().withRunMonitor(runMonitor).run(new IRunnable() {

            @Override
            public void run() throws Exception {
                try {
                    // this method blocks until cancelled.
                    invocationContext.getPort().webMethod();
                    fail("WebServiceRequestCancelledException expected");
                } catch (WebServiceRequestCancelledException e) {
                    verify(m_implementorSpecifics).closeSocket(same(m_port), anyString());
                }
            }
        });
    } finally {
        processingCondition.setBlocking(false);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 39 with BlockingCountDownLatch

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

the class MultipleSessionTest method testCancel.

@Test
public void testCancel() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch1 = new BlockingCountDownLatch(2);
    final BlockingCountDownLatch setupLatch2 = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch interruptedJob1_S1_Latch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch awaitAllCancelledLatch = new BlockingCountDownLatch(1);
    // Session 1 (job1)
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job1-S1");
            try {
                setupLatch1.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job1-S1-interrupted");
            } finally {
                interruptedJob1_S1_Latch.countDown();
            }
            // ensure the thread's interrupted status to be cleared in order to continue the test.
            Thread.interrupted();
            awaitAllCancelledLatch.await();
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-1-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // Session 1 (job2) --> never starts running because cancelled while job1 is mutex-owner
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job2-S1");
            try {
                setupLatch2.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job2-S1-interrupted");
            }
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-2-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // Session 2 (job1)
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job1-S2");
            try {
                setupLatch1.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job1-S2-interrupted");
            }
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-1-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // Session 2 (job2)
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job2-S2");
            try {
                setupLatch2.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job2-S2-interrupted");
            }
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-2-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    assertTrue(setupLatch1.await());
    assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S2"), protocol);
    Jobs.getJobManager().cancel(ModelJobs.newFutureFilterBuilder().andMatch(new SessionFutureFilter(m_clientSession1)).toFilter(), // cancels job-1-S1 and job-2-S1, meaning that job-2-S1 never starts running.
    true);
    awaitAllCancelledLatch.unblock();
    assertTrue(interruptedJob1_S1_Latch.await());
    setupLatch1.unblock();
    assertTrue(setupLatch2.await());
    assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S1-interrupted", "job1-S2", "job2-S2"), protocol);
    setupLatch2.unblock();
    // Wait until all jobs completed
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) SessionFutureFilter(org.eclipse.scout.rt.shared.job.filter.future.SessionFutureFilter) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 40 with BlockingCountDownLatch

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

the class MultipleSessionTest method testMutalExclusion.

@Test
public void testMutalExclusion() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch latch1 = new BlockingCountDownLatch(2);
    final BlockingCountDownLatch latch2 = new BlockingCountDownLatch(2);
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job1-S1");
            latch1.countDownAndBlock();
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-1-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job2-S1");
            latch2.countDownAndBlock();
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true)).withName("job-2-S1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job1-S2");
            latch1.countDownAndBlock();
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-1-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job2-S2");
            latch2.countDownAndBlock();
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession2, true)).withName("job-2-S2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    assertTrue(latch1.await());
    assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S2"), protocol);
    latch1.unblock();
    assertTrue(latch2.await());
    assertEquals(CollectionUtility.hashSet("job1-S1", "job1-S2", "job2-S1", "job2-S2"), protocol);
    latch2.unblock();
    // Wait until all jobs completed
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) 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