Search in sources :

Example 1 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project wildfly by wildfly.

the class EJBReadWriteLockTest method testTimeout.

/**
 * Test that when a thread tries to obtain a read lock when another thread holds a write lock,
 * fails to acquire the lock, if the write lock is not released within the timeout specified
 *
 * @throws Exception
 */
@Test
public void testTimeout() throws Exception {
    // we use a countdown latch for the 2 threads involved
    CountDownLatch latch = new CountDownLatch(2);
    // get a write lock
    Lock writeLock = this.ejbReadWriteLock.writeLock();
    // create a thread which will get hold of a write lock
    // and do some processing for 5 seconds
    Thread threadHoldingWriteLock = new Thread(new ThreadHoldingWriteLock(latch, writeLock, 5000));
    // get a read lock
    Lock readLock = this.ejbReadWriteLock.readLock();
    // start the write lock thread (which internally will obtain
    // a write lock and start a 5 second processing)
    threadHoldingWriteLock.start();
    // wait for few milli sec for the write lock thread to obtain a write lock
    Thread.sleep(500);
    // before the 2 second timeout
    try {
        // try a read lock with 2 second timeout
        boolean readLockAcquired = readLock.tryLock(2, TimeUnit.SECONDS);
        Assert.assertFalse("Unexpected obtained a read lock", readLockAcquired);
    } catch (ConcurrentAccessTimeoutException cate) {
    // expected
    } finally {
        // let the latch know that this thread is done with its part
        // of processing
        latch.countDown();
        // now let's wait for the other thread to complete processing
        // and bringing down the count on the latch
        latch.await();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) Lock(java.util.concurrent.locks.Lock) EJBReadWriteLock(org.jboss.as.ejb3.component.singleton.EJBReadWriteLock) Test(org.junit.Test)

Example 2 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project wildfly by wildfly.

the class SingletonConcurrencyInheritanceTestCase method testImplicitWriteLockMethodNotOverridden.

@Test
public void testImplicitWriteLockMethodNotOverridden() throws Exception {
    final SingletonBaseBean singleton = lookup(SingletonChildBean.class.getSimpleName(), SingletonChildBean.class);
    ExecutorService pool = Executors.newSingleThreadExecutor();
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch entered = new CountDownLatch(1);
    // call a method with a write lock
    // this will block till we hit the latch
    Future<?> future = pool.submit(new Runnable() {

        @Override
        public void run() {
            try {
                singleton.impliedWriteLock(latch, entered);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    entered.await();
    try {
        singleton.impliedWriteLock(latch, entered);
        throw new RuntimeException("Expecting a concurrency access exception");
    } catch (ConcurrentAccessTimeoutException e) {
    // expected
    }
    latch.countDown();
    future.get();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) Test(org.junit.Test)

Example 3 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project wildfly by wildfly.

the class SingletonConcurrencyInheritanceTestCase method testWritersBlockReader.

@Test
public void testWritersBlockReader() throws Exception {
    final SingletonBaseBean singleton = lookup(SingletonChildBean.class.getSimpleName(), SingletonChildBean.class);
    ExecutorService pool = Executors.newSingleThreadExecutor();
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch entered = new CountDownLatch(1);
    // call a method with a write lock
    // this will block till we hit the latch
    Future<?> future = pool.submit(new Runnable() {

        @Override
        public void run() {
            try {
                singleton.writeLock(latch, entered);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    entered.await();
    try {
        singleton.readLock(latch, entered);
        throw new RuntimeException("Expecting a concurrency access exception");
    } catch (ConcurrentAccessTimeoutException e) {
    // expected
    }
    latch.countDown();
    future.get();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) Test(org.junit.Test)

Example 4 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project wildfly by wildfly.

the class SingletonConcurrencyInheritanceTestCase method testOverridenMethodNoAnnotation.

/**
 * Test overriden method uses correct lock type
 */
@Test
public void testOverridenMethodNoAnnotation() throws Exception {
    final SingletonBaseBean singleton = lookup(SingletonChildBean.class.getSimpleName(), SingletonChildBean.class);
    ExecutorService pool = Executors.newSingleThreadExecutor();
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch entered = new CountDownLatch(1);
    // call a method with a write lock
    // this will block till we hit the latch
    Future<?> future = pool.submit(new Runnable() {

        @Override
        public void run() {
            try {
                singleton.readLockOverriddenByParent(latch, entered);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    entered.await();
    try {
        singleton.readLockOverriddenByParent(latch, entered);
        throw new RuntimeException("Expecting a concurrency access exception");
    } catch (ConcurrentAccessTimeoutException e) {
    // expected
    }
    latch.countDown();
    future.get();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) Test(org.junit.Test)

Example 5 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project wildfly by wildfly.

the class SingletonBeanTestCase method testLongWritesSingleton.

/**
 * Tests that invocation on a singleton bean method with write lock results in ConcurrentAccessTimeoutException
 * for subsequent invocations, if the previous invocation(s) hasn't yet completed.
 *
 * @throws Exception
 */
@Test
public void testLongWritesSingleton() throws Exception {
    // let's invoke a bean method (with WRITE lock semantics) which takes a long time to complete
    final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    // let's now try and invoke on this bean while the previous operation is in progress.
    // we expect a ConcurrentAccessTimeoutException
    final int NUM_THREADS = 10;
    final ExecutorService nextTenInvocations = Executors.newFixedThreadPool(NUM_THREADS);
    Future<?>[] results = new Future[NUM_THREADS];
    // set on it
    for (int i = 0; i < NUM_THREADS; i++) {
        results[i] = nextTenInvocations.submit(new LongWritesSingletonBeanInvoker(longWritesSingletonBean));
    }
    // Now fetch the results.
    // all are expected to timeout
    // one is expected to complete successfully
    // rest all are expected to timeout
    final List<Object> passed = new ArrayList<Object>();
    final List<Throwable> throwables = new ArrayList<Throwable>();
    for (int i = 0; i < NUM_THREADS; i++) {
        try {
            passed.add(results[i].get(10, TimeUnit.SECONDS));
        } catch (ExecutionException ee) {
            throwables.add(ee.getCause());
        }
    }
    // only one call succeeded, so count should be 1
    Assert.assertEquals("Unexpected count on singleton bean after invocation on method with WRITE lock semantic: ", 1, this.longWritesSingletonBean.getCount());
    for (Throwable t : throwables) {
        assertTrue(t.toString(), t instanceof ConcurrentAccessTimeoutException);
    }
    assertEquals(1, passed.size());
    assertEquals(NUM_THREADS - 1, throwables.size());
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) Test(org.junit.Test)

Aggregations

ConcurrentAccessTimeoutException (javax.ejb.ConcurrentAccessTimeoutException)17 CountDownLatch (java.util.concurrent.CountDownLatch)7 Test (org.junit.Test)6 ExecutorService (java.util.concurrent.ExecutorService)5 RemoteException (java.rmi.RemoteException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Lock (java.util.concurrent.locks.Lock)3 ConcurrentAccessException (javax.ejb.ConcurrentAccessException)3 ApplicationException (org.apache.openejb.ApplicationException)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 ArrayList (java.util.ArrayList)2 InitialContext (javax.naming.InitialContext)2 BeanContext (org.apache.openejb.BeanContext)2 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)2 Pool (org.apache.openejb.util.Pool)2 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)2 EJBComponent (org.jboss.as.ejb3.component.EJBComponent)2 FencedLock (com.hazelcast.cp.lock.FencedLock)1 InvocationInfo (com.sun.ejb.InvocationInfo)1 MethodLockInfo (com.sun.ejb.MethodLockInfo)1