cvs commit: spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers DelayingRequestHandler.java RequestHandlerTestCase.java
Peter Donald <[email protected]>
| Newsgroups | gmane.comp.java.spice.cvs |
|---|---|
| Message-ID | <[email protected]> |
donaldp 03/10/28 19:26:56
Modified: components/netserve/src/test/org/jcomponent/netserve/connection/handlers
RequestHandlerTestCase.java
Added: components/netserve/src/test/org/jcomponent/netserve/connection/handlers
DelayingRequestHandler.java
Log:
Make sure threads are shutdown properly
Revision Changes Path
1.2 +69 -1 spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/RequestHandlerTestCase.java
Index: RequestHandlerTestCase.java
===================================================================
RCS file: /cvsroot/spice/spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/RequestHandlerTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequestHandlerTestCase.java 29 Oct 2003 03:00:14 -0000 1.1
+++ RequestHandlerTestCase.java 29 Oct 2003 03:26:56 -0000 1.2
@@ -13,7 +13,7 @@
/**
*
* @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:26:56 $
*/
public class RequestHandlerTestCase
extends TestCase
@@ -59,5 +59,73 @@
assertEquals( "getErrorClosingConnectionThrowable",
ExceptingRequestHandler.EXCEPTION,
handler.getErrorHandlingConnectionThrowable() );
+ }
+
+ public void testShutdownWhileThreadStillGoingButInteruptible()
+ throws Exception
+ {
+ final DelayingRequestHandler handler = new DelayingRequestHandler( 2000, true );
+ final Socket socket = new Socket();
+ final Runnable runnable = new Runnable()
+ {
+ public void run()
+ {
+ handler.handleConnection( socket );
+ }
+ };
+ final Thread thread = new Thread( runnable );
+ thread.start();
+ Thread.sleep( 50 );
+
+ handler.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 Socket socket = new Socket();
+ final Runnable runnable = new Runnable()
+ {
+ public void run()
+ {
+ handler.handleConnection( socket );
+ }
+ };
+ final Thread thread = new Thread( runnable );
+ thread.start();
+ Thread.sleep( 50 );
+
+ handler.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 Socket socket = new Socket();
+ final Runnable runnable = new Runnable()
+ {
+ public void run()
+ {
+ handler.handleConnection( socket );
+ }
+ };
+ final Thread thread = new Thread( runnable );
+ thread.start();
+ Thread.sleep( 50 );
+
+ handler.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/DelayingRequestHandler.java
Index: DelayingRequestHandler.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 java.net.Socket;
/**
*
* @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2003/10/29 03:26:56 $
*/
class DelayingRequestHandler
extends AbstractRequestHandler
{
private final long m_delay;
private final boolean m_wakeupOnInterrupt;
private boolean m_exited;
private boolean m_exitDueToInterrupt;
DelayingRequestHandler( final long delay,
final boolean wakeupOnInterrupt )
{
m_delay = delay;
m_wakeupOnInterrupt = wakeupOnInterrupt;
}
protected void doPerformRequest( Socket socket )
throws Exception
{
final int code = System.identityHashCode( this );
final String prefix = "Handler(" + code + ") ";
System.out.println( prefix + "Started" );
final long then = System.currentTimeMillis() + m_delay;
while( System.currentTimeMillis() < then )
{
final long rest = then - System.currentTimeMillis();
System.out.println( prefix + "Sleeping for " + rest );
try
{
Thread.sleep( rest );
}
catch( InterruptedException e )
{
System.out.println( prefix + "Woken up" );
if( m_wakeupOnInterrupt )
{
m_exitDueToInterrupt = true;
break;
}
}
}
m_exited = true;
System.out.println( prefix + "Returning" );
}
boolean isExitDueToInterrupt()
{
return m_exitDueToInterrupt;
}
boolean isExited()
{
return m_exited;
}
}
-------------------------------------------------------
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/