cvs commit: spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers MockThreadControl.java MockThreadPool.java ThreadPerRequestHandlerTestCase.java

Peter Donald <[email protected]>
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
donaldp     03/10/28 19:51:59

  Modified:    components/netserve/src/test/org/jcomponent/netserve/connection/handlers
                        ThreadPerRequestHandlerTestCase.java
  Added:       components/netserve/src/test/org/jcomponent/netserve/connection/handlers
                        MockThreadControl.java MockThreadPool.java
  Log:
  Add in shutdown of worker thread for per-thread handler and unit test it
  
  Revision  Changes    Path
  1.2       +58 -2     spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/ThreadPerRequestHandlerTestCase.java
  
  Index: ThreadPerRequestHandlerTestCase.java
  ===================================================================
  RCS file: /cvsroot/spice/spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/ThreadPerRequestHandlerTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ThreadPerRequestHandlerTestCase.java	29 Oct 2003 03:00:14 -0000	1.1
  +++ ThreadPerRequestHandlerTestCase.java	29 Oct 2003 03:51:59 -0000	1.2
  @@ -12,12 +12,13 @@
   import com.mockobjects.dynamic.C;
   import org.jcomponent.netserve.connection.RequestHandler;
   import org.jcomponent.threadpool.ThreadPool;
  +import org.jcomponent.threadpool.ThreadControl;
   import java.net.Socket;
   
   /**
    *
    * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
  - * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
  + * @version $Revision: 1.2 $ $Date: 2003/10/29 03:51:59 $
    */
   public class ThreadPerRequestHandlerTestCase
       extends TestCase
  @@ -42,11 +43,14 @@
       public void testThreadPoolInvoked()
           throws Exception
       {
  +        final Mock mockControl = new Mock( ThreadControl.class );
  +        final ThreadControl control = (ThreadControl)mockControl.proxy();
  +
           final Mock mockHandler = new Mock( RequestHandler.class );
           final RequestHandler handler = (RequestHandler)mockHandler.proxy();
   
           final Mock mockPool = new Mock( ThreadPool.class );
  -        mockPool.expectAndReturn( "execute", C.args( C.isA( Runnable.class ) ), null );
  +        mockPool.expectAndReturn( "execute", C.args( C.isA( Runnable.class ) ), control );
           final ThreadPool threadPool = (ThreadPool)mockPool.proxy();
   
           final ThreadPerRequestHandler requestHandler =
  @@ -55,5 +59,57 @@
   
           mockHandler.verify();
           mockPool.verify();
  +        mockControl.verify();
  +    }
  +
  +    public void testShutdownWhileThreadStillGoingButInteruptible()
  +        throws Exception
  +    {
  +        final DelayingRequestHandler handler =
  +            new DelayingRequestHandler( 2000, true );
  +
  +        final ThreadPerRequestHandler requestHandler =
  +            new ThreadPerRequestHandler( handler, new MockThreadPool() );
  +        requestHandler.handleConnection( new Socket() );
  +        Thread.sleep( 50 );
  +
  +        requestHandler.shutdown( 50 );
  +        assertEquals( "isShutdown", true, handler.isShutdown() );
  +        assertEquals( "isExited", true, handler.isExited() );
  +        assertEquals( "isExitDueToInterrupt", true, handler.isExitDueToInterrupt() );
  +    }
  +
  +    public void testShutdownWhileThreadStillGoingAndNotInteruptible()
  +        throws Exception
  +    {
  +        final DelayingRequestHandler handler =
  +            new DelayingRequestHandler( 2000, false );
  +
  +        final ThreadPerRequestHandler requestHandler =
  +            new ThreadPerRequestHandler( handler, new MockThreadPool() );
  +        requestHandler.handleConnection( new Socket() );
  +        Thread.sleep( 50 );
  +
  +        requestHandler.shutdown( 50 );
  +        assertEquals( "isShutdown", true, handler.isShutdown() );
  +        assertEquals( "isExited", false, handler.isExited() );
  +        assertEquals( "isExitDueToInterrupt", false, handler.isExitDueToInterrupt() );
  +    }
  +
  +    public void testShutdownWhileThreadStillGoingAndWaitIndefinetly()
  +        throws Exception
  +    {
  +        final DelayingRequestHandler handler =
  +            new DelayingRequestHandler( 200, false );
  +
  +        final ThreadPerRequestHandler requestHandler =
  +            new ThreadPerRequestHandler( handler, new MockThreadPool() );
  +        requestHandler.handleConnection( new Socket() );
  +        Thread.sleep( 50 );
  +
  +        requestHandler.shutdown( 0 );
  +        assertEquals( "isShutdown", true, handler.isShutdown() );
  +        assertEquals( "isExited", true, handler.isExited() );
  +        assertEquals( "isExitDueToInterrupt", false, handler.isExitDueToInterrupt() );
       }
   }
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/MockThreadControl.java
  
  Index: MockThreadControl.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import org.jcomponent.threadpool.ThreadControl;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:51:59 $
   */
  class MockThreadControl
      implements ThreadControl
  {
      private final Thread m_thread;
  
      public MockThreadControl( Thread thread )
      {
          m_thread = thread;
      }
  
      public void join( long milliSeconds )
          throws InterruptedException
      {
          m_thread.join( milliSeconds );
      }
  
      public void interrupt()
          throws IllegalStateException, SecurityException
      {
          m_thread.interrupt();
      }
  
      public boolean isFinished()
      {
          return !m_thread.isAlive();
      }
  
      public Throwable getThrowable()
      {
          return null;
      }
  }
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/MockThreadPool.java
  
  Index: MockThreadPool.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import org.jcomponent.threadpool.ThreadPool;
  import org.jcomponent.threadpool.ThreadControl;
  import org.jcomponent.threadpool.Executable;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:51:59 $
   */
  class MockThreadPool
      implements ThreadPool
  {
      public ThreadControl execute( Runnable work )
      {
          final Thread thread = new Thread( work );
          thread.start();
          return new MockThreadControl( thread );
      }
  
      public ThreadControl execute( Executable work )
      {
          return null;
      }
  }
  
  
  


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.