Search in sources :

Example 6 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor 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 7 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class JobManager method createJobFutureTask.

/**
 * Creates the Future to be given to {@link ExecutorService} and registers it with this job manager.
 *
 * @param callable
 *          callable to be given to the executor for execution.
 * @param input
 *          input that describes the job to be executed.
 */
protected <RESULT> JobFutureTask<RESULT> createJobFutureTask(final Callable<RESULT> callable, final JobInput input) {
    final RunMonitor runMonitor = Assertions.assertNotNull(input.getRunContext() != null ? input.getRunContext().getRunMonitor() : BEANS.get(RunMonitor.class), "'RunMonitor' required if providing a 'RunContext'");
    final JobInput inputCopy = ensureJobInputName(input, callable.getClass().getName());
    return new JobFutureTask<>(this, runMonitor, inputCopy, new CallableChain<RESULT>(), callable);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor)

Example 8 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class ClientJobCancelTest method doPingRequestAsync.

/**
 * Runs a 'ping-request' which gets blocked in the service implementation.
 */
protected RequestData doPingRequestAsync(final String pingRequest) throws Exception {
    final BlockingCountDownLatch serviceCallSetupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch serviceCallCompletedLatch = new BlockingCountDownLatch(1);
    final AtomicBoolean serviceCallInterrupted = new AtomicBoolean(false);
    // Mock the PingService.
    class PingService implements IPingService {

        @Override
        public String ping(String s) {
            try {
                assertTrue(serviceCallSetupLatch.countDownAndBlock());
            } catch (java.lang.InterruptedException e) {
                serviceCallInterrupted.set(true);
            } finally {
                serviceCallCompletedLatch.countDown();
            }
            return s.toUpperCase();
        }
    }
    // Create a separate RunContext with a separate RunMonitor, so we can wait for the service result in case of cancellation.
    final ClientRunContext runContext = ClientRunContexts.copyCurrent();
    final RunMonitor runMonitor = BEANS.get(RunMonitor.class);
    runContext.withRunMonitor(runMonitor);
    IFuture<String> pingFuture = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return runContext.call(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    IBean<?> bean = TestingUtility.registerBean(new BeanMetaData(PingService.class).withInitialInstance(new PingService()).withApplicationScoped(true));
                    try {
                        return ServiceTunnelUtility.createProxy(IPingService.class).ping(pingRequest);
                    } finally {
                        TestingUtility.unregisterBeans(Arrays.asList(bean));
                    }
                }
            });
        }
    }, Jobs.newInput().withExceptionHandling(null, false));
    // Wait for the ping request to enter service implementation.
    assertTrue(serviceCallSetupLatch.await());
    return new RequestData(pingFuture, runMonitor, serviceCallSetupLatch, serviceCallCompletedLatch, serviceCallInterrupted);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) BeanMetaData(org.eclipse.scout.rt.platform.BeanMetaData) IPingService(org.eclipse.scout.rt.shared.services.common.ping.IPingService) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IPingService(org.eclipse.scout.rt.shared.services.common.ping.IPingService) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor)

Example 9 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class RunMonitorCancelRegistry method cancelAllBySessionId.

/**
 * Cancels all registered monitors associated with the given 'sessionId' except the one that is currently running and
 * invoking this method (to ensure that the calling {@link RunMonitor} is not cancelled).
 *
 * @return <code>true</code> if all monitors matching the given 'sessionId' could be cancelled, or <code>false</code>
 *         if no monitor is registered, or if at least one monitor was already cancelled or failed to be cancelled.
 */
public boolean cancelAllBySessionId(final String sessionId) {
    final List<RegistryEntry> registryEntries;
    final RunMonitor currentRunMonitor = RunMonitor.CURRENT.get();
    synchronized (m_registryLock) {
        registryEntries = m_sessionIdIndex.get(sessionId);
        for (Iterator<RegistryEntry> it = registryEntries.iterator(); it.hasNext(); ) {
            RegistryEntry next = it.next();
            if (next.getRunMonitor() == currentRunMonitor) {
                it.remove();
            }
        }
        m_registry.remove(registryEntries);
    }
    return cancelRunMonitors(getRunMonitors(registryEntries));
}
Also used : RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor)

Example 10 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class RunMonitorCancelRegistryTest method before.

/**
 * Prepares 3 RunMonitors:<br>
 * session1: monitor1 (requestId=1), monitor2 (requestId=2)<br/>
 * session2: monitor3 (requestId=1)
 */
@Before
public void before() {
    m_registry = new RunMonitorCancelRegistry();
    m_monitor1 = new RunMonitor();
    m_monitor2 = new RunMonitor();
    m_monitor3 = new RunMonitor();
    m_regHandle1 = m_registry.register(m_monitor1, SESSION_ID_1, 1L);
    m_regHandle2 = m_registry.register(m_monitor2, SESSION_ID_1, 2L);
    m_regHandle3 = m_registry.register(m_monitor3, SESSION_ID_2, 1L);
    assertEquals(CollectionUtility.hashSet(m_monitor1, m_monitor2, m_monitor3), m_registry.getAll());
    assertEquals(CollectionUtility.arrayList(m_monitor1, m_monitor2), m_registry.getAllBySession(SESSION_ID_1));
    assertEquals(CollectionUtility.arrayList(m_monitor3), m_registry.getAllBySession(SESSION_ID_2));
    assertEquals(CollectionUtility.arrayList(m_monitor1), m_registry.getAllBySessionIdAndRequestId(SESSION_ID_1, 1L));
    assertEquals(CollectionUtility.arrayList(m_monitor2), m_registry.getAllBySessionIdAndRequestId(SESSION_ID_1, 2L));
    assertEquals(CollectionUtility.arrayList(m_monitor3), m_registry.getAllBySessionIdAndRequestId(SESSION_ID_2, 1L));
}
Also used : RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) Before(org.junit.Before)

Aggregations

RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)12 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)7 Test (org.junit.Test)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)2 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2 Callable (java.util.concurrent.Callable)1 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 JobInput (org.eclipse.scout.rt.platform.job.JobInput)1 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)1 IPingService (org.eclipse.scout.rt.shared.services.common.ping.IPingService)1 JSONObject (org.json.JSONObject)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1