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();
}
}
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();
}
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();
}
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();
}
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());
}
Aggregations