[Jboss-cvs] JBossAS SVN: r56251 - branches/JBoss_4_0_3_SP1_JBAS_3453/testsuite/src/main/org/jboss/test/jca/test

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Aug 25 06:32:55 EDT 2006


Author: weston.price at jboss.com
Date: 2006-08-25 06:32:54 -0400 (Fri, 25 Aug 2006)
New Revision: 56251

Added:
   branches/JBoss_4_0_3_SP1_JBAS_3453/testsuite/src/main/org/jboss/test/jca/test/PurgePolicyStressTestCase.java
Log:
[JBAS-3453] Unit test cases for both purge policy, as well as external
code support.

Added: branches/JBoss_4_0_3_SP1_JBAS_3453/testsuite/src/main/org/jboss/test/jca/test/PurgePolicyStressTestCase.java
===================================================================
--- branches/JBoss_4_0_3_SP1_JBAS_3453/testsuite/src/main/org/jboss/test/jca/test/PurgePolicyStressTestCase.java	2006-08-25 09:33:00 UTC (rev 56250)
+++ branches/JBoss_4_0_3_SP1_JBAS_3453/testsuite/src/main/org/jboss/test/jca/test/PurgePolicyStressTestCase.java	2006-08-25 10:32:54 UTC (rev 56251)
@@ -0,0 +1,265 @@
+package org.jboss.test.jca.test;
+
+import javax.resource.ResourceException;
+import javax.resource.spi.ConnectionRequestInfo;
+import javax.resource.spi.ManagedConnectionFactory;
+import javax.security.auth.Subject;
+
+import org.jboss.logging.Logger;
+import org.jboss.resource.connectionmanager.BaseConnectionManager2;
+import org.jboss.resource.connectionmanager.CachedConnectionManager;
+import org.jboss.resource.connectionmanager.ConnectionListener;
+import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
+import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
+import org.jboss.resource.connectionmanager.ManagedConnectionPool;
+import org.jboss.resource.connectionmanager.NoTxConnectionManager;
+import org.jboss.resource.connectionmanager.PurgePolicy;
+import org.jboss.test.JBossTestCase;
+import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
+import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
+import org.jboss.test.jca.test.BaseConnectionManagerStressTestCase.TestPool;
+
+public class PurgePolicyStressTestCase extends JBossTestCase
+{
+   public PurgePolicyStressTestCase(String name)
+   {
+
+      super(name);
+   }
+
+   Logger log = Logger.getLogger(getClass());
+
+   boolean failed;
+   ResourceException error;
+   int startedThreadCount;
+   final Object startedLock = new Object();
+   int finishedThreadCount;
+   final Object finishedLock = new Object();
+   int connectionCount;
+   int errorCount;
+   float elapsed = 0;
+   float held = 0;
+   float getConnection = 0;
+   float returnConnection = 0;
+
+   Subject subject = new Subject();
+   ConnectionRequestInfo cri = new TestConnectionRequestInfo();
+   CachedConnectionManager ccm = new CachedConnectionManager();
+
+
+   private BaseConnectionManager2 getCM(
+      InternalManagedConnectionPool.PoolParams pp)
+      throws Exception
+   {
+      ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
+      ManagedConnectionPool poolingStrategy = new TestPool(mcf, pp, false, log);
+      BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy);
+      poolingStrategy.setConnectionListenerFactory(cm);
+      return cm;
+   }
+
+
+   private void shutdown(BaseConnectionManager2 cm)
+   {
+      TestPool pool = (TestPool) cm.getPoolingStrategy();
+      pool.shutdown();
+   }
+
+   protected void setUp()
+   {
+      log.debug("================> Start " + getName());
+   }
+
+   protected void tearDown()
+   {
+      log.debug("================> End " + getName());
+   }
+
+   public void testShortBlockingNoFill()
+      throws Exception
+   {
+      doShortBlocking(20, 0, 5000);
+   }
+
+   public void testShortBlockingFill()
+      throws Exception
+   {
+      doShortBlocking(20, getBeanCount(), 5000);
+   }
+
+   public void testShortBlockingPartFill()
+      throws Exception
+   {
+      doShortBlocking(20, getBeanCount()/2, 5000);
+   }
+
+   public void testShortBlockingNearlyFill()
+      throws Exception
+   {
+      doShortBlocking(20, getBeanCount() - 1, 5000);
+   }
+
+   public void testShortBlockingAggressiveRemoval()
+      throws Exception
+   {
+      doShortBlocking(20, 0, 10);
+   }
+
+   public void testShortBlockingAggressiveRemovalAndFill()
+      throws Exception
+   {
+      doShortBlocking(20, getBeanCount(), 10);
+   }
+
+   /**
+    * The testShortBlocking test tries to simulate extremely high load on the pool,
+    * with a short blocking timeout.  It tests fairness in scheduling servicing
+    * requests. The work time is modeled by sleepTime. Allowing overhead of
+    * 15 ms/pool request, the blocking is calculated at
+    * (worktime + overhead) * (threadsPerConnection)
+    *
+    * @exception Exception if an error occurs
+    */
+   public void doShortBlocking(long sleep, int min, long idle) throws Exception
+   {  
+      startedThreadCount = 0;
+      finishedThreadCount = 0;
+      connectionCount = 0;
+      errorCount = 0;
+
+      final int reps = getIterationCount();
+      final int threadsPerConnection = getThreadCount();
+      final long sleepTime = sleep;
+      failed = false;
+      InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
+      pp.minSize = min;
+      pp.maxSize = getBeanCount();
+      pp.blockingTimeout =  (threadsPerConnection) * ((int)sleepTime + 15);
+      if (pp.blockingTimeout < 1000)
+         pp.blockingTimeout = 1000;
+      pp.idleTimeout = idle;
+      pp.purgePolilcy = PurgePolicy.POOL;
+      
+      final BaseConnectionManager2 cm = getCM(pp);
+      try
+      {
+         int totalThreads = pp.maxSize * threadsPerConnection;
+         log.info("ShortBlocking test with connections: " + pp.maxSize + " totalThreads: " + totalThreads + " reps: " + reps);
+         for (int i = 0; i < totalThreads; i++)
+         {
+            Runnable t = new Runnable()
+            {
+               int id;
+               public void run()
+               {
+                  synchronized (startedLock)
+                  {
+                     id = startedThreadCount;
+                     startedThreadCount++;
+                     startedLock.notify();
+                  }
+                  long duration = 0;
+                  long getConnection = 0;
+                  long returnConnection = 0;
+                  long heldConnection = 0;
+                  for (int j = 0; j < reps; j++)
+                  {
+                     try
+                     {
+                        long startGetConnection = System.currentTimeMillis();
+                        ConnectionListener cl = cm.getManagedConnection(null, null);
+                        long endGetConnection = System.currentTimeMillis();
+                        //maybe should be synchronized
+                        PurgePolicyStressTestCase.this.connectionCount++;
+                        Thread.sleep(sleepTime);
+                        long startReturnConnection = System.currentTimeMillis();
+                        cm.returnManagedConnection(cl, false);
+                        long endReturnConnection = System.currentTimeMillis();
+                        
+                        duration += (endReturnConnection - startGetConnection);
+                        getConnection += (endGetConnection - startGetConnection);
+                        returnConnection += (endReturnConnection - startReturnConnection);
+                        heldConnection += (startReturnConnection - endGetConnection);
+                      }
+                      catch (ResourceException re)
+                      {
+                         PurgePolicyStressTestCase.this.log.info("error: iterationCount: " + j + ", connectionCount: " + PurgePolicyStressTestCase.this.connectionCount + " " + re.getMessage());
+                         PurgePolicyStressTestCase.this.errorCount++;
+                         PurgePolicyStressTestCase.this.error = re;
+                         PurgePolicyStressTestCase.this.failed = true;
+                      } // end of try-catch
+                      catch (InterruptedException ie)
+                      {
+                         break;
+                      } // end of catch
+
+
+                   }
+                   synchronized (PurgePolicyStressTestCase.this)
+                   {
+                      PurgePolicyStressTestCase.this.elapsed += duration;
+                      PurgePolicyStressTestCase.this.getConnection += getConnection;
+                      PurgePolicyStressTestCase.this.returnConnection += returnConnection;
+                      PurgePolicyStressTestCase.this.held += heldConnection;
+                   }
+                   synchronized (finishedLock)
+                   {
+                     finishedThreadCount++;
+                     finishedLock.notify();
+                  }
+               }
+            };
+            new Thread(t).start();
+            synchronized (startedLock)
+            {
+               while (startedThreadCount < i + 1)
+               {
+                  startedLock.wait();
+               } // end of while ()
+            }
+         } // end of for ()
+         synchronized (finishedLock)
+         {
+            while (finishedThreadCount < totalThreads)
+            {
+               finishedLock.wait();
+            } // end of while ()
+         }
+         
+         // Stop the pool/idle remover, otherwise the following checks will be random
+         TestPool pool = (TestPool) cm.getPoolingStrategy();
+         pool.shutdownWithoutClear();
+         
+         float expected = totalThreads * reps;
+         float lessWaiting = getConnection - (threadsPerConnection - 1) * held;
+         log.info("completed " + getName() + " with connectionCount: " + connectionCount + ", expected : " + expected);
+         log.info("errorCount: " + errorCount + " %error=" + ((100 * errorCount) / expected));
+         log.info("Total time elapsed: " + elapsed  + ", perRequest: " + (elapsed / connectionCount));
+         log.info("Total time held   : " + held  + ", perRequest: " + (held / connectionCount));
+         log.info("Time getConnection: " + getConnection  + ", perRequest: " + (getConnection / connectionCount));
+         log.info("     lessWaiting  : " + lessWaiting  + ", perRequest: " + (lessWaiting / connectionCount));
+         log.info("Time retConnection: " + returnConnection  + ", perRequest: " + (returnConnection / connectionCount));
+         int available = (int) pool.getAvailableConnectionCount();
+         assertTrue("Wrong number of connections counted: " + available, available == pp.maxSize);
+         assertTrue("Blocking Timeout occurred in ShortBlocking test: " + error, !failed);
+      }
+      finally
+      {
+         shutdown(cm);
+      }
+   }
+   
+   public class TestPool extends JBossManagedConnectionPool.OnePool
+   {
+      public TestPool(final ManagedConnectionFactory mcf, final InternalManagedConnectionPool.PoolParams poolParams,
+            final boolean noTxSeparatePools, final Logger log)
+      {
+         super(mcf, poolParams, noTxSeparatePools, log);
+      }
+
+      public void shutdownWithoutClear()
+      {
+         super.shutdownWithoutClear();
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list