[jboss-cvs] JBossAS SVN: r61908 - branches/JBoss_4_0_3_SP1_CP/testsuite/src/main/org/jboss/test/web/test.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Mar 30 14:46:06 EDT 2007
Author: bstansberry at jboss.com
Date: 2007-03-30 14:46:06 -0400 (Fri, 30 Mar 2007)
New Revision: 61908
Added:
branches/JBoss_4_0_3_SP1_CP/testsuite/src/main/org/jboss/test/web/test/TreeCacheSSOClusterManagerUnitTestCase.java
Log:
[ASPATCH-157] Fix broken synchronization in TreeCacheSSOClusterManager
Copied: branches/JBoss_4_0_3_SP1_CP/testsuite/src/main/org/jboss/test/web/test/TreeCacheSSOClusterManagerUnitTestCase.java (from rev 61887, branches/Branch_4_2/testsuite/src/main/org/jboss/test/web/test/TreeCacheSSOClusterManagerUnitTestCase.java)
===================================================================
--- branches/JBoss_4_0_3_SP1_CP/testsuite/src/main/org/jboss/test/web/test/TreeCacheSSOClusterManagerUnitTestCase.java (rev 0)
+++ branches/JBoss_4_0_3_SP1_CP/testsuite/src/main/org/jboss/test/web/test/TreeCacheSSOClusterManagerUnitTestCase.java 2007-03-30 18:46:06 UTC (rev 61908)
@@ -0,0 +1,432 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2007, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.web.test;
+
+import java.security.Principal;
+import java.util.Random;
+import java.util.Set;
+import java.util.Vector;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+import javax.transaction.TransactionManager;
+
+import org.apache.catalina.Manager;
+import org.apache.catalina.Session;
+import org.apache.catalina.session.StandardManager;
+import org.apache.catalina.session.StandardSession;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.TreeCacheListener;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.TransactionManagerLookup;
+import org.jboss.cache.TreeCache;
+import org.jboss.cache.transaction.BatchModeTransactionManager;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.jboss.security.SimplePrincipal;
+import org.jboss.test.JBossTestCase;
+import org.jboss.util.threadpool.BasicThreadPool;
+import org.jboss.web.tomcat.tc5.sso.ClusteredSingleSignOn;
+import org.jboss.web.tomcat.tc5.sso.SSOClusterManager;
+import org.jboss.web.tomcat.tc5.sso.TreeCacheSSOClusterManager;
+import org.jgroups.View;
+import org.jgroups.ViewId;
+import org.jgroups.stack.IpAddress;
+
+import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
+
+/**
+ * Test of the TreeCacheSSOClusterManager class.
+ *
+ * @author Brian Stansberry
+ */
+public class TreeCacheSSOClusterManagerUnitTestCase extends JBossTestCase
+{
+ private static IpAddress LOCAL_ADDRESS;
+ private static IpAddress REMOTE_ADDRESS;
+
+ private MBeanServer mbeanServer;
+ private TreeCacheSSOClusterManager testee;
+ private Runner[] runners;
+
+ public TreeCacheSSOClusterManagerUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ LOCAL_ADDRESS = new IpAddress("127.0.0.1", 11111);
+ REMOTE_ADDRESS = new IpAddress("192.168.0.1", 11111);
+
+ mbeanServer =
+ MBeanServerFactory.createMBeanServer("concurrencyTest");
+ try
+ {
+ // Register a cache
+ MockTreeCache cache = new MockTreeCache();
+ // JBAS-4097 -- don't use a TransactionManagerLookup that will
+ // bind DummyTransactionManager into JNDI, as that will screw
+ // up other tests
+ cache.setTransactionManagerLookup(new MockTransactionManagerLookup());
+ mbeanServer.registerMBean(cache, new ObjectName(TreeCacheSSOClusterManager.DEFAULT_GLOBAL_CACHE_NAME));
+ cache.startService();
+
+ // Build up an SSO infrastructure based on LOCAL_ADDRESS
+ testee = new TreeCacheSSOClusterManager(mbeanServer);
+ testee.setCacheName(TreeCacheSSOClusterManager.DEFAULT_GLOBAL_CACHE_NAME);
+
+ MockSSOValve localValve = new MockSSOValve();
+ localValve.setClusterManager(testee);
+ testee.setSingleSignOnValve(localValve);
+ testee.start();
+ }
+ catch (Throwable t)
+ {
+ MBeanServer server = mbeanServer;
+ mbeanServer = null;
+ MBeanServerFactory.releaseMBeanServer(server);
+ }
+ }
+
+ protected void tearDown() throws Exception
+ {
+ if (mbeanServer != null)
+ MBeanServerFactory.releaseMBeanServer(mbeanServer);
+
+ super.tearDown();
+
+ if (runners != null)
+ {
+ for (int i = 0; i < runners.length; i++)
+ runners[i].stop();
+ }
+
+ }
+
+ public void testUpdateCredentialsConcurrency() throws Exception
+ {
+ log.debug("+++ testUpdateCredentialsConcurrency()");
+
+ runners = new Runner[4];
+
+ for (int i = 0; i < runners.length; i++)
+ runners[i] = new UpdateCredentialsRunner(testee, i);
+
+ concurrencyTest(runners);
+ }
+
+ public void testLocalLogoutConcurrency() throws Exception
+ {
+ log.debug("+++ testLocalLogoutConcurrency()");
+
+ runners = new Runner[4];
+
+ for (int i = 0; i < runners.length; i++)
+ runners[i] = new LocalLogoutRunner(testee, i);
+
+ concurrencyTest(runners);
+ }
+
+ public void testRemoteLogoutConcurrency() throws Exception
+ {
+ log.debug("+++ testRemoteLogoutConcurrency()");
+
+ runners = new Runner[4];
+
+ for (int i = 0; i < runners.length; i++)
+ runners[i] = new RemoteLogoutRunner(testee, i);
+
+ concurrencyTest(runners);
+ }
+
+ public void testCombinedConcurrency() throws Exception
+ {
+ log.debug("+++ testCombinedConcurrency()");
+
+ runners = new Runner[12];
+
+ for (int i = 0; i < 4; i++)
+ runners[i] = new UpdateCredentialsRunner(testee, i);
+
+ for (int i = 4; i < 8; i++)
+ runners[i] = new RemoteLogoutRunner(testee, i);
+
+ for (int i = 8; i < 12; i++)
+ runners[i] = new RemoteLogoutRunner(testee, i);
+
+ concurrencyTest(runners);
+ }
+
+ public void concurrencyTest(Runner[] runners) throws Exception
+ {
+
+ for (int i = 0; i < runners.length; i++)
+ runners[i].start();
+
+ for (int i = 0; i < 30; i++)
+ {
+ sleep(1000);
+
+ for (int j = 0; j < runners.length; j++)
+ {
+ assertEquals("Runner had no exception", null, runners[j].getException());
+ assertTrue("Runner is running", runners[j].running);
+ }
+ }
+
+ for (int i = 0; i < runners.length; i++)
+ runners[i].stop();
+ }
+
+
+ static class MockTreeCache extends TreeCache
+ {
+ private IpAddress ourAddress = LOCAL_ADDRESS;
+ private TreeCacheListener listener;
+
+ public MockTreeCache() throws Exception
+ {
+ super();
+ setCacheMode(TreeCache.LOCAL);
+ }
+
+ public Object getLocalAddress()
+ {
+ return ourAddress;
+ }
+
+ void setOurAddress(IpAddress address)
+ {
+ ourAddress = address;
+ }
+
+ public Vector getMembers()
+ {
+ Vector v = new Vector();
+ v.add(LOCAL_ADDRESS);
+ v.add(REMOTE_ADDRESS);
+ return v;
+ }
+
+ public void addTreeCacheListener(TreeCacheListener arg0)
+ {
+ this.listener = arg0;
+ }
+
+ public void removeTreeCacheListener(TreeCacheListener arg0)
+ {
+ this.listener = null;
+ }
+
+ public Object put(Fqn arg0, Object arg1, Object arg2) throws CacheException
+ {
+ if (listener != null)
+ listener.nodeModified(arg0);
+ return null;
+ }
+
+ public Object remove(Fqn arg0, Object arg1) throws CacheException
+ {
+ if (listener != null)
+ listener.nodeModified(arg0);
+ return null;
+ }
+
+ public void remove(Fqn arg0) throws CacheException
+ {
+ if (listener != null)
+ listener.nodeRemoved(arg0);
+ }
+ }
+
+ /**
+ * Override ClusteredSingleSignOn to suppress the empty SSO callbacks
+ */
+ static class MockSSOValve extends ClusteredSingleSignOn
+ {
+ SSOClusterManager mgr;
+
+
+ protected void logout(String ssoId)
+ {
+ mgr.removeSession(ssoId, null);
+ }
+
+ public void setClusterManager(SSOClusterManager clusterManager)
+ {
+ mgr = clusterManager;
+ }
+
+ }
+
+ static class MockSession extends StandardSession
+ {
+ private static final long serialVersionUID = 1L;
+
+ private String ourId;
+
+ MockSession(Manager manager, String id)
+ {
+ super(manager);
+ ourId = id;
+ }
+
+ public String getId()
+ {
+ return ourId;
+ }
+ }
+
+ static class MockTransactionManagerLookup implements TransactionManagerLookup
+ {
+ public TransactionManager getTransactionManager() throws Exception
+ {
+ return new BatchModeTransactionManager();
+ }
+
+ }
+
+ abstract class Runner implements Runnable
+ {
+ boolean running = false;
+ Exception ex = null;
+ Thread thread = null;
+ TreeCacheSSOClusterManager testee;
+ int id;
+ String[] ids;
+ Random random = new Random();
+
+ Runner(TreeCacheSSOClusterManager testee, int id)
+ {
+ this.testee = testee;
+ this.id = id;
+
+ ids = new String[4];
+ for (int i = 0; i < ids.length; i++)
+ {
+ ids[i] = Integer.toString((id + 1) * 100 +i);
+ }
+ }
+
+ Exception getException()
+ {
+ return ex;
+ }
+
+ public void run()
+ {
+ while (running)
+ {
+ try
+ {
+ execute();
+ }
+ catch (Exception e)
+ {
+ ex = e;
+ break;
+ }
+ }
+ }
+
+ abstract void execute() throws Exception;
+
+ void start()
+ {
+ thread = new Thread(this);
+ thread.setDaemon(true);
+ running = true;
+ thread.start();
+ }
+
+ void stop()
+ {
+ running = false;
+ if (thread != null && thread.isAlive())
+ {
+ try
+ {
+ thread.join(100);
+ }
+ catch (InterruptedException e)
+ {
+ }
+
+ if (thread.isAlive())
+ thread.interrupt();
+ }
+ }
+ }
+
+ class UpdateCredentialsRunner extends Runner
+ {
+ UpdateCredentialsRunner(TreeCacheSSOClusterManager testee, int id)
+ {
+ super(testee, id);
+ }
+
+ void execute() throws Exception
+ {
+ int pos = random.nextInt(ids.length);
+ testee.updateCredentials(ids[pos], "BASIC", "BRIAN", "SECRET");
+ }
+ }
+
+ class LocalLogoutRunner extends Runner
+ {
+ LocalLogoutRunner(TreeCacheSSOClusterManager testee, int id)
+ {
+ super(testee, id);
+ }
+
+ void execute() throws Exception
+ {
+ int pos = random.nextInt(ids.length);
+ testee.logout(ids[pos]);
+ }
+ }
+
+ class RemoteLogoutRunner extends Runner
+ {
+ Fqn[] fqns;
+ RemoteLogoutRunner(TreeCacheSSOClusterManager testee, int id)
+ {
+ super(testee, id);
+
+ fqns = new Fqn[ids.length];
+ for (int i = 0; i < fqns.length; i++)
+ {
+ Object[] obj = { "SSO", ids[i], "credentials"};
+ fqns[i] = new Fqn(obj);
+ }
+ }
+
+ void execute() throws Exception
+ {
+ int pos = random.nextInt(fqns.length);
+ testee.nodeRemoved(fqns[pos]);
+ }
+ }
+
+}
More information about the jboss-cvs-commits
mailing list