use of org.eclipse.scout.rt.client.IClientSession 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());
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class SessionStore method registerUiSession.
@Override
public void registerUiSession(final IUiSession uiSession) {
Assertions.assertNotNull(uiSession);
LOG.debug("Register UI session with ID {} in store (clientSessionId={})", uiSession.getUiSessionId(), uiSession.getClientSessionId());
m_writeLock.lock();
try {
IClientSession clientSession = uiSession.getClientSession();
// Store UI session
m_uiSessionMap.put(uiSession.getUiSessionId(), uiSession);
// Store client session (in case it was not yet stored)
m_clientSessionMap.put(clientSession.getId(), clientSession);
// Link to client session
Set<IUiSession> map = m_uiSessionsByClientSession.get(clientSession);
if (map == null) {
map = new HashSet<>();
m_uiSessionsByClientSession.put(clientSession, map);
}
map.add(uiSession);
} finally {
m_writeLock.unlock();
}
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class SessionStore method unregisterUiSession.
@Override
public void unregisterUiSession(final IUiSession uiSession) {
if (uiSession == null) {
return;
}
LOG.debug("Unregister UI session with ID {} from store (clientSessionId={})", uiSession.getUiSessionId(), uiSession.getClientSessionId());
m_writeLock.lock();
try {
// Remove uiSession
m_uiSessionMap.remove(uiSession.getUiSessionId());
// Unlink uiSession from clientSession
final IClientSession clientSession = uiSession.getClientSession();
Set<IUiSession> map = m_uiSessionsByClientSession.get(clientSession);
if (map != null) {
map.remove(uiSession);
}
// Start housekeeping
LOG.debug("{} UI sessions remaining for client session {}", (map == null ? 0 : map.size()), clientSession.getId());
if (map == null || map.isEmpty()) {
m_uiSessionsByClientSession.remove(clientSession);
if (uiSession.isPersistent()) {
// don't start housekeeping for persistent sessions to give the users more time on app switches in ios home screen mode
return;
}
startHousekeeping(clientSession);
}
} finally {
m_writeLock.unlock();
}
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class SessionStore method getClientSessionForUse.
@Override
public IClientSession getClientSessionForUse(String clientSessionId) {
if (clientSessionId == null) {
return null;
}
m_writeLock.lock();
try {
// If housekeeping is scheduled for this session, cancel it (session will be used again, so no cleanup necessary)
IFuture<?> future = m_housekeepingFutures.get(clientSessionId);
if (future != null) {
LOG.debug("Client session with ID {} reserved for use - session housekeeping cancelled!", clientSessionId);
future.cancel(false);
m_housekeepingFutures.remove(clientSessionId);
}
IClientSession clientSession = m_clientSessionMap.get(clientSessionId);
if (clientSession != null && (!clientSession.isActive() || clientSession.isStopping())) {
// only return active sessions
clientSession = null;
}
return clientSession;
} finally {
m_writeLock.unlock();
}
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class AbstractObservableNotificationHandler method notifyListenersOfAllSessions.
/**
* Notify all listeners independent of the session in the current {@link RunContext}
*/
protected void notifyListenersOfAllSessions(final T notification) {
// create copy of m_listeners (EventListenerList is thread-safe)
Map<IClientSession, EventListenerList> listenerMap;
synchronized (m_listeners) {
listenerMap = new HashMap<>(m_listeners);
}
// schedule model job per session to handle notifications
for (Entry<IClientSession, EventListenerList> entry : listenerMap.entrySet()) {
final IClientSession session = entry.getKey();
final EventListenerList list = entry.getValue();
scheduleHandlingNotifications(notification, list, session);
}
}
Aggregations