Search in sources :

Example 1 with ThreadGroupRunner

use of org.apereo.portal.test.ThreadGroupRunner 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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadGroupRunner(org.apereo.portal.test.ThreadGroupRunner) ThrowingRunnable(org.apereo.portal.utils.threading.ThrowingRunnable) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Ignore(org.junit.Ignore)

Example 2 with ThreadGroupRunner

use of org.apereo.portal.test.ThreadGroupRunner 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();
}
Also used : ThreadGroupRunner(org.apereo.portal.test.ThreadGroupRunner) ThrowingRunnable(org.apereo.portal.utils.threading.ThrowingRunnable) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 3 with ThreadGroupRunner

use of org.apereo.portal.test.ThreadGroupRunner 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());
}
Also used : ThreadGroupRunner(org.apereo.portal.test.ThreadGroupRunner) Callable(java.util.concurrent.Callable) ThrowingRunnable(org.apereo.portal.utils.threading.ThrowingRunnable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 4 with ThreadGroupRunner

use of org.apereo.portal.test.ThreadGroupRunner 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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadGroupRunner(org.apereo.portal.test.ThreadGroupRunner) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) ThrowingRunnable(org.apereo.portal.utils.threading.ThrowingRunnable) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Aggregations

ThreadGroupRunner (org.apereo.portal.test.ThreadGroupRunner)4 ThrowingRunnable (org.apereo.portal.utils.threading.ThrowingRunnable)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)3 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)2 Test (org.junit.Test)2 Callable (java.util.concurrent.Callable)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Ignore (org.junit.Ignore)1