Search in sources :

Example 6 with CallableWithoutResult

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

the class JpaAggregatedTabLookupDaoTest method testLoginAggregationLifecycle.

@Test
public void testLoginAggregationLifecycle() throws Exception {
    when(portalJdbcOperations.queryForList("SELECT NAME FROM UP_LAYOUT_STRUCT where USER_ID = ? AND LAYOUT_ID = ? AND STRUCT_ID = ?", String.class, 1, 1, 1)).thenReturn(Collections.singletonList("TabName"));
    when(portalJdbcOperations.queryForList("SELECT NAME FROM UP_LAYOUT_STRUCT where USER_ID = ? AND LAYOUT_ID = ? AND STRUCT_ID = ?", String.class, 1, 1, 2)).thenReturn(Collections.<String>emptyList());
    when(portalJdbcOperations.queryForList("SELECT USER_NAME FROM UP_USER WHERE USER_ID=?", String.class, 1)).thenReturn(Collections.singletonList("FragmentName"));
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId("u1l1s1");
            assertNotNull(tabMappings);
            assertEquals("FragmentName", tabMappings.getFragmentName());
            assertEquals("TabName", tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId("u1l1s2");
            assertNotNull(tabMappings);
            assertEquals("FragmentName", tabMappings.getFragmentName());
            assertEquals("u1l1s2", tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId(null);
            assertNotNull(tabMappings);
            assertEquals(AggregatedTabMapping.MISSING_TAB_FRAGMENT_NAME, tabMappings.getFragmentName());
            assertEquals(AggregatedTabMapping.MISSING_TAB_NAME, tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId("s1");
            assertNotNull(tabMappings);
            assertEquals(AggregatedTabMapping.PERSONAL_TAB_FRAGMENT_NAME, tabMappings.getFragmentName());
            assertEquals(AggregatedTabMapping.PERSONAL_TAB_NAME, tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId("u1l1s1");
            assertNotNull(tabMappings);
            assertEquals("FragmentName", tabMappings.getFragmentName());
            assertEquals("TabName", tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId("u1l1s2");
            assertNotNull(tabMappings);
            assertEquals("FragmentName", tabMappings.getFragmentName());
            assertEquals("u1l1s2", tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId(null);
            assertNotNull(tabMappings);
            assertEquals(AggregatedTabMapping.MISSING_TAB_FRAGMENT_NAME, tabMappings.getFragmentName());
            assertEquals(AggregatedTabMapping.MISSING_TAB_NAME, tabMappings.getTabName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedTabMapping tabMappings = aggregatedTabLookupDao.getMappedTabForLayoutId("s1");
            assertNotNull(tabMappings);
            assertEquals(AggregatedTabMapping.PERSONAL_TAB_FRAGMENT_NAME, tabMappings.getFragmentName());
            assertEquals(AggregatedTabMapping.PERSONAL_TAB_NAME, tabMappings.getTabName());
        }
    });
}
Also used : CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BaseAggrEventsJpaDaoTest(org.apereo.portal.test.BaseAggrEventsJpaDaoTest)

Example 7 with CallableWithoutResult

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

the class JpaClusterLockDaoTest method testAbandoned.

/**
 * Ignoring this test because it is nondeterministic.
 *
 * @throws Exception
 */
@Test
@Ignore
public void testAbandoned() throws Exception {
    // Used to make a 'mutable string'
    final AtomicReference<String> currentServer = new AtomicReference<String>("ServerA");
    final String mutexName = "testNotAbandoned";
    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() {
            final ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
            Assert.assertNotNull(mutex);
        }
    });
    // lock serverA
    currentServer.set("ServerA");
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
            Assert.assertNotNull(mutex);
        }
    });
    final AtomicInteger lockFailCount = new AtomicInteger(0);
    final AtomicBoolean serverBLocked = new AtomicBoolean(false);
    currentServer.set("ServerB");
    // test context configures a 100ms abandoned lock timeout, wait 110 between tests
    for (int i = 0; i < 5 && !serverBLocked.get(); i++) {
        // try lock ServerB
        execute(new CallableWithoutResult() {

            @Override
            protected void callWithoutResult() {
                final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
                if (mutex == null) {
                    lockFailCount.incrementAndGet();
                } else {
                    serverBLocked.set(true);
                }
            }
        });
        TimeUnit.MILLISECONDS.sleep(110);
    }
    assertTrue(serverBLocked.get());
    assertEquals(1, lockFailCount.get());
    currentServer.set("ServerB");
    ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
    assertTrue(mutex.isLocked());
    clusterLockDao.releaseLock(mutexName);
    mutex = clusterLockDao.getClusterMutex(mutexName);
    assertFalse(mutex.isLocked());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Ignore(org.junit.Ignore) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 8 with CallableWithoutResult

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

the class JpaClusterLockDaoTest method testUnlockedRelease.

@Test(expected = IllegalMonitorStateException.class)
public void testUnlockedRelease() 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.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 9 with CallableWithoutResult

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

the class JpaClusterLockDaoTest method testNotAbandoned.

/**
 * Ignoring this test because it is nondeterministic.
 *
 * @throws Exception
 */
@Test
@Ignore
public void testNotAbandoned() throws Exception {
    // Used to make a 'mutable string'
    final AtomicReference<String> currentServer = new AtomicReference<String>("ServerA");
    final String mutexName = "testNotAbandoned";
    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() {
            final ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
            assertNotNull(mutex);
        }
    });
    // lock serverA
    currentServer.set("ServerA");
    final ClusterMutex lockedMutex = execute(new Callable<ClusterMutex>() {

        @Override
        public ClusterMutex call() throws Exception {
            final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
            assertNotNull(mutex);
            return mutex;
        }
    });
    // 110ms
    while (lockedMutex.getLockStart() + 110 > System.currentTimeMillis()) {
        // try lock ServerB
        currentServer.set("ServerB");
        execute(new CallableWithoutResult() {

            @Override
            protected void callWithoutResult() {
                final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
                assertNull(mutex);
            }
        });
        TimeUnit.MILLISECONDS.sleep(1);
        // ServerA update ping
        currentServer.set("ServerA");
        execute(new CallableWithoutResult() {

            @Override
            protected void callWithoutResult() {
                clusterLockDao.updateLock(mutexName);
            }
        });
        TimeUnit.MILLISECONDS.sleep(1);
    }
    currentServer.set("ServerA");
    ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
    assertTrue(mutex.isLocked());
    clusterLockDao.releaseLock(mutexName);
    mutex = clusterLockDao.getClusterMutex(mutexName);
    assertFalse(mutex.isLocked());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Ignore(org.junit.Ignore) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 10 with CallableWithoutResult

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

the class JpaClusterLockDaoTest method testWrongServerUpdate.

@Test(expected = IllegalMonitorStateException.class)
public void testWrongServerUpdate() 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.getLock(mutexName);
        }
    });
    currentServer.set("ServerB");
    // 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)

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