use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class MutualExclusionTest method testAcquisition1.
/**
* Tests non-blocking mutex acquisition.
*/
@Test(timeout = 1000)
public void testAcquisition1() {
ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
assertEquals(0, mutex.getCompetitorCount());
// Task1 acquires the mutex.
mutex.acquire(m_task1, QueuePosition.HEAD);
assertTrue(mutex.isPermitOwner(m_task1));
assertEquals(1, mutex.getCompetitorCount());
// Wrong mutex release.
try {
mutex.release(m_task2);
fail();
} catch (AssertionException e) {
assertTrue(mutex.isPermitOwner(m_task1));
assertEquals(1, mutex.getCompetitorCount());
}
// Task1 releases the mutex.
mutex.release(m_task1);
assertFalse(mutex.isPermitOwner(m_task1));
assertEquals(0, mutex.getCompetitorCount());
}
use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class MutualExclusionTest method testAwaitDoneWithSameMutexButNotMutexOwner.
/**
* A mutual exclusive job is running, and passes the mutex via BlockingCondition.waitFor() to the next task. But the
* blocking condition is never unblocked, which results in a timeout. However, the job re-acquires the mutex anew
* before continuing.<br/>
* Tests, that the job is the mutex owner after the timeout, and that it cannot wait for another model job to
* complete.
*/
@Test(timeout = 5000)
public void testAwaitDoneWithSameMutexButNotMutexOwner() {
final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
IBlockingCondition bc = Jobs.newBlockingCondition(true);
try {
bc.waitFor(1, TimeUnit.SECONDS);
fail("timeout expected");
} catch (TimedOutError e) {
assertTrue(IFuture.CURRENT.get().getExecutionSemaphore().isPermitOwner(IFuture.CURRENT.get()));
try {
final AtomicBoolean run = new AtomicBoolean(false);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
run.set(true);
}
}, Jobs.newInput().withExecutionSemaphore(mutex)).awaitDone(1, TimeUnit.SECONDS);
fail("AssertionException expected, because the current job is the mutex owner");
} catch (TimedOutError e1) {
fail("no timeout expected");
} catch (AssertionException e1) {
// NOOP: OK
}
}
}
}, Jobs.newInput().withExecutionSemaphore(mutex)).awaitDoneAndGet();
}
use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class HttpSessionMutex method of.
/**
* Gets the mutex for the given {@link HttpSession} to synchronize on.
* <p>
* Returns the session mutex object ({@link #SESSION_MUTEX_ATTRIBUTE_NAME}) if available. To make this mutex object
* available register the class {@link HttpSessionMutex} as listener in the {@code web.xml}.
* <p>
* If no mutex object is available, the {@link HttpSession} itself is returned as mutex. This is valid for many cases
* and servlet containers.
*
* @param httpSession
* The {@link HttpSession} for which the mutex object should be returned. Must not be {@code null}.
* @return A mutex for the given session. Never returns {@code null}.
* @throws AssertionException
* if the given HTTP session is <code>null</code>.
* @throws IllegalStateException
* if the given HTTP session is invalid.
* @see HttpSessionListener
* @see <a href=
* "http://stackoverflow.com/questions/9802165/is-synchronization-within-an-httpsession-feasible">is-synchronization-within-an-httpsession-feasible<a>
* @see <a href=
* "http://stackoverflow.com/questions/616601/is-httpsession-thread-safe-are-set-get-attribute-thread-safe-operations">is-httpsession-thread-safe-are-set-get-attribute-thread-safe-operations</a>
* @see <a href=
* "https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/util/WebUtils.java">Spring
* Implementation of method getSessionMutex</a>
*/
public static Object of(HttpSession httpSession) {
Object mutex = assertNotNull(httpSession).getAttribute(SESSION_MUTEX_ATTRIBUTE_NAME);
if (mutex != null) {
return mutex;
}
LOG.info("Session without mutex: {}. Consider registering {} as listener in the web.xml", httpSession.getId(), HttpSessionMutex.class, new Exception("origin"));
return httpSession;
}
Aggregations