You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2002/02/12 07:37:12 UTC

cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/pool/test MultiThreadedPoolComparisonProfile.java

leif        02/02/11 22:37:11

  Modified:    src/scratchpad/org/apache/avalon/excalibur/pool/test
                        MultiThreadedPoolComparisonProfile.java
  Log:
  Add class to make it easier to write multithreaded tests.
  
  Revision  Changes    Path
  1.2       +51 -110   jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/pool/test/MultiThreadedPoolComparisonProfile.java
  
  Index: MultiThreadedPoolComparisonProfile.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/pool/test/MultiThreadedPoolComparisonProfile.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MultiThreadedPoolComparisonProfile.java	2 Feb 2002 17:31:32 -0000	1.1
  +++ MultiThreadedPoolComparisonProfile.java	12 Feb 2002 06:37:11 -0000	1.2
  @@ -19,6 +19,7 @@
   import org.apache.avalon.excalibur.pool.SoftResourceLimitingPool;
   import org.apache.avalon.excalibur.pool.VariableSizePool;
   import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError;
  +import org.apache.avalon.excalibur.testcase.LatchedThreadGroup;
   
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.logger.LogEnabled;
  @@ -30,19 +31,19 @@
    *  given a single access thread.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version $Id: MultiThreadedPoolComparisonProfile.java,v 1.1 2002/02/02 17:31:32 leif Exp $
  + * @version $Id: MultiThreadedPoolComparisonProfile.java,v 1.2 2002/02/12 06:37:11 leif Exp $
    */
   public class MultiThreadedPoolComparisonProfile
       extends PoolComparisonProfileAbstract
   {
       protected static final int THREADS = 10;
       
  -    private Object  m_semaphore = new Object();
  -    private int     m_startedCount;
  -    private boolean m_latched;
  -    private int     m_completedCount;
  -    private int     m_getCount;
  -    private Exception m_exception;
  +    private Object    m_semaphore = new Object();
  +    private int       m_startedCount;
  +    private boolean   m_latched;
  +    private int       m_completedCount;
  +    private int       m_getCount;
  +    private Throwable m_throwable;
       
       /*---------------------------------------------------------------
        * Constructors
  @@ -63,142 +64,82 @@
               fail( "gets must be evenly divisible by THREADS");
           }
           
  -        m_startedCount = 0;
  -        m_latched = false;
  -        m_completedCount = 0;
           m_getCount = 0;
  -        m_exception = null;
  +        m_throwable = null;
           
  -        // Create the threads.  Implement a simple latch so they all run at the same time
  -        Thread[] threads = new Thread[THREADS];
  -        for ( int i = 0; i < THREADS; i++ )
  +        // Create the runnable
  +        Runnable runnable = new Runnable()
           {
  -            Runnable runnable = new Runnable()
  +            public void run()
               {
  -                public void run()
  +                // Perform this threads part of the test.
  +                final int cnt = gets / THREADS;
  +                final Poolable[] poolTmp = new Poolable[cnt];
  +                final int loops = TEST_SIZE / gets;
  +                for( int i = 0; i < loops; i++ )
                   {
  -                    // Need all threads to wait until all the others are ready.
  -                    synchronized(m_semaphore)
  +                    // Get some Poolables
  +                    for ( int j = 0; j < cnt; j++ )
                       {
  -                        m_startedCount++;
  -                        m_logger.debug( "Started " + m_startedCount + " test threads." );
  -                        if ( m_startedCount >= THREADS )
  +                        try
                           {
  -                            m_semaphore.notifyAll();
  -                        }
  -                        while ( !m_latched )
  -                        {
  -                            try
  +                            poolTmp[j] = pool.get();
  +                            synchronized(m_semaphore)
                               {
  -                                m_semaphore.wait();
  +                                m_getCount++;
                               }
  -                            catch ( InterruptedException e ) {}
                           }
  -                    }
  -                    
  -                    try
  -                    {
  -                        // Perform this threads part of the test.
  -                        final int cnt = gets / THREADS;
  -                        final Poolable[] poolTmp = new Poolable[cnt];
  -                        final int loops = TEST_SIZE / gets;
  -                        for( int i = 0; i < loops; i++ )
  +                        catch ( Throwable t )
                           {
  -                            // Get some Poolables
  -                            for ( int j = 0; j < cnt; j++ )
  -                            {
  -                                try
  -                                {
  -                                    poolTmp[j] = pool.get();
  -                                    synchronized(m_semaphore)
  -                                    {
  -                                        m_getCount++;
  -                                    }
  -                                }
  -                                catch ( Exception e )
  -                                {
  -                                    m_poolLogger.error( "Unexpected error", e );
  -                                    
  -                                    synchronized(m_semaphore)
  -                                    {
  -                                        if (m_exception == null) {
  -                                            m_exception = e;
  -                                        }
  -                                    }
  -                                    return;
  -                                }
  -                            }
  +                            m_poolLogger.error( "Unexpected error", t );
                               
  -                            // Make the loops hold the poolables longer than they are released, but only slightly.
  -                            Thread.yield();
  -                            
  -                            // Put the Poolables back
  -                            for ( int j = 0; j < cnt; j++ )
  +                            synchronized(m_semaphore)
                               {
  -                                pool.put( poolTmp[j] );
  -                                synchronized(m_semaphore)
  -                                {
  -                                    m_getCount--;
  +                                if (m_throwable == null) {
  +                                    m_throwable = t;
                                   }
  -                                poolTmp[j] = null;
                               }
  +                            return;
                           }
                       }
                       
  +                    // Make the loops hold the poolables longer than they are released, but only slightly.
  +                    Thread.yield();
                       
  -                    finally
  +                    // Put the Poolables back
  +                    for ( int j = 0; j < cnt; j++ )
                       {
  -                        // Say that we are done
  +                        pool.put( poolTmp[j] );
                           synchronized(m_semaphore)
                           {
  -                            m_completedCount++;
  -                            m_logger.debug( m_completedCount + " test threads completed." );
  -                            m_semaphore.notifyAll();
  +                            m_getCount--;
                           }
  +                        poolTmp[j] = null;
                       }
                   }
  -            };
  -            
  -            threads[i] = new Thread( runnable, "Test_Thread_" + i );
  -            threads[i].start();
  -        }
  +            }
  +        };
  +        
  +        LatchedThreadGroup group = new LatchedThreadGroup( runnable, THREADS );
  +        group.enableLogging( m_logger );
   
  -        // Wait for all of the threads to start before starting to time the test
  -        synchronized(m_semaphore)
  +        long duration;
  +        try
           {
  -            while ( m_startedCount < THREADS ) {
  -                m_semaphore.wait();
  -            }
  -            
  -            // Start clean
  -            resetMemory();
  -            
  -            // Release the threads.
  -            m_latched = true;
  -            m_logger.debug( "Main thread released the test thread latch." );
  -            m_semaphore.notifyAll();
  +            duration = group.go();
           }
  -        // Start timing
  -        long startTime = System.currentTimeMillis();
  -        
  -        // Wait for all of the threads to complete
  -        synchronized(m_semaphore)
  +        catch ( Throwable t )
           {
  -            m_logger.debug( "Waiting for test threads to all complete." );
  -            while ( m_completedCount < THREADS ) {
  -                try
  -                {
  -                    m_semaphore.wait();
  -                }
  -                catch ( InterruptedException e ) {}
  +            // Throwable could have been thrown by one of the tests.
  +            if (m_throwable == null) {
  +                m_throwable = t;
               }
  +            duration = 0;
           }
  -        final long duration = System.currentTimeMillis() - startTime;
  -        m_logger.debug( "All test threads completed." );
           
  -        if ( m_exception != null )
  +        if ( m_throwable != null )
           {
  -            throw new CascadingAssertionFailedError( "Exception in test thread.", m_exception );
  +            throw new CascadingAssertionFailedError( "Exception in test thread.", m_throwable );
           }
           
           assertTrue( "m_getCount == 0 (" + m_getCount + ")", m_getCount == 0 );
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>