Search in sources :

Example 21 with TimedOutError

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

the class JmsMomImplementorTest method testRequestReplyWithBlockingCondition.

@Test(timeout = 200_000)
public void testRequestReplyWithBlockingCondition() throws InterruptedException {
    final IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testRequestReplyWithBlockingCondition", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    // semaphore for jobs
    IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    // request should block with IBlockingCondition.waitFor in order to release semaphore
    IFuture<String> requestFuture = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return MOM.request(JmsTestMom.class, queue, "hello world");
        }
    }, Jobs.newInput().withName("requester (T)").withExecutionHint(m_testJobExecutionHint).withExceptionHandling(null, false).withExecutionSemaphore(mutex));
    // test if semaphore was released (with waitFor)
    IFuture<Void> otherFuture = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
        // nop
        }
    }, Jobs.newInput().withName("null job").withExecutionHint(m_testJobExecutionHint).withExecutionSemaphore(mutex));
    // Verify
    try {
        otherFuture.awaitDone(2000, TimeUnit.MILLISECONDS);
        assertSame(JobState.WAITING_FOR_BLOCKING_CONDITION, requestFuture.getState());
    } catch (TimedOutError e) {
        fail();
    }
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException) Test(org.junit.Test)

Example 22 with TimedOutError

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

the class JmsMomImplementorTest method testRequestReplyTimeoutInternal.

private void testRequestReplyTimeoutInternal(final IBiDestination<String, String> destination) throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(2);
    final AtomicBoolean requestorTimedOut = new AtomicBoolean();
    final AtomicBoolean replierInterrupted = new AtomicBoolean();
    // Subscribe for the destination
    m_disposables.add(MOM.reply(JmsTestMom.class, destination, new IRequestListener<String, String>() {

        @Override
        public String onRequest(IMessage<String> request) {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                replierInterrupted.set(true);
            } finally {
                verifyLatch.countDown();
            }
            return request.getTransferObject().toUpperCase();
        }
    }));
    // Initiate 'request-reply' communication
    try {
        MOM.request(JmsTestMom.class, destination, "hello world", MOM.newPublishInput().withRequestReplyTimeout(1, TimeUnit.SECONDS));
    } catch (TimedOutError e) {
        requestorTimedOut.set(true);
    } finally {
        verifyLatch.countDown();
    }
    assertTrue(verifyLatch.await());
    // Verify
    assertTrue(requestorTimedOut.get());
    assertTrue(replierInterrupted.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRequestListener(org.eclipse.scout.rt.mom.api.IRequestListener) IMessage(org.eclipse.scout.rt.mom.api.IMessage) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)

Example 23 with TimedOutError

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

the class JmsMomImplementorTest method after.

@After
public void after() throws Exception {
    // Dispose resources
    dispose(m_disposables);
    // Cancel jobs
    IFilter<IFuture<?>> testJobsFilter = Jobs.newFutureFilterBuilder().andMatchExecutionHint(m_testJobExecutionHint).toFilter();
    Set<IFuture<?>> futures = Jobs.getJobManager().getFutures(testJobsFilter);
    if (futures.size() > 0) {
        LOG.info("Cancelling {} jobs: {}", futures.size(), futures);
        Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchFuture(futures).andMatchNotState(JobState.DONE).toFilter(), true);
        long t0 = System.nanoTime();
        try {
            Jobs.getJobManager().awaitDone(testJobsFilter, 10, TimeUnit.SECONDS);
            LOG.info("All jobs have finished after {} ms", StringUtility.formatNanos(System.nanoTime() - t0));
        } catch (TimedOutError e) {
            LOG.warn("Some cancelled jobs are still running after {} ms! Please check their implementation.", StringUtility.formatNanos(System.nanoTime() - t0));
        }
    }
    uninstallTestMom();
    // ensure activeMQ is stopped
    BrokerService brokerService = BrokerRegistry.getInstance().findFirst();
    if (brokerService != null) {
        brokerService.stop();
        brokerService.waitUntilStopped();
    }
    LOG.info("Finished test in {} ms", StringUtility.formatNanos(System.nanoTime() - m_t0));
    LOG.info("</{}>", m_testName.getMethodName());
}
Also used : TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IFuture(org.eclipse.scout.rt.platform.job.IFuture) BrokerService(org.apache.activemq.broker.BrokerService) After(org.junit.After)

Example 24 with TimedOutError

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

the class JmsMomImplementor method request.

@Override
public <REQUEST, REPLY> REPLY request(final IBiDestination<REQUEST, REPLY> destination, final REQUEST requestObject, final PublishInput input) {
    assertTrue(m_requestReplyEnabled, "'request-reply' messaging is not enabled for this MOM");
    assertNotNull(destination, "destination not specified");
    assertNotNull(input, "publishInput not specified");
    assertFalse(input.isTransactional(), "transactional mode not supported for 'request-reply' communication");
    // JMS message ID not applicable because unknown until sent
    final String replyId = String.format("scout.mom.requestreply.uid-%s", UUID.randomUUID());
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    IFuture<Message> requestFuture = Jobs.schedule(new Callable<Message>() {

        @Override
        public Message call() throws Exception {
            try {
                return requestImpl(destination, requestObject, input, replyId);
            } finally {
                condition.setBlocking(false);
            }
        }
    }, newJobInput().withName("request on {}", destination.getName()).withExceptionHandling(BEANS.get(MomExceptionHandler.class), false).withRunContext(RunContexts.copyCurrent(true).withDiagnostics(BEANS.all(IJmsRunContextDiagnostics.class))));
    try {
        long timeout = input.getRequestReplyTimeout();
        if (timeout == PublishInput.INFINITELY) {
            condition.waitFor();
        } else {
            condition.waitFor(timeout, TimeUnit.MILLISECONDS);
        }
        Message responseMessage = requestFuture.awaitDoneAndGet();
        return transform(responseMessage, replyId, resolveMarshaller(destination));
    } catch (JMSException e) {
        throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
    } catch (ThreadInterruptedError | TimedOutError e) {
        // send cancel to replier
        cancelRequest(replyId);
        // cancel request job
        if (requestFuture.cancel(true)) {
            requestFuture.awaitDone();
        }
        throw e;
    }
}
Also used : Message(javax.jms.Message) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) JMSException(javax.jms.JMSException) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition)

Example 25 with TimedOutError

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

the class JobStateTest method testBlockedAndTimeout.

@Test
// regression
@Times(100)
public void testBlockedAndTimeout() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final AtomicReference<Thread> workerThread = new AtomicReference<>();
    final IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            workerThread.set(Thread.currentThread());
            assertSame(JobState.RUNNING, IFuture.CURRENT.get().getState());
            try {
                condition.waitFor(1, TimeUnit.MILLISECONDS, "ABC");
                fail("timeout expected");
            } catch (TimedOutError e) {
                assertTrue(mutex.isPermitOwner(IFuture.CURRENT.get()));
                assertSame(JobState.RUNNING, IFuture.CURRENT.get().getState());
            }
        }
    }, Jobs.newInput().withName("job-1").withExecutionSemaphore(mutex));
    // Wait until job-1 completed
    future1.awaitDoneAndGet(10, TimeUnit.SECONDS);
    assertTrue(future1.isDone());
    assertFalse(future1.isCancelled());
    // wait because permit is released just after done (max 30s)
    JobTestUtil.waitForPermitCompetitors(mutex, 0);
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future1, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertHintChangedEvent(JobEventType.JOB_EXECUTION_HINT_ADDED, future1, "ABC", capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertBlockedStateEvent(future1, condition, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertHintChangedEvent(JobEventType.JOB_EXECUTION_HINT_REMOVED, future1, "ABC", capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    // due to timeout
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, 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) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Aggregations

TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)32 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)20 Test (org.junit.Test)20 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)11 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)9 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)8 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)6 IFuture (org.eclipse.scout.rt.platform.job.IFuture)6 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)5 ArrayList (java.util.ArrayList)4 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)4 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)4 Times (org.eclipse.scout.rt.testing.platform.runner.Times)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 JMSException (javax.jms.JMSException)3 NamingException (javax.naming.NamingException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JobCompletionDelayOnSessionShutdown (org.eclipse.scout.rt.client.ClientConfigProperties.JobCompletionDelayOnSessionShutdown)2