Search in sources :

Example 61 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class AbstractJaxWsClientTest method testAcquirePortConcurrentlyInDifferentTransactions.

@Test
public void testAcquirePortConcurrentlyInDifferentTransactions() throws InterruptedException {
    final CountDownLatch txn1InitLatch = new CountDownLatch(1);
    final CountDownLatch txn2InitLatch = new CountDownLatch(1);
    final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port0 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port0, 0);
            txn1PortHolder.setValue(port0);
            txn1InitLatch.countDown();
            txn2InitLatch.await();
            JaxWsConsumerTestServicePortType port1 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSamePort(port0, port1);
            assertSendEcho(port1, 1);
        }
    }, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
    txn1InitLatch.await();
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port0 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port0, 0);
            txn2PortHolder.setValue(port0);
            txn2InitLatch.countDown();
            JaxWsConsumerTestServicePortType port1 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSamePort(port0, port1);
            assertSendEcho(port1, 1);
        }
    }, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
    txn2InitLatch.await();
    assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
Also used : Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) JaxWsConsumerTestServicePortType(org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.JaxWsConsumerTestServicePortType) SocketTimeoutException(java.net.SocketTimeoutException) WebServiceException(javax.xml.ws.WebServiceException) Test(org.junit.Test)

Example 62 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class AbstractJaxWsClientTest method testAcquirePortInDifferentTransactions.

/*
   * ************************************************************
   * Test acquire port in different transactions
   * ************************************************************/
@Test
public void testAcquirePortInDifferentTransactions() throws InterruptedException {
    final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    // This test case expects at most one port in the pool. It is guaranteed by discarding all pooled entries.
    BEANS.get(JaxWsConsumerTestClient.class).discardAllPoolEntries();
    ServerRunContexts.copyCurrent().run(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port, 0);
            txn1PortHolder.setValue(port);
        }
    });
    ServerRunContexts.copyCurrent().run(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port, 0);
            txn2PortHolder.setValue(port);
        }
    });
    if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
        assertSamePort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
    } else {
        assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
    }
}
Also used : Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JaxWsConsumerTestServicePortType(org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.JaxWsConsumerTestServicePortType) SocketTimeoutException(java.net.SocketTimeoutException) WebServiceException(javax.xml.ws.WebServiceException) JaxWsImplementorSpecifics(org.eclipse.scout.rt.server.jaxws.implementor.JaxWsImplementorSpecifics) Test(org.junit.Test)

Example 63 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class UiSessionTest method doTestLogoutWithBlockingModelDisposal.

/**
 * Tests that session invalidation still works even if the model is blocking during its disposal. Especially the
 * {@link HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)} method is expected not
 * to be blocked by the {@link SessionStore} (the method is invoked by the servlet container and blocking its thread
 * could interfere with the application server itself, for example when a background thread is cleaning up timed out
 * sessions).
 */
protected void doTestLogoutWithBlockingModelDisposal(final CloseAction openFormCloseAction, boolean expectFormFinallyCompleted, boolean expectSessionActiveAfterwards) throws InterruptedException {
    // create new UI session along with client client and HTTP sessions
    UiSession uiSession = (UiSession) JsonTestUtility.createAndInitializeUiSession();
    final HttpSession httpSession = UiSessionTestUtility.getHttpSession(uiSession);
    IClientSession clientSession = uiSession.getClientSession();
    assertFalse(uiSession.isDisposed());
    // register ui session in session store
    final ISessionStore sessionStore = BEANS.get(HttpSessionHelper.class).getSessionStore(httpSession);
    sessionStore.registerUiSession(uiSession);
    assertThat(sessionStore, is(instanceOf(HttpSessionBindingListener.class)));
    // create and start test form in a model job
    SessionStoreTestForm form = ModelJobs.schedule(new Callable<SessionStoreTestForm>() {

        @Override
        public SessionStoreTestForm call() throws Exception {
            SessionStoreTestForm f = new SessionStoreTestForm(openFormCloseAction);
            f.start();
            return f;
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(clientSession, true))).awaitDoneAndGet(1, TimeUnit.SECONDS);
    // schedule a job that emulates servlet container that performs session invalidation on session timeout
    IFuture<Void> appServerSessionTimeoutFuture = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            httpSession.invalidate();
        }
    }, Jobs.newInput().withName("simulate session timeout").withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(200, TimeUnit.MILLISECONDS)));
    // perform logout on UI session that stops the client session and disposes all model objects.
    uiSession.logout();
    assertTrue(uiSession.isDisposed());
    if (expectFormFinallyCompleted) {
        form.awaitDoFinallyCompleted(1, TimeUnit.SECONDS);
    } else {
        BEANS.get(UiJobs.class).awaitModelJobs(clientSession, JUnitExceptionHandler.class);
    }
    appServerSessionTimeoutFuture.awaitDone(3, TimeUnit.SECONDS);
    assertTrue(appServerSessionTimeoutFuture.isDone());
    if (expectFormFinallyCompleted) {
        assertTrue(form.isFinallyCompleted());
        BEANS.get(UiJobs.class).awaitModelJobs(clientSession, JUnitExceptionHandler.class);
    } else {
        assertFalse(form.isFinallyCompleted());
        // The model job was canceled by the SessionStore. Hence we cannot use UiJobs.awaitModelJobs().
        // We still sleep some time
        SleepUtil.sleepSafe(200, TimeUnit.MILLISECONDS);
    }
    assertEquals(expectSessionActiveAfterwards, clientSession.isActive());
}
Also used : HttpSession(javax.servlet.http.HttpSession) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Callable(java.util.concurrent.Callable) IClientSession(org.eclipse.scout.rt.client.IClientSession) SessionStoreTestForm(org.eclipse.scout.rt.ui.html.fixtures.SessionStoreTestForm)

Example 64 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobListenerBlockedFutureTest method testEventsForBlockingJob.

@Test(timeout = 10000)
public void testEventsForBlockingJob() {
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    IClientSession clientSession = mock(IClientSession.class);
    when(clientSession.getModelJobSemaphore()).thenReturn(Jobs.newExecutionSemaphore(1));
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(ModelJobs.newEventFilterBuilder().toFilter(), captureListener);
    IFuture<Void> outerFuture = null;
    final AtomicReference<IFuture<?>> innerFuture = new AtomicReference<>();
    final JobInput modelJobInput = ModelJobs.newInput(ClientRunContexts.empty().withSession(clientSession, true));
    // start recording of events
    outerFuture = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            innerFuture.set(Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    condition.setBlocking(false);
                    // Wait until the outer future is re-acquiring the mutex.
                    // 2=outer-job + inner-job
                    JobTestUtil.waitForPermitCompetitors(modelJobInput.getExecutionSemaphore(), 2);
                }
            }, modelJobInput.copy().withName("inner").withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(2, TimeUnit.SECONDS))));
            condition.waitFor();
        }
    }, modelJobInput.copy().withName("outer"));
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(outerFuture).toFilter(), 1, TimeUnit.MINUTES);
    Jobs.getJobManager().shutdown();
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    // inner
    i++;
    assertStateChangedEvent(innerFuture.get(), JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    // inner
    i++;
    assertStateChangedEvent(innerFuture.get(), JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.WAITING_FOR_BLOCKING_CONDITION, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
    // inner
    i++;
    assertStateChangedEvent(innerFuture.get(), JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    // inner
    i++;
    assertStateChangedEvent(innerFuture.get(), JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    // inner
    i++;
    assertStateChangedEvent(innerFuture.get(), JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    // outer
    i++;
    assertStateChangedEvent(outerFuture, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IFuture(org.eclipse.scout.rt.platform.job.IFuture) JobInput(org.eclipse.scout.rt.platform.job.JobInput) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IClientSession(org.eclipse.scout.rt.client.IClientSession) JobState(org.eclipse.scout.rt.platform.job.JobState) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 65 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class ModelJobTest method testModelThread.

@Test
public void testModelThread() {
    final AtomicBoolean modelThread = new AtomicBoolean();
    assertFalse(ModelJobs.isModelThread());
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            modelThread.set(ModelJobs.isModelThread());
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(m_clientSession1, true))).awaitDoneAndGet();
    assertFalse(ModelJobs.isModelThread());
    assertTrue(modelThread.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Aggregations

IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)260 Test (org.junit.Test)210 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)82 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)68 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)40 ArrayList (java.util.ArrayList)36 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)26 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)21 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)20 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)20 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)17 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)13 Times (org.eclipse.scout.rt.testing.platform.runner.Times)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 IFuture (org.eclipse.scout.rt.platform.job.IFuture)10 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)10 JMSException (javax.jms.JMSException)9