[jboss-cvs] JBossAS SVN: r80095 - in projects/ejb3/trunk/core/src: test/java/org/jboss/ejb3/core/test and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 27 02:47:18 EDT 2008
Author: ALRubinger
Date: 2008-10-27 02:47:18 -0400 (Mon, 27 Oct 2008)
New Revision: 80095
Added:
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManager.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManagerFactory.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCache.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCacheFactory.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulBean.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulLocal.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/unit/
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/unit/PassivationDoesNotPreventNewActivityUnitTestCase.java
Modified:
projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java
projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/AbstractEJB3TestCase.java
projects/ejb3/trunk/core/src/test/resources/statefulcontainer-beans.xml
Log:
[EJBTHREE-1549] Added Unit Tests to expose SimpleStatefulCache Passivation blocks new session creation and other session invocations
Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java 2008-10-27 04:48:05 UTC (rev 80094)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -56,7 +56,7 @@
private StatefulSessionPersistenceManager pm;
private long sessionTimeout = 300; // 5 minutes
private long removalTimeout = 0;
- private SessionTimeoutTask timeoutTask;
+ private Thread timeoutTask;
private RemovalTimeoutTask removalTask = null;
private boolean running = true;
private int createCount = 0;
@@ -166,12 +166,17 @@
}
}
- private class SessionTimeoutTask extends Thread
+ protected class SessionTimeoutTask extends Thread
{
public SessionTimeoutTask(String name)
{
super(name);
}
+
+ public void block() throws InterruptedException
+ {
+ Thread.sleep(sessionTimeout * 1000);
+ }
public void run()
{
@@ -179,7 +184,7 @@
{
try
{
- Thread.sleep(sessionTimeout * 1000);
+ block();
}
catch (InterruptedException e)
{
@@ -189,7 +194,7 @@
try
{
synchronized (cacheMap)
- {
+ {
if (!running) return;
boolean trace = log.isTraceEnabled();
@@ -489,4 +494,14 @@
{
return this.running;
}
+
+ protected Thread getTimeoutTask()
+ {
+ return timeoutTask;
+ }
+
+ protected void setTimeoutTask(Thread timeoutTask)
+ {
+ this.timeoutTask = timeoutTask;
+ }
}
Modified: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/AbstractEJB3TestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/AbstractEJB3TestCase.java 2008-10-27 04:48:05 UTC (rev 80094)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/common/AbstractEJB3TestCase.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -37,6 +37,8 @@
import org.jboss.ejb3.Ejb3Deployment;
import org.jboss.ejb3.Ejb3Registry;
import org.jboss.ejb3.InitialContextFactory;
+import org.jboss.ejb3.cache.CacheFactoryRegistry;
+import org.jboss.ejb3.cache.Ejb3CacheFactory;
import org.jboss.ejb3.cache.persistence.PersistenceManagerFactoryRegistry;
import org.jboss.ejb3.common.registrar.plugin.mc.Ejb3McRegistrar;
import org.jboss.ejb3.common.registrar.spi.DuplicateBindException;
@@ -74,6 +76,8 @@
private static final String OBJECT_STORE_NAME_PM_FACTORY_REGISTRY = "EJB3PersistenceManagerFactoryRegistry";
+ private static final String OBJECT_STORE_NAME_CACHE_FACTORY_REGISTRY = "EJB3CacheFactoryRegistry";
+
private static InitialContext initialContext;
private static List<SessionContainer> containers = new ArrayList<SessionContainer>();
@@ -215,16 +219,19 @@
DeploymentUnit unit = new MockDeploymentUnit();
Ejb3Deployment deployment = new MockEjb3Deployment(unit, null);
- // Is SFSB, manually set a PM Factory Registry
+ // Is SFSB, manually set a PM Factory Registry and Cache Factory
//TODO C'mon, here? Much better elsewhere.
- if(sessionType.equals(ContainerType.SFSB))
+ if (sessionType.equals(ContainerType.SFSB))
{
- // Lookup PM Factory Registry in MC
+ // Lookup Factory Registries in MC
PersistenceManagerFactoryRegistry registry = Ejb3RegistrarLocator.locateRegistrar().lookup(
AbstractEJB3TestCase.OBJECT_STORE_NAME_PM_FACTORY_REGISTRY, PersistenceManagerFactoryRegistry.class);
+ CacheFactoryRegistry cacheFactoryRegistry = Ejb3RegistrarLocator.locateRegistrar().lookup(
+ AbstractEJB3TestCase.OBJECT_STORE_NAME_CACHE_FACTORY_REGISTRY, CacheFactoryRegistry.class);
// Set on the deployment
deployment.setPersistenceManagerFactoryRegistry(registry);
+ deployment.setCacheFactoryRegistry(cacheFactoryRegistry);
}
// Create a Session Container
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManager.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManager.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManager.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,78 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549;
+
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.jboss.ejb3.cache.simple.StatefulSessionFilePersistenceManager;
+import org.jboss.ejb3.stateful.StatefulBeanContext;
+import org.jboss.logging.Logger;
+
+/**
+ * BlockingPersistenceManager
+ *
+ * An implementation of a PersistenceManager which, instead of
+ * persisting directly, exposes a blocking mechanism allowing
+ * tests to control when passivation occurs
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class BlockingPersistenceManager extends StatefulSessionFilePersistenceManager
+{
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ private static final Logger log = Logger.getLogger(BlockingPersistenceManager.class);
+
+ /**
+ * Publicly-accessible lock to allow tests to block passivation
+ */
+ public static final Lock LOCK = new ReentrantLock();
+
+ // --------------------------------------------------------------------------------||
+ // Required Implementations -------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ @Override
+ public void passivateSession(StatefulBeanContext ctx)
+ {
+ // Block until the lock may be acquired,
+ // may currently be held by the test Thread
+ LOCK.lock();
+
+ try
+ {
+ // Mock Passivate
+ log.info("Mock Passivation on " + ctx);
+ }
+ finally
+ {
+ // Release the lock
+ LOCK.unlock();
+ }
+
+ }
+
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManagerFactory.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManagerFactory.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/BlockingPersistenceManagerFactory.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,56 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549;
+
+import org.jboss.ejb3.cache.persistence.PersistenceManagerFactory;
+import org.jboss.ejb3.cache.simple.StatefulSessionPersistenceManager;
+
+/**
+ * BlockingPersistenceManagerFactory
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class BlockingPersistenceManagerFactory implements PersistenceManagerFactory
+{
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Name under which this PM will be bound into the PersistenceManagerFactoryRegistry
+ */
+ public static final String REGISTRY_BIND_NAME = "BlockingPersistenceManager";
+
+ // --------------------------------------------------------------------------------||
+ // Required Implementations -------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.cache.persistence.PersistenceManagerFactory#createPersistenceManager()
+ */
+ public StatefulSessionPersistenceManager createPersistenceManager()
+ {
+ return new BlockingPersistenceManager();
+ }
+
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCache.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCache.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCache.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,112 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549;
+
+import org.jboss.ejb3.cache.simple.SimpleStatefulCache;
+import org.jboss.logging.Logger;
+
+/**
+ * ForcePassivationCache
+ *
+ * An extension of the SimpleStatefulCache which provides for
+ * forcing the Passivation Thread to run
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class ForcePassivationCache extends SimpleStatefulCache
+{
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ private static final Logger log = Logger.getLogger(ForcePassivationCache.class);
+
+ private static final Object PASSIVATION_LOCK = new Object();
+
+ // --------------------------------------------------------------------------------||
+ // Functional Methods -------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Triggers passivation to run
+ */
+ public static void forcePassivation()
+ {
+ // Get a lock
+ log.info("Awaiting lock to force passivation");
+ synchronized (PASSIVATION_LOCK)
+ {
+ // Notify that passivation should run
+ log.info("Notifying passivation via manual force...");
+ PASSIVATION_LOCK.notify();
+ }
+ }
+
+ // --------------------------------------------------------------------------------||
+ // Overridden Implementations -----------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ @Override
+ public void start()
+ {
+ // Switch up the Timeout (Passivation) Task to a blocking implementation
+ this.setTimeoutTask(new BlockingPassivationTask("EJBTHREE-1549 SFSB Passivation Thread"));
+
+ // Call super implementation
+ super.start();
+
+ }
+
+ /**
+ * BlockingPassivationTask
+ *
+ * An extension of the default timeout task which, instead of
+ * waiting for a timeout, will await (block until) notification that passivation
+ * should run
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ private class BlockingPassivationTask extends SessionTimeoutTask
+ {
+ public BlockingPassivationTask(String name)
+ {
+ super(name);
+ }
+
+ public void block() throws InterruptedException
+ {
+ // Get a lock on our monitor
+ synchronized (PASSIVATION_LOCK)
+ {
+ // Wait until we're signaled
+ log.info("Waiting to be notified to run passivation...");
+ PASSIVATION_LOCK.wait();
+ }
+
+ // Log that we've been notified
+ log.info("Notified to run passivation");
+ }
+ }
+
+}
\ No newline at end of file
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCacheFactory.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCacheFactory.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/ForcePassivationCacheFactory.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549;
+
+import org.jboss.ejb3.cache.Ejb3CacheFactory;
+import org.jboss.ejb3.cache.StatefulCache;
+
+/**
+ * ForcePassivationCacheFactory
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class ForcePassivationCacheFactory implements Ejb3CacheFactory
+{
+
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Name under which this Cache Factory will be bound into the registry
+ */
+ public static final String REGISTRY_BIND_NAME = "ForcePassivationCache";
+
+ // --------------------------------------------------------------------------------||
+ // Required Implementations -------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.ejb3.cache.Ejb3CacheFactory#createCache()
+ */
+ public StatefulCache createCache()
+ {
+ return new ForcePassivationCache();
+ }
+
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulBean.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulBean.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulBean.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549;
+
+import javax.ejb.Local;
+import javax.ejb.Stateful;
+
+import org.jboss.ejb3.annotation.Cache;
+import org.jboss.ejb3.annotation.CacheConfig;
+import org.jboss.ejb3.annotation.LocalBinding;
+import org.jboss.ejb3.annotation.PersistenceManager;
+
+/**
+ * MyStatefulBean
+ *
+ * A SFSB with the following overrides:
+ *
+ * 1) Uses a Cache implementation that provides mechanism to
+ * force passivation (instead of waiting upon a Thread.sleep timeout)
+ *
+ * 2) Uses a backing PersistenceManager that provides mechanism to
+ * block completion of passivation from the test until ready
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Stateful
+ at Local(MyStatefulLocal.class)
+ at LocalBinding(jndiBinding = MyStatefulLocal.JNDI_NAME)
+/*
+ * Use a CacheFctory that instead of using a timed reaping Thread,
+ * exposes a static "forcePassivation" method for the tests
+ */
+ at Cache(ForcePassivationCacheFactory.REGISTRY_BIND_NAME)
+/*
+ * Make instances eligible for timeout very soon after last invocation
+ */
+ at CacheConfig(idleTimeoutSeconds = MyStatefulLocal.PASSIVATION_TIMEOUT)
+/*
+ * Set up a persistence manager that allows us to block, and therefore
+ * lets the test decide how long the processes of performing passivation
+ * should take. Used to manually interleave Threads to target the test case.
+ */
+ at PersistenceManager(BlockingPersistenceManagerFactory.REGISTRY_BIND_NAME)
+public class MyStatefulBean implements MyStatefulLocal
+{
+ // --------------------------------------------------------------------------------||
+ // Instance Members ---------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ private int counter;
+
+ // --------------------------------------------------------------------------------||
+ // Required Implementations -------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Returns and increments the counter, which starts at 0
+ */
+ public int getNextCounter()
+ {
+ return counter++;
+ }
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulLocal.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulLocal.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/MyStatefulLocal.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549;
+
+/**
+ * MyStatefulLocal
+ *
+ * A local business interface of a Test EJB
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface MyStatefulLocal
+{
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ String JNDI_NAME = "TestSFSB/local";
+
+ int PASSIVATION_TIMEOUT = 1;
+
+ // --------------------------------------------------------------------------------||
+ // Contracts ----------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Returns and increments the counter, which starts at 0
+ */
+ int getNextCounter();
+}
Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/unit/PassivationDoesNotPreventNewActivityUnitTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/unit/PassivationDoesNotPreventNewActivityUnitTestCase.java (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1549/unit/PassivationDoesNotPreventNewActivityUnitTestCase.java 2008-10-27 06:47:18 UTC (rev 80095)
@@ -0,0 +1,331 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.ejb3.core.test.ejbthree1549.unit;
+
+import junit.framework.TestCase;
+
+import org.jboss.ejb3.cache.CacheFactoryRegistry;
+import org.jboss.ejb3.cache.persistence.PersistenceManagerFactoryRegistry;
+import org.jboss.ejb3.common.registrar.spi.Ejb3RegistrarLocator;
+import org.jboss.ejb3.core.test.common.AbstractEJB3TestCase;
+import org.jboss.ejb3.core.test.ejbthree1549.BlockingPersistenceManager;
+import org.jboss.ejb3.core.test.ejbthree1549.BlockingPersistenceManagerFactory;
+import org.jboss.ejb3.core.test.ejbthree1549.ForcePassivationCache;
+import org.jboss.ejb3.core.test.ejbthree1549.ForcePassivationCacheFactory;
+import org.jboss.ejb3.core.test.ejbthree1549.MyStatefulBean;
+import org.jboss.ejb3.core.test.ejbthree1549.MyStatefulLocal;
+import org.jboss.ejb3.session.SessionContainer;
+import org.jboss.logging.Logger;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * PassivationDoesNotPreventNewActivityUnitTestCase
+ *
+ * Contains tests to ensure that performing passivation does not
+ * block either new session creation or invocation in other sessions
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class PassivationDoesNotPreventNewActivityUnitTestCase extends AbstractEJB3TestCase
+{
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ private static SessionContainer container;
+
+ private static final Logger log = Logger.getLogger(PassivationDoesNotPreventNewActivityUnitTestCase.class);
+
+ // --------------------------------------------------------------------------------||
+ // Tests --------------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Tests that a new session may be created while another is being passivated
+ */
+ @Test
+ public void testNewSessionMayBeCreatedDuringPassivation() throws Throwable
+ {
+ // Initialize
+ final String sfsbJndiName = MyStatefulLocal.JNDI_NAME;
+
+ // Define the task to lookup a new session
+ TestThread lookupNewSessionTest = new TestThread("Lookup New Session Test Thread")
+ {
+ /**
+ * Lookup a new session, and set that the test succeeded if we're able
+ */
+ public void run()
+ {
+ try
+ {
+ // Create a new session, which should be allowed during passivation
+ MyStatefulLocal bean2 = (MyStatefulLocal) container.getInitialContext().lookup(sfsbJndiName);
+
+ // If we've got a new session
+ if (bean2 != null)
+ {
+ // Test is good
+ this.setTestSucceeded(true);
+ }
+
+ }
+ // We can't fail the unit test from here, so log the error
+ catch (Exception e)
+ {
+ log.error("Test encountered an error", e);
+ }
+ }
+ };
+
+ // Run the Test
+ this.runTest(lookupNewSessionTest);
+ }
+
+ /**
+ * Tests that a one session may carry out an invocation while another session
+ * is undergoing passivation
+ */
+ @Test
+ public void testSessionMayBeInvokedWhileAnotherIsPassivating() throws Throwable
+ {
+ // Initialize
+ final String sfsbJndiName = MyStatefulLocal.JNDI_NAME;
+
+ // Define the task to invoke upon a SFSB
+ TestThread invokeDuringPasssivationTest = new TestThread("Invoke During Passivation Test Thread")
+ {
+ /**
+ * Lookup a new session, invoke upon it, and set that the test succeeded if we're able
+ */
+ public void run()
+ {
+ try
+ {
+ // Create a new session, which should be allowed during passivation
+ MyStatefulLocal bean2 = (MyStatefulLocal) container.getInitialContext().lookup(sfsbJndiName);
+
+ // If we've got nothing
+ if (bean2 == null)
+ {
+ // Let test fail, logging along the wayF
+ log.error("Lookup did not succeed");
+ return;
+ }
+
+ // Invoke upon the new Session
+ int next = bean2.getNextCounter();
+
+ // Test the value
+ if (next == 0)
+ {
+ this.setTestSucceeded(true);
+ return;
+ }
+
+ // Value was not expected, let the test fail and log
+ log.error("Invocation succeeded, but expected next counter of 0 and got: " + next);
+
+ }
+ // We can't fail the unit test from here, so log the error
+ catch (Exception e)
+ {
+ log.error("Test encountered an error", e);
+ }
+ }
+ };
+
+ // Run the Test
+ this.runTest(invokeDuringPasssivationTest);
+ }
+
+ // --------------------------------------------------------------------------------||
+ // Internal Helper Methods --------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Performs the following common test actions:
+ *
+ * 1) Looks up and invokes upon a new SFSB instance
+ * 2) Blocks passivation from completing
+ * 3) Waits for the SFSB instance to becomes eligible for passivation
+ * 4) Forces passivation to run
+ * 5) Runs the specified Test Thread
+ * 6) Allows the test to complete either successfully or unsuccessfully, or times out if stuck
+ * 7) Reports failure or success overall
+ */
+ protected void runTest(TestThread testThread) throws Throwable
+ {
+ // Initialize
+ final String sfsbJndiName = MyStatefulLocal.JNDI_NAME;
+ boolean testSucceeded = false;
+
+ // Lookup an instance
+ MyStatefulLocal bean1 = (MyStatefulLocal) container.getInitialContext().lookup(sfsbJndiName);
+
+ // Invoke upon our bean
+ int next = bean1.getNextCounter();
+ log.info("Got counter from bean1 : " + next);
+ TestCase.assertEquals("SFSB did not return expected next counter", 0, next);
+
+ // Get the lock to block the PM, now
+ boolean gotLock = BlockingPersistenceManager.LOCK.tryLock();
+
+ // Once PM lock is acquired, everything is in "try" so we release in "finally"
+ try
+ {
+ // Ensure we got the PM lock, else fail the test
+ TestCase.assertTrue("Test was not able to immediately get the lock to block the PersistenceManager", gotLock);
+ log.info("Locked " + BlockingPersistenceManager.class.getSimpleName());
+
+ // Wait to allow bean to be eligible for passivation
+ long sleepTime = MyStatefulLocal.PASSIVATION_TIMEOUT * 1000 + 1000; // Add 1/2 a second to the configured passivation timeout
+ Thread.sleep(sleepTime);
+
+ // Trigger Passivation
+ ForcePassivationCache.forcePassivation();
+ log.info("Passivation forced, carrying out test");
+
+ // Wait to allow passivation to actually start
+ Thread.sleep(2000);
+
+ /*
+ * At this point, we've told the passivation Thread to start, and have
+ * locked it from completing. So let's try our test in another Thread
+ * so we can detect a deadlock or permanent blocking after a timeout
+ */
+
+ // Start up the test Thread
+ testThread.start();
+
+ // Define a timeout for the Test Thread to complete
+ int timeoutSeconds = 5;
+ long startTime = System.currentTimeMillis();
+ long endTime = timeoutSeconds * 1000 + startTime;
+
+ // Loop until timeout
+ while (System.currentTimeMillis() < endTime)
+ {
+ // If the test has not succeeded, and the test Thread is dead
+ if (!testThread.testSucceeded && !testThread.isAlive())
+ {
+ // Fail
+ TestCase.fail("The test has completed without success");
+ break;
+ }
+
+ // If the test has been successful
+ if (testThread.testSucceeded)
+ {
+ log.info("Test Succeeded");
+ testSucceeded = true;
+ break;
+ }
+
+ // Wait a little before looping again
+ Thread.sleep(50);
+ }
+ }
+ finally
+ {
+
+ // Allow the Persistence Manager to finish up
+ BlockingPersistenceManager.LOCK.unlock();
+ }
+
+ // Ensure we're good
+ TestCase.assertTrue("The test did not succeed", testSucceeded);
+ }
+
+ // --------------------------------------------------------------------------------||
+ // Inner Classes ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Defines a test task to run alongside main test execution, providing
+ * hooks to test whether the task was successful or not
+ */
+ protected static class TestThread extends Thread
+ {
+ /**
+ * Whether or not the test succeeded
+ */
+ private boolean testSucceeded;
+
+ public TestThread(String threadName)
+ {
+ super(threadName);
+ }
+
+ public boolean isTestSucceeded()
+ {
+ return testSucceeded;
+ }
+
+ public void setTestSucceeded(boolean testSucceeded)
+ {
+ this.testSucceeded = testSucceeded;
+ }
+ }
+
+ // --------------------------------------------------------------------------------||
+ // Lifecycle Methods --------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ @BeforeClass
+ public static void beforeClass() throws Exception
+ {
+ // Perform generic setup of MC, default beans, etc
+ AbstractEJB3TestCase.beforeClass();
+
+ // Add the Blocking PersistenceManager
+ PersistenceManagerFactoryRegistry pmFactoryRegistry = Ejb3RegistrarLocator.locateRegistrar().lookup(
+ "EJB3PersistenceManagerFactoryRegistry", PersistenceManagerFactoryRegistry.class);
+ pmFactoryRegistry.getFactories().put(BlockingPersistenceManagerFactory.REGISTRY_BIND_NAME,
+ BlockingPersistenceManagerFactory.class);
+
+ // Add the Force Passivation Cache
+ CacheFactoryRegistry cacheFactoryRegistry = Ejb3RegistrarLocator.locateRegistrar().lookup(
+ "EJB3CacheFactoryRegistry", CacheFactoryRegistry.class);
+ String forcePassivationCacheRegistryName = ForcePassivationCacheFactory.REGISTRY_BIND_NAME;
+ cacheFactoryRegistry.getFactories().put(forcePassivationCacheRegistryName, ForcePassivationCacheFactory.class);
+ log.info("Added " + forcePassivationCacheRegistryName);
+
+ // Deploy the test SFSB
+ Class<?> ejbImplClass = MyStatefulBean.class;
+ log.info("Deploying SFSB: " + ejbImplClass.getName());
+ container = deploySessionEjb(ejbImplClass);
+ }
+
+ @AfterClass
+ public static void afterClass() throws Exception
+ {
+ // Undeploy the test SFSB
+ undeployEjb(container);
+
+ AbstractEJB3TestCase.afterClass();
+ }
+
+}
Modified: projects/ejb3/trunk/core/src/test/resources/statefulcontainer-beans.xml
===================================================================
--- projects/ejb3/trunk/core/src/test/resources/statefulcontainer-beans.xml 2008-10-27 04:48:05 UTC (rev 80094)
+++ projects/ejb3/trunk/core/src/test/resources/statefulcontainer-beans.xml 2008-10-27 06:47:18 UTC (rev 80095)
@@ -9,13 +9,37 @@
<!-- StatefulSessionFilePersistenceManager -->
<entry>
<key>StatefulSessionFilePersistenceManager</key>
- <!-- A dummy persistence manager which doesn't rely on the jboss mbean -->
- <value>org.jboss.ejb3.test.cachepassivation.MyStatefulSessionFilePersistenceManagerFactory</value>
+ <value>org.jboss.ejb3.cache.simple.StatefulSessionFilePersistenceManagerFactory</value>
</entry>
</map>
</property>
</bean>
+ <!-- EJB3 Cache Factory Registry -->
+ <bean name="EJB3CacheFactoryRegistry" class="org.jboss.ejb3.cache.CacheFactoryRegistry">
+ <property name="factories">
+ <!-- Define each of the registered factories -->
+ <map class="java.util.HashMap" keyClass="java.lang.String"
+ valueClass="java.lang.Class">
+ <!-- NoPassivationCache -->
+ <entry>
+ <key>NoPassivationCache</key>
+ <value>org.jboss.ejb3.cache.NoPassivationCacheFactory</value>
+ </entry>
+ <!-- SimpleStatefulCache -->
+ <entry>
+ <key>SimpleStatefulCache</key>
+ <value>org.jboss.ejb3.cache.simple.SimpleStatefulCacheFactory</value>
+ </entry>
+ <!-- StatefulTreeCache -->
+ <entry>
+ <key>StatefulTreeCache</key>
+ <value>org.jboss.ejb3.cache.tree.StatefulTreeCacheFactory</value>
+ </entry>
+ </map>
+ </property>
+ </bean>
+
<!-- SFSB JNDI Registrar -->
<bean name="org.jboss.ejb3.JndiRegistrar.Session.SFSBJndiRegistrar"
class="org.jboss.ejb3.proxy.jndiregistrar.JndiStatefulSessionRegistrar">
More information about the jboss-cvs-commits
mailing list