use of org.apereo.portal.utils.threading.ThrowingRunnable in project uPortal by Jasig.
the class JpaClusterLockDaoTest method testConcurrentCreateLocking.
/**
* This test turns out to be nondeterministic under load and so can yield false-negatives
* (failures that don't seem to actually indicate a regression).
*
* @throws InterruptedException
*/
@Ignore
public void testConcurrentCreateLocking() throws InterruptedException {
reset(portalInfoProvider);
when(portalInfoProvider.getUniqueServerName()).thenReturn("ServerA");
final String mutexName = "testConcurrentLocking";
final ThreadGroupRunner threadGroupRunner = new ThreadGroupRunner("JpaClusterLockDaoTest-", true);
final AtomicInteger lockCounter = new AtomicInteger();
threadGroupRunner.addTask(3, new ThrowingRunnable() {
@Override
public void runWithException() throws Throwable {
executeInTransaction(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
try {
threadGroupRunner.tick(1);
try {
final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
if (mutex != null) {
lockCounter.incrementAndGet();
}
} finally {
threadGroupRunner.tick(3);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
});
threadGroupRunner.start();
threadGroupRunner.join();
assertEquals(1, lockCounter.intValue());
ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
assertTrue(mutex.isLocked());
clusterLockDao.releaseLock(mutexName);
mutex = clusterLockDao.getClusterMutex(mutexName);
assertFalse(mutex.isLocked());
}
use of org.apereo.portal.utils.threading.ThrowingRunnable in project uPortal by Jasig.
the class JpaClusterLockDaoTest method testConcurrentCreation.
@Test
public void testConcurrentCreation() throws InterruptedException {
reset(portalInfoProvider);
when(portalInfoProvider.getUniqueServerName()).thenReturn("ServerA");
final ThreadGroupRunner threadGroupRunner = new ThreadGroupRunner("JpaClusterLockDaoTest-", true);
threadGroupRunner.addTask(3, new ThrowingRunnable() {
@Override
public void runWithException() throws Throwable {
executeInTransaction(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
try {
final String mutexName = "testConcurrentCreation";
threadGroupRunner.tick(1);
ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
assertNotNull(mutex);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
});
threadGroupRunner.start();
threadGroupRunner.join();
}
use of org.apereo.portal.utils.threading.ThrowingRunnable in project uPortal by Jasig.
the class ClusterLockServiceImplDbBackedTest method testTryLockFunction.
private void testTryLockFunction(final IClusterLockService service) throws InterruptedException {
reset(portalInfoProvider);
when(portalInfoProvider.getUniqueServerName()).thenReturn("ServerA");
final ThreadGroupRunner threadGroupRunner = new ThreadGroupRunner("ClusterLockServiceImplTest-", true);
final String mutexName = "testLockFunction";
final AtomicInteger executionCounter = new AtomicInteger(0);
final AtomicBoolean concurrent = new AtomicBoolean(false);
final AtomicInteger trueCounter = new AtomicInteger(0);
final AtomicInteger falseCounter = new AtomicInteger(0);
final int threads = 3;
threadGroupRunner.addTask(threads, new ThrowingRunnable() {
@Override
public void runWithException() throws Throwable {
execute(new Callable<Object>() {
@Override
public Object call() throws Exception {
threadGroupRunner.tick(1);
final TryLockFunctionResult<Object> result = service.doInTryLock(mutexName, new Function<ClusterMutex, Object>() {
@Override
public Object apply(ClusterMutex input) {
if (concurrent.getAndSet(true)) {
fail("Only one thread should be in Function at a time");
}
try {
executionCounter.incrementAndGet();
logger.debug("Starting 1500ms of work");
Thread.sleep(1500);
logger.debug("Completed 1500ms of work");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
concurrent.set(false);
}
return null;
}
});
if (result.isExecuted()) {
trueCounter.incrementAndGet();
} else {
falseCounter.incrementAndGet();
}
return result;
}
});
}
});
threadGroupRunner.start();
threadGroupRunner.join();
assertEquals(1, executionCounter.get());
assertEquals(1, trueCounter.get());
assertEquals(threads - 1, falseCounter.get());
assertFalse(concurrent.get());
}
use of org.apereo.portal.utils.threading.ThrowingRunnable in project uPortal by Jasig.
the class JpaClusterLockDaoTest method testConcurrentLocking.
@Test
public void testConcurrentLocking() throws InterruptedException {
reset(portalInfoProvider);
when(portalInfoProvider.getUniqueServerName()).thenReturn("ServerA");
final String mutexName = "testConcurrentLocking";
execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
final ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
assertNotNull(mutex);
}
});
final ThreadGroupRunner threadGroupRunner = new ThreadGroupRunner("JpaClusterLockDaoTest-", true);
final AtomicInteger lockCounter = new AtomicInteger();
threadGroupRunner.addTask(3, new ThrowingRunnable() {
@Override
public void runWithException() throws Throwable {
executeInTransaction(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
try {
threadGroupRunner.tick(1);
try {
final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
if (mutex != null) {
lockCounter.incrementAndGet();
}
} finally {
threadGroupRunner.tick(3);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
});
threadGroupRunner.start();
threadGroupRunner.join();
assertEquals(1, lockCounter.intValue());
ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
assertTrue(mutex.isLocked());
clusterLockDao.releaseLock(mutexName);
mutex = clusterLockDao.getClusterMutex(mutexName);
assertFalse(mutex.isLocked());
}
Aggregations