[jboss-cvs] JBossAS SVN: r89658 - in projects/ejb3/trunk: testsuite/src/test/java/org/jboss/ejb3/test/stateless/unit and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Jun 2 06:12:02 EDT 2009
Author: jaikiran
Date: 2009-06-02 06:12:02 -0400 (Tue, 02 Jun 2009)
New Revision: 89658
Modified:
projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/AbstractPool.java
projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/StrictMaxPool.java
projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/stateless/unit/MetricsUnitTestCase.java
Log:
EJBTHREE-1840 Fixed the StrictMaxPool to return the correct AvailableCount
Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/AbstractPool.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/AbstractPool.java 2009-06-02 10:03:01 UTC (rev 89657)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/AbstractPool.java 2009-06-02 10:12:02 UTC (rev 89658)
@@ -52,16 +52,16 @@
{
return createCount;
}
-
+
public int getRemoveCount()
{
return removeCount;
}
-
+
public void initialize(Container container, int maxSize, long timeout)
{
assert container != null : "container is null";
-
+
this.container = container;
}
@@ -72,7 +72,7 @@
{
return create(null, null);
}
-
+
protected BeanContext<?> create(Class[] initTypes, Object[] initValues)
{
BeanContext ctx;
@@ -89,24 +89,45 @@
{
container.popContext();
}
-
+
container.invokePostConstruct(ctx, initValues);
-
+
//TODO This needs to be reimplemented as replacement for create() on home interface
container.invokeInit(ctx.getInstance(), initTypes, initValues);
-
+
++createCount;
-
+
return ctx;
}
-
+
private BeanContext createBeanContext()
{
return container.createBeanContext();
}
-
+
public void remove(BeanContext ctx)
{
+ this.doRemove(ctx);
+ }
+
+ public void discard(BeanContext<?> ctx)
+ {
+ this.doRemove(ctx);
+ }
+
+ public void setInjectors(Injector[] injectors)
+ {
+ this.injectors = injectors;
+ }
+
+ /**
+ * Remove the bean context and invoke any callbacks
+ * and track the remove count
+ *
+ * @param ctx
+ */
+ protected void doRemove(BeanContext<?> ctx)
+ {
try
{
container.invokePreDestroy(ctx);
@@ -117,14 +138,4 @@
++removeCount;
}
}
-
- public void discard(BeanContext<?> ctx)
- {
- remove(ctx);
- }
-
- public void setInjectors(Injector[] injectors)
- {
- this.injectors = injectors;
- }
}
Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/StrictMaxPool.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/StrictMaxPool.java 2009-06-02 10:03:01 UTC (rev 89657)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/pool/StrictMaxPool.java 2009-06-02 10:12:02 UTC (rev 89658)
@@ -83,22 +83,22 @@
this.strictMaxSize = new Semaphore(maxSize, true);
this.strictTimeout = timeout;
}
-
+
public int getCurrentSize()
{
return getCreateCount() - getRemoveCount();
}
-
+
public int getAvailableCount()
{
return maxSize - inUse;
}
-
+
public int getMaxSize()
{
return maxSize;
}
-
+
public void setMaxSize(int maxSize)
{
this.maxSize = maxSize;
@@ -144,7 +144,7 @@
// Pool is empty, create an instance
++inUse;
return create();
-
+
}
public BeanContext get(Class[] initTypes, Object[] initValues)
@@ -152,7 +152,7 @@
boolean trace = log.isTraceEnabled();
if (trace)
log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass());
-
+
// Block until an instance is available
try
{
@@ -223,14 +223,14 @@
catch (Exception ignored)
{
}
-
+
}
public void destroy()
{
freeAll();
}
-
+
public void discard(BeanContext ctx)
{
if (log.isTraceEnabled())
@@ -244,8 +244,8 @@
strictMaxSize.release();
--inUse;
- // Throw away, unsetContext()
- super.discard(ctx);
+ // Let the super do any other remove stuff
+ super.doRemove(ctx);
}
// Package protected ---------------------------------------------
@@ -275,7 +275,7 @@
}
pool.clear();
inUse = 0;
-
+
}
// Inner classes -------------------------------------------------
@@ -291,7 +291,7 @@
strictMaxSize.release();
--inUse;
-
- super.remove(ctx);
+ // let the super do the other remove stuff
+ super.doRemove(ctx);
}
}
Modified: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/stateless/unit/MetricsUnitTestCase.java
===================================================================
--- projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/stateless/unit/MetricsUnitTestCase.java 2009-06-02 10:03:01 UTC (rev 89657)
+++ projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/stateless/unit/MetricsUnitTestCase.java 2009-06-02 10:12:02 UTC (rev 89658)
@@ -24,10 +24,11 @@
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
+import junit.framework.Test;
+
import org.jboss.ejb3.test.stateless.StatelessRemote;
import org.jboss.logging.Logger;
import org.jboss.test.JBossTestCase;
-import junit.framework.Test;
/**
@@ -42,76 +43,77 @@
{
super(name);
}
-
+
public void testDefaultJmxMetrics() throws Exception
{
MBeanServerConnection server = getServer();
ObjectName testerName = new ObjectName("jboss.j2ee:jar=stateless-test.jar,name=DefaultPoolStatelessBean,service=EJB3");
-
+
int size = 0;
-
+
size = (Integer)server.getAttribute(testerName, "CurrentSize");
assertEquals(0, size);
-
+
size = (Integer)server.getAttribute(testerName, "CreateCount");
assertEquals(0, size);
-
+
StatelessRemote stateless = (StatelessRemote)getInitialContext().lookup("DefaultPoolStatelessBean/remote");
assertNotNull(stateless);
- stateless.test();
-
+ stateless.test();
+
size = (Integer)server.getAttribute(testerName, "CurrentSize");
assertEquals(1, size);
-
+
size = (Integer)server.getAttribute(testerName, "AvailableCount");
assertEquals(30, size);
-
+
size = (Integer)server.getAttribute(testerName, "MaxSize");
assertEquals(30, size);
-
+
size = (Integer)server.getAttribute(testerName, "CreateCount");
assertEquals(1, size);
-
+
size = (Integer)server.getAttribute(testerName, "RemoveCount");
assertEquals(0, size);
-
+
}
-
+
public void testStrictMaxPoolJmxMetrics() throws Exception
{
MBeanServerConnection server = getServer();
ObjectName testerName = new ObjectName("jboss.j2ee:jar=stateless-test.jar,name=StrictMaxPoolStatelessBean,service=EJB3");
int size = 0;
-
+
size = (Integer)server.getAttribute(testerName, "CurrentSize");
assertEquals(0, size);
-
+
size = (Integer)server.getAttribute(testerName, "CreateCount");
assertEquals(0, size);
-
+
StatelessRemote stateless = (StatelessRemote)getInitialContext().lookup("StrictMaxPoolStatelessBean/remote");
assertNotNull(stateless);
- stateless.test();
-
+ stateless.test();
+
size = (Integer)server.getAttribute(testerName, "CurrentSize");
assertEquals(1, size);
-
+
size = (Integer)server.getAttribute(testerName, "AvailableCount");
assertEquals(3, size);
-
+
size = (Integer)server.getAttribute(testerName, "MaxSize");
assertEquals(3, size);
-
- size = (Integer)server.getAttribute(testerName, "CreateCount");
- assertEquals(1, size);
-
+
+ int createCount = (Integer)server.getAttribute(testerName, "CreateCount");
+ assertEquals(1, createCount);
+
runConcurrentTests(20, 1);
-
+
int currentSize = (Integer)server.getAttribute(testerName, "CurrentSize");
- assertEquals(1, size);
-
+
checkMetrics(server, testerName, currentSize, 3, 3, currentSize, 0);
-
+
+ createCount = (Integer)server.getAttribute(testerName, "CreateCount");
+
for (int i = 1 ; i <= 10 ; ++i)
{
try
@@ -126,22 +128,52 @@
assertEquals(i, removeCount);
}
}
-
+ // New instances should have been created in the loop above,
+ // since 10 bean instances were discarded because of exception.
+ int createCountAfterExceptionOnBeanInstances = (Integer)server.getAttribute(testerName, "CreateCount");
+ assertTrue("New instances not created", createCountAfterExceptionOnBeanInstances > createCount);
+
+
+ // 10 method calls led to exceptions so 10 bean instances would be removed
+ int removedCount = (Integer)server.getAttribute(testerName, "RemoveCount");
+ assertEquals("Removed count does not match", 10, removedCount);
+
+ // current size (created - removed) should now be zero
+ // since all the created ones have been removed after exception
+ currentSize = (Integer)server.getAttribute(testerName, "CurrentSize");
+ assertEquals("Current size is incorrect", 0, currentSize);
+
+ // none of the instances are in use, so available count should be
+ // max (=3)
+ int availableCount = (Integer) server.getAttribute(testerName, "AvailableCount");
+ assertEquals("Available count does not match", 3, availableCount);
+
+
+ // one more round of concurrent invocations
runConcurrentTests(20, 1);
-
- currentSize = (Integer)server.getAttribute(testerName, "CurrentSize");
- assertEquals(1, size);
-
- checkMetrics(server, testerName, currentSize, 3, 3, currentSize + 10, 10);
-
+
+ // again there should be no "inUse" instances and hence available count
+ // should be equal to max (=3)
+ availableCount = (Integer) server.getAttribute(testerName, "AvailableCount");
+ assertEquals("Available count not matching", 3, availableCount);
+
+ // some new instances should have been created in runconcurrent tests,
+ // since all the bean instances were earlier removed because of exception.
+ // We won't know the exact count because of the thread concurrency involved
+ // but the create count should definitely be greater than the earlier
+ // create count
+ createCount = (Integer)server.getAttribute(testerName, "CreateCount");
+ assertTrue("Create count does not match after concurrent test",createCount > createCountAfterExceptionOnBeanInstances);
+
+
Runnable r = new Runnable()
{
public void run()
{
try
{
- StatelessRemote stateless = (StatelessRemote)getInitialContext().lookup("StrictMaxPoolStatelessBean/remote");
- stateless.delay();
+ StatelessRemote stateless = (StatelessRemote)getInitialContext().lookup("StrictMaxPoolStatelessBean/remote");
+ stateless.delay();
}
catch (Exception e)
{
@@ -149,54 +181,54 @@
}
}
};
-
+
new Thread(r).start();
-
+
Thread.sleep(10 * 1000);
-
+
int maxSize = (Integer)server.getAttribute(testerName, "MaxSize");
System.out.println("MaxSize=" + maxSize);
-
- int availableCount = (Integer)server.getAttribute(testerName, "AvailableCount");
+
+ availableCount = (Integer)server.getAttribute(testerName, "AvailableCount");
System.out.println("AvailableCount=" + availableCount);
-
+
assertEquals(maxSize - 1, availableCount);
}
-
+
protected void checkMetrics(MBeanServerConnection server, ObjectName testerName, int current, int available, int max, int create, int remove)
throws Exception
{
-
+
int currentSize = (Integer)server.getAttribute(testerName, "CurrentSize");
System.out.println("CurrentSize=" + currentSize);
-
+
int availableCount = (Integer)server.getAttribute(testerName, "AvailableCount");
System.out.println("AvailableCount=" + availableCount);
-
+
int maxSize = (Integer)server.getAttribute(testerName, "MaxSize");
System.out.println("MaxSize=" + maxSize);
-
+
int createCount = (Integer)server.getAttribute(testerName, "CreateCount");
System.out.println("CreateCount=" + createCount);
-
+
int removeCount = (Integer)server.getAttribute(testerName, "RemoveCount");
System.out.println("RemoveCount=" + removeCount);
-
+
if (availableCount != maxSize)
{
System.out.println("Waiting to stabilize ... " + availableCount + "<" + maxSize);
Thread.sleep(1 * 60 * 1000);
availableCount = (Integer)server.getAttribute(testerName, "AvailableCount");
}
-
+
assertEquals(current, currentSize);
assertEquals(available, availableCount);
assertEquals(max, maxSize);
assertEquals(create, createCount);
assertEquals(remove, removeCount);
-
+
}
-
+
protected void runConcurrentTests(int numThreads, int sleepSecs) throws Exception
{
for (int i = 0 ; i < numThreads ; ++i)
@@ -207,10 +239,10 @@
{
try
{
- StatelessRemote stateless = (StatelessRemote)getInitialContext().lookup("StrictMaxPoolStatelessBean/remote");
+ StatelessRemote stateless = (StatelessRemote)getInitialContext().lookup("StrictMaxPoolStatelessBean/remote");
for (int i = 0 ; i < 25 ; ++i)
- {
- stateless.test();
+ {
+ stateless.test();
}
}
catch (Exception e)
@@ -219,11 +251,11 @@
}
}
};
-
+
new Thread(r).start();
}
-
- Thread.sleep(sleepSecs * 60 * 1000);
+
+ Thread.sleep(sleepSecs * 60 * 1000);
}
public static Test suite() throws Exception
More information about the jboss-cvs-commits
mailing list