avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/thread/src/test/org/apache/avalon/excalibur/thread/impl/test ResourceLimitingThreadPoolTestCase.java
Date Fri, 10 May 2002 14:46:58 GMT
leif        02/05/10 07:46:58

  Modified:    thread   build.xml
  Added:       thread/src/test/org/apache/avalon/excalibur/thread/impl/test
                        ResourceLimitingThreadPoolTestCase.java
  Log:
  Add a test of the ResourceLimitingThreadPool's functions.
  
  Revision  Changes    Path
  1.19      +4 -0      jakarta-avalon-excalibur/thread/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/thread/build.xml,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- build.xml	26 Apr 2002 01:36:23 -0000	1.18
  +++ build.xml	10 May 2002 14:46:57 -0000	1.19
  @@ -32,6 +32,8 @@
       <path id="test.class.path">
           <pathelement location="${build.testclasses}"/>
           <pathelement location="${junit.jar}"/>
  +        <pathelement location="${excalibur-collections.jar}"/>
  +        <pathelement location="${excalibur-testcase.jar}"/>
           <path refid="project.class.path"/>
       </path>
       <property name="cp" refid="test.class.path"/>
  @@ -53,6 +55,8 @@
           <!-- Need the jar to prevent recursive deps. -->
   
           <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkJUnit"/>
  +        <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkCollections"/>
  +        <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkTestcase"/>
       </target>
   
   
  
  
  
  1.1                  jakarta-avalon-excalibur/thread/src/test/org/apache/avalon/excalibur/thread/impl/test/ResourceLimitingThreadPoolTestCase.java
  
  Index: ResourceLimitingThreadPoolTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.pool.test;
  
  import com.clarkware.junitperf.ConstantTimer;
  import com.clarkware.junitperf.LoadTest;
  import com.clarkware.junitperf.TimedTest;
  import com.clarkware.junitperf.Timer;
  import junit.extensions.TestSetup;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.avalon.excalibur.thread.impl.ResourceLimitingThreadPool;
  import org.apache.avalon.excalibur.testcase.BufferedLogger;
  
  /**
   * @author <a href="mailto:leif@tanukisoftware.com">Leif Mortenson</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/05/10 14:46:58 $
   * @since 4.1
   */
  public final class ResourceLimitingThreadPoolTestCase
      extends TestCase
  {
      private volatile int m_completeCount;
      
      /*---------------------------------------------------------------
       * Constructors
       *-------------------------------------------------------------*/
      public ResourceLimitingThreadPoolTestCase()
      {
          this( "ResourceLimitingThreadPool Test Case" );
      }
  
      public ResourceLimitingThreadPoolTestCase( final String name )
      {
          super( name );
      }
  
      /*---------------------------------------------------------------
       * Suite
       *-------------------------------------------------------------*/
      public void test1Worker1Task()
      {
          commonTest( 1, 1, 0L, 200L, 1, true, true, -1, -1 );
      }
      
      public void test1Worker5Tasks()
      {
          // One will start immediately, 4 will have to wait 200ms each in turn.
          commonTest( 5, 1, 800L, 1000L, 1, true, true, -1, -1 );
      }
      
      public void test5Workers10Tasks()
      {
          // 5 will start immediately, 5 will have to wait 200ms for the first 5 to complete.
          commonTest( 10, 5, 200L, 400L, 5, true, true, -1, -1 );
      }
      
      public void test10Workers100Tasks()
      {
          // 10 will start immediately, next 10 will have to wait 200ms for the
          //  first 10 to complete and so on.
          commonTest( 100, 10, 1800L, 2000L, 10, true, true, -1, -1 );
      }
      
      public void test5Workers6TasksNoBlock()
      {
          commonTest( 6,  5,  0L, 200L, 5, true, false, -1, -1 );
      }
      
      public void test5Workers10TasksNotStrict()
      {
          commonTest( 10, 10, 0L, 200L, 5, false, false, -1, -1 );
      }
      
      private void incCompleteCount()
      {
          synchronized (this)
          {
              m_completeCount++;
          }
      }
      
      private void commonTest( int taskCount,
                               int firstSize,
                               long firstTime,
                               long totalTime,
                               int max,
                               boolean maxStrict,
                               boolean blocking,
                               long blockTimeout,
                               long trimInterval )
      {
          BufferedLogger logger = new BufferedLogger();
          ResourceLimitingThreadPool pool = new ResourceLimitingThreadPool(
              "Test Worker Pool", max, maxStrict, blocking, blockTimeout, trimInterval );
          pool.enableLogging( logger );
          
          Runnable runner = new Runnable()
                  {
                      public void run()
                      {
                          try
                          {
                              Thread.sleep( 200 );
                          }
                          catch (InterruptedException e)
                          {}
                          
                          incCompleteCount();
                      }
                  };
          
          long start = System.currentTimeMillis();
          m_completeCount = 0;
          for ( int i = 0; i < taskCount; i++ )
          {
              if ( maxStrict && ( !blocking ) && i >= max )
              {
                  // This request shoudl throw an exception.
                  try
                  {
                      pool.execute( runner );
                      fail( "Should have failed when requesting more than max resources."
);
                  }
                  catch ( Exception e )
                  {
                      // Ok
                      incCompleteCount();
                  }
              }
              else
              {
                  pool.execute( runner );
              }
          }
          long dur = System.currentTimeMillis() - start;
          
          // Make sure that the size of the pool is what is expected.
          assertEquals( "The pool size was not what it should be.", firstSize, pool.getSize()
);
          
          // Make sure this took about the right amount of time to get here.
          //System.out.println( "First time: " + dur );
          if ( Math.abs( dur - firstTime ) > 50 )
          {
              fail( "Time to start all tasks, " + dur + 
                  ", was not within 50ms of the expected time, " + firstTime );
          }
          
          // Wait for all worker threads to complete.
          while ( m_completeCount < taskCount )
          {
              try
              {
                  Thread.sleep( 10 );
              }
              catch (InterruptedException e)
              {}
          }
          
          dur = System.currentTimeMillis() - start;
          
          // Make sure this took about the right amount of time to get here.
          //System.out.println( "Total time: " + dur );
          if ( Math.abs( dur - totalTime ) > 50 )
          {
              fail( "Time to complete all tasks, " + dur + 
                  ", was not within 50ms of the expected time, " + totalTime );
          }
          
          //System.out.println( logger.toString() );
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message