1
2
3
4
5
6
7
8 package org.codehaus.spice.netserve.connection.handlers;
9
10 import junit.framework.TestCase;
11 import com.mockobjects.dynamic.Mock;
12 import com.mockobjects.dynamic.C;
13 import org.codehaus.spice.netserve.connection.RequestHandler;
14 import org.codehaus.spice.threadpool.ThreadPool;
15 import org.codehaus.spice.threadpool.ThreadControl;
16 import java.net.Socket;
17
18 /***
19 *
20 * @author Peter Donald
21 * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
22 */
23 public class ThreadPerRequestHandlerTestCase
24 extends TestCase
25 {
26 public void testNullThreadPoolPassedToCtor()
27 throws Exception
28 {
29 final Mock mockHandler = new Mock( RequestHandler.class );
30 final RequestHandler handler = (RequestHandler)mockHandler.proxy();
31 try
32 {
33 new ThreadPerRequestHandler( handler, null );
34 }
35 catch( final NullPointerException npe )
36 {
37 assertEquals( "npe.getMessage()", "threadPool", npe.getMessage() );
38 return;
39 }
40 fail( "Expected to fail due to null ThreadPool passed into Ctor" );
41 }
42
43 public void testThreadPoolInvoked()
44 throws Exception
45 {
46 final Mock mockControl = new Mock( ThreadControl.class );
47 final ThreadControl control = (ThreadControl)mockControl.proxy();
48
49 final Mock mockHandler = new Mock( RequestHandler.class );
50 final RequestHandler handler = (RequestHandler)mockHandler.proxy();
51
52 final Mock mockPool = new Mock( ThreadPool.class );
53 mockPool.expectAndReturn( "execute", C.args( C.isA( Runnable.class ) ), control );
54 final ThreadPool threadPool = (ThreadPool)mockPool.proxy();
55
56 final ThreadPerRequestHandler requestHandler =
57 new ThreadPerRequestHandler( handler, threadPool );
58 requestHandler.handleConnection( new Socket() );
59
60 mockHandler.verify();
61 mockPool.verify();
62 mockControl.verify();
63 }
64
65 public void testShutdownWhileThreadStillGoingButInteruptible()
66 throws Exception
67 {
68 final DelayingRequestHandler handler =
69 new DelayingRequestHandler( 2000, true );
70
71 final ThreadPerRequestHandler requestHandler =
72 new ThreadPerRequestHandler( handler, new MockThreadPool() );
73 requestHandler.handleConnection( new Socket() );
74 Thread.sleep( 50 );
75
76 requestHandler.shutdown( 50 );
77 assertEquals( "isShutdown", true, handler.isShutdown() );
78 assertEquals( "isExited", true, handler.isExited() );
79 assertEquals( "isExitDueToInterrupt", true, handler.isExitDueToInterrupt() );
80 }
81
82 public void testShutdownWhileThreadStillGoingAndNotInteruptible()
83 throws Exception
84 {
85 final DelayingRequestHandler handler =
86 new DelayingRequestHandler( 2000, false );
87
88 final ThreadPerRequestHandler requestHandler =
89 new ThreadPerRequestHandler( handler, new MockThreadPool() );
90 requestHandler.handleConnection( new Socket() );
91 Thread.sleep( 50 );
92
93 requestHandler.shutdown( 50 );
94 assertEquals( "isShutdown", true, handler.isShutdown() );
95 assertEquals( "isExited", false, handler.isExited() );
96 assertEquals( "isExitDueToInterrupt", false, handler.isExitDueToInterrupt() );
97 }
98
99 public void testShutdownWhileThreadStillGoingAndWaitIndefinetly()
100 throws Exception
101 {
102 final DelayingRequestHandler handler =
103 new DelayingRequestHandler( 200, false );
104
105 final ThreadPerRequestHandler requestHandler =
106 new ThreadPerRequestHandler( handler, new MockThreadPool() );
107 requestHandler.handleConnection( new Socket() );
108 Thread.sleep( 50 );
109
110 requestHandler.shutdown( 0 );
111 assertEquals( "isShutdown", true, handler.isShutdown() );
112 assertEquals( "isExited", true, handler.isExited() );
113 assertEquals( "isExitDueToInterrupt", false, handler.isExitDueToInterrupt() );
114 }
115 }