Search in sources :

Example 11 with AssertionException

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());
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 12 with AssertionException

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) 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) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 13 with AssertionException

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;
}
Also used : AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException)

Aggregations

AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)13 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)8 Test (org.junit.Test)7 IOException (java.io.IOException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchProviderException (java.security.NoSuchProviderException)5 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)5 InvalidKeyException (java.security.InvalidKeyException)4 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)3 KeyFactory (java.security.KeyFactory)2 Signature (java.security.Signature)2 SignatureException (java.security.SignatureException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)2 OutputStream (java.io.OutputStream)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1