use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.
the class PlatformTestRunner method classBlock.
@Override
protected Statement classBlock(final RunNotifier notifier) {
final Statement s4 = super.classBlock(notifier);
final Statement s3 = new RunContextStatement(s4, new IRunContextProvider() {
@Override
public RunContext create() {
return createJUnitRunContext();
}
});
final Statement s2 = new AssertNoRunningJobsStatement(s3, "Test class");
final Statement s1 = new PlatformStatement(s2, ReflectionUtility.getAnnotation(RunWithNewPlatform.class, getTestClass().getJavaClass()));
return s1;
}
use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.
the class ServerRunContextTest method testRunContextsCopyCurrent.
@Test
public void testRunContextsCopyCurrent() {
final IServerSession session = mock(IServerSession.class);
final UserAgent userAgent = UserAgents.create().build();
final Locale locale = Locale.CANADA_FRENCH;
final Subject subject = new Subject();
ServerRunContexts.empty().withSession(session).withUserAgent(userAgent).withLocale(locale).withSubject(subject).run(new IRunnable() {
@Override
public void run() throws Exception {
RunContext runContext = RunContexts.copyCurrent();
assertThat(runContext, CoreMatchers.instanceOf(ServerRunContext.class));
ServerRunContext serverCtx = (ServerRunContext) runContext;
assertSame(session, serverCtx.getSession());
assertSame(userAgent, serverCtx.getUserAgent());
assertSame(locale, serverCtx.getLocale());
assertSame(subject, serverCtx.getSubject());
}
});
}
use of org.eclipse.scout.rt.platform.context.RunContext 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);
}
use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.
the class ClientRunContextTest method testRunContextsCopyCurrent.
@Test
public void testRunContextsCopyCurrent() {
final IClientSession session = mock(IClientSession.class);
final UserAgent sessionUserAgent = UserAgents.create().build();
final Locale sessionLocale = Locale.CANADA_FRENCH;
final Subject sessionSubject = new Subject();
final IDesktop sessionDesktop = mock(IDesktop.class);
when(session.getUserAgent()).thenReturn(sessionUserAgent);
when(session.getLocale()).thenReturn(sessionLocale);
when(session.getSubject()).thenReturn(sessionSubject);
when(session.getDesktopElseVirtualDesktop()).thenReturn(sessionDesktop);
ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {
@Override
public void run() throws Exception {
RunContext runContext = RunContexts.copyCurrent();
assertThat(runContext, CoreMatchers.instanceOf(ClientRunContext.class));
ClientRunContext clientCtx = (ClientRunContext) runContext;
assertSame(session, clientCtx.getSession());
assertSame(sessionLocale, clientCtx.getLocale());
assertSame(sessionUserAgent, clientCtx.getUserAgent());
assertSame(sessionDesktop, clientCtx.getDesktop());
}
});
}
use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.
the class ClientRunContextTest method testRunContextsEmpty.
@Test
public void testRunContextsEmpty() {
RunContext runContext = RunContexts.empty();
assertThat(runContext, CoreMatchers.instanceOf(ClientRunContext.class));
ClientRunContext clientCtx = (ClientRunContext) runContext;
assertNull(clientCtx.getSubject());
assertNull(clientCtx.getSession());
assertNull(clientCtx.getUserAgent());
assertNull(clientCtx.getLocale());
assertEquals(TransactionScope.REQUIRED, clientCtx.getTransactionScope());
}
Aggregations