Search in sources :

Example 11 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult 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 12 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaClusterLockDaoTest method testUnlockedUpdate.

@Test(expected = IllegalMonitorStateException.class)
public void testUnlockedUpdate() throws Exception {
    // Used to make a 'mutable string'
    final AtomicReference<String> currentServer = new AtomicReference<String>("ServerA");
    final String mutexName = "testUnlockedUpdate";
    reset(portalInfoProvider);
    when(portalInfoProvider.getUniqueServerName()).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return currentServer.get();
        }
    });
    // get/create the mutex
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            clusterLockDao.updateLock(mutexName);
        }
    });
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 13 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaClusterLockDaoTest method testWrongServerRelease.

@Test(expected = IllegalMonitorStateException.class)
public void testWrongServerRelease() throws Exception {
    // Used to make a 'mutable string'
    final AtomicReference<String> currentServer = new AtomicReference<String>("ServerA");
    final String mutexName = "testUnlockedRelease";
    reset(portalInfoProvider);
    when(portalInfoProvider.getUniqueServerName()).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return currentServer.get();
        }
    });
    // get/create the mutex
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            clusterLockDao.getLock(mutexName);
        }
    });
    currentServer.set("ServerB");
    // get/create the mutex
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            clusterLockDao.releaseLock(mutexName);
        }
    });
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 14 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult 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
 */
@Test
@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) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 15 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaVersionDaoTest method testVersionBadSql.

@Test
public void testVersionBadSql() {
    final String productName = "TEST_VERSION";
    // Create
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            Version version = versionDao.getVersion(productName);
            assertNull(version);
            version = versionDao.setVersion(productName, 1, 2, 3, null);
            assertNotNull(version);
            assertEquals(1, version.getMajor());
            assertEquals(2, version.getMinor());
            assertEquals(3, version.getPatch());
            assertNull(version.getLocal());
        }
    });
    jdbcOperations.execute("ALTER TABLE UP_VERSION DROP LOCAL_VER");
    try {
        // Doesn't exist
        this.execute(new CallableWithoutResult() {

            @Override
            protected void callWithoutResult() {
                final Version version = versionDao.getVersion(productName);
                assertNotNull(version);
                assertEquals(1, version.getMajor());
                assertEquals(2, version.getMinor());
                assertEquals(3, version.getPatch());
                assertNull(version.getLocal());
            }
        });
    } finally {
        jdbcOperations.execute("ALTER TABLE UP_VERSION ADD COLUMN LOCAL_VER INTEGER");
    }
}
Also used : Version(org.apereo.portal.version.om.Version) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Aggregations

CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)32 Test (org.junit.Test)28 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)14 BaseAggrEventsJpaDaoTest (org.apereo.portal.test.BaseAggrEventsJpaDaoTest)12 DateTime (org.joda.time.DateTime)8 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 CompositeName (javax.naming.CompositeName)7 IEntityGroup (org.apereo.portal.groups.IEntityGroup)7 LinkedList (java.util.LinkedList)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)5 Ignore (org.junit.Ignore)5 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MutableInt (org.apache.commons.lang.mutable.MutableInt)3 MutableObject (org.apache.commons.lang.mutable.MutableObject)3 FunctionWithoutResult (org.apereo.portal.concurrency.FunctionWithoutResult)3 File (java.io.File)2 FileWriter (java.io.FileWriter)2