Search in sources :

Example 6 with JobInput

use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.

the class ExceptionProcessorTest method testWithNullExceptionHandlerAndSwallow.

@Test
public void testWithNullExceptionHandlerAndSwallow() throws Exception {
    final RuntimeException exception = new RuntimeException("expected JUnit test exception");
    JobInput jobInput = Jobs.newInput().withExceptionHandling(null, true);
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new ExceptionProcessor<String>(jobInput));
    chain.call(new Callable<String>() {

        @Override
        public String call() throws Exception {
            throw exception;
        }
    });
    verify(m_exceptionHandler, never()).handle(eq(exception));
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 7 with JobInput

use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.

the class ExceptionProcessorTest method testWithCustomExceptionHandler.

@Test
public void testWithCustomExceptionHandler() throws Exception {
    final RuntimeException exception = new RuntimeException("expected JUnit test exception");
    final AtomicReference<Throwable> error = new AtomicReference<>();
    JobInput jobInput = Jobs.newInput().withExceptionHandling(new ExceptionHandler() {

        @Override
        public void handle(Throwable t) {
            error.set(t);
        }
    }, true);
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new ExceptionProcessor<String>(jobInput));
    chain.call(new Callable<String>() {

        @Override
        public String call() throws Exception {
            throw exception;
        }
    });
    assertSame(exception, error.get());
    verify(m_exceptionHandler, never()).handle(eq(exception));
}
Also used : CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) AtomicReference(java.util.concurrent.atomic.AtomicReference) JobInput(org.eclipse.scout.rt.platform.job.JobInput) ExceptionHandler(org.eclipse.scout.rt.platform.exception.ExceptionHandler) Test(org.junit.Test)

Example 8 with JobInput

use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.

the class BlockingTestUtility method runBlockingAction.

/**
 * Helper method to test code which will enter a blocking condition.
 * <p>
 * If <code>runnableOnceBlocked</code> throws an exception, it is given to {@link JUnitExceptionHandler} to make the
 * JUnit test fail.
 *
 * @param runnableGettingBlocked
 *          {@code IRunnable} that will enter a blocking condition.
 * @param runnableOnceBlocked
 *          {@code IRunnable} to be executed once the 'runnableGettingBlocked' enters a blocking condition.
 * @param awaitBackgroundJobs
 *          true waits for background jobs running in the same session to complete before runnableOnceBlocked is
 *          called
 */
public static void runBlockingAction(final IRunnable runnableGettingBlocked, final IRunnable runnableOnceBlocked, final boolean awaitBackgroundJobs) {
    final ClientRunContext runContext = ClientRunContexts.copyCurrent();
    final IBlockingCondition onceBlockedDoneCondition = Jobs.newBlockingCondition(true);
    // remember the list of client jobs before blocking
    final Set<IFuture<?>> jobsBefore = new HashSet<>();
    jobsBefore.addAll(BEANS.get(IJobManager.class).getFutures(new IFilter<IFuture<?>>() {

        @Override
        public boolean accept(IFuture<?> cand) {
            final RunContext candContext = cand.getJobInput().getRunContext();
            return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession();
        }
    }));
    final IRegistrationHandle listenerRegistration = IFuture.CURRENT.get().addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).andMatchExecutionHint(ModelJobs.EXECUTION_HINT_UI_INTERACTION_REQUIRED).toFilter(), new IJobListener() {

        @Override
        public void changed(final JobEvent event) {
            // waitFor was entered
            final IRunnable callRunnableOnceBlocked = new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        runnableOnceBlocked.run();
                    } finally {
                        event.getData().getBlockingCondition().setBlocking(false);
                        onceBlockedDoneCondition.setBlocking(false);
                    }
                }
            };
            final JobInput jobInputForRunnableOnceBlocked = ModelJobs.newInput(runContext).withExceptionHandling(BEANS.get(JUnitExceptionHandler.class), true).withName("JUnit: Handling blocked thread because waiting for a blocking condition");
            if (awaitBackgroundJobs) {
                // wait until all background jobs finished
                Jobs.schedule(new IRunnable() {

                    @Override
                    public void run() throws Exception {
                        jobsBefore.add(IFuture.CURRENT.get());
                        BEANS.get(IJobManager.class).awaitFinished(new IFilter<IFuture<?>>() {

                            @Override
                            public boolean accept(IFuture<?> f) {
                                RunContext candContext = f.getJobInput().getRunContext();
                                return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession() && !jobsBefore.contains(f);
                            }
                        }, 5, TimeUnit.MINUTES);
                        // call runnableOnceBlocked
                        ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
                    }
                }, Jobs.newInput().withName("wait until background jobs finished"));
            } else {
                // call runnableOnceBlocked directly
                ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
            }
        }
    });
    try {
        // this action will enter a blocking condition which causes the 'runnableOnceBlocked' to be executed.
        runnableGettingBlocked.run();
    } catch (final Exception e) {
        throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
    } finally {
        listenerRegistration.dispose();
    }
    // we need to wait until the runnableOnceBlocked is completed.
    // runnableOnceBlocked may, during its execution,  set the original blocking condition to non-blocking but still execute
    // important code afterwards. Therefore, the original blocking condition that starts runnableOnceBlocked is only used
    // to indicate the start of the runnableOnceBlocked, but this method returns only AFTER runnableOnceBlocked completes execution.
    onceBlockedDoneCondition.waitForUninterruptibly(120, TimeUnit.SECONDS);
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IFuture(org.eclipse.scout.rt.platform.job.IFuture) IJobListener(org.eclipse.scout.rt.platform.job.listener.IJobListener) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) JobInput(org.eclipse.scout.rt.platform.job.JobInput) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IFilter(org.eclipse.scout.rt.platform.filter.IFilter) RunContext(org.eclipse.scout.rt.platform.context.RunContext) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) HashSet(java.util.HashSet)

Example 9 with JobInput

use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.

the class ExceptionProcessorTest method testWithSwallow.

@Test
public void testWithSwallow() throws Exception {
    final RuntimeException exception = new RuntimeException("expected JUnit test exception");
    JobInput jobInput = Jobs.newInput().withExceptionHandling(BEANS.get(ExceptionHandler.class), true);
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new ExceptionProcessor<String>(jobInput));
    chain.call(new Callable<String>() {

        @Override
        public String call() throws Exception {
            throw exception;
        }
    });
    verify(m_exceptionHandler, times(1)).handle(eq(exception));
    verifyNoMoreInteractions(m_exceptionHandler);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) ExceptionHandler(org.eclipse.scout.rt.platform.exception.ExceptionHandler) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 10 with JobInput

use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.

the class ModelJobValidatorTest method test.

@Test
public void test() {
    new ModelJobValidator().validateJobInput(new JobInput().withExecutionSemaphore(m_clientSession.getModelJobSemaphore()).withRunContext(ClientRunContexts.empty().withSession(m_clientSession, true)));
    assertTrue(true);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) Test(org.junit.Test)

Aggregations

JobInput (org.eclipse.scout.rt.platform.job.JobInput)10 Test (org.junit.Test)8 CallableChain (org.eclipse.scout.rt.platform.chain.callable.CallableChain)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ExceptionHandler (org.eclipse.scout.rt.platform.exception.ExceptionHandler)2 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 HashSet (java.util.HashSet)1 IClientSession (org.eclipse.scout.rt.client.IClientSession)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 RunContext (org.eclipse.scout.rt.platform.context.RunContext)1 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 IFilter (org.eclipse.scout.rt.platform.filter.IFilter)1 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)1 JobState (org.eclipse.scout.rt.platform.job.JobState)1 IJobListener (org.eclipse.scout.rt.platform.job.listener.IJobListener)1 IRegistrationHandle (org.eclipse.scout.rt.platform.util.IRegistrationHandle)1