001: /*
002: * Copyright (C) The Spice Group. All rights reserved.
003: *
004: * This software is published under the terms of the Spice
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.spice.netserve.connection.handlers;
009:
010: import junit.framework.TestCase;
011: import com.mockobjects.dynamic.Mock;
012: import com.mockobjects.dynamic.C;
013: import org.codehaus.spice.netserve.connection.RequestHandler;
014: import org.codehaus.spice.threadpool.ThreadPool;
015: import org.codehaus.spice.threadpool.ThreadControl;
016: import java.net.Socket;
017:
018: /**
019: *
020: * @author Peter Donald
021: * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
022: */
023: public class ThreadPerRequestHandlerTestCase extends TestCase {
024: public void testNullThreadPoolPassedToCtor() throws Exception {
025: final Mock mockHandler = new Mock(RequestHandler.class);
026: final RequestHandler handler = (RequestHandler) mockHandler
027: .proxy();
028: try {
029: new ThreadPerRequestHandler(handler, null);
030: } catch (final NullPointerException npe) {
031: assertEquals("npe.getMessage()", "threadPool", npe
032: .getMessage());
033: return;
034: }
035: fail("Expected to fail due to null ThreadPool passed into Ctor");
036: }
037:
038: public void testThreadPoolInvoked() throws Exception {
039: final Mock mockControl = new Mock(ThreadControl.class);
040: final ThreadControl control = (ThreadControl) mockControl
041: .proxy();
042:
043: final Mock mockHandler = new Mock(RequestHandler.class);
044: final RequestHandler handler = (RequestHandler) mockHandler
045: .proxy();
046:
047: final Mock mockPool = new Mock(ThreadPool.class);
048: mockPool.expectAndReturn("execute", C.args(C
049: .isA(Runnable.class)), control);
050: final ThreadPool threadPool = (ThreadPool) mockPool.proxy();
051:
052: final ThreadPerRequestHandler requestHandler = new ThreadPerRequestHandler(
053: handler, threadPool);
054: requestHandler.handleConnection(new Socket());
055:
056: mockHandler.verify();
057: mockPool.verify();
058: mockControl.verify();
059: }
060:
061: public void testShutdownWhileThreadStillGoingButInteruptible()
062: throws Exception {
063: final DelayingRequestHandler handler = new DelayingRequestHandler(
064: 2000, true);
065:
066: final ThreadPerRequestHandler requestHandler = new ThreadPerRequestHandler(
067: handler, new MockThreadPool());
068: requestHandler.handleConnection(new Socket());
069: Thread.sleep(50);
070:
071: requestHandler.shutdown(50);
072: assertEquals("isShutdown", true, handler.isShutdown());
073: assertEquals("isExited", true, handler.isExited());
074: assertEquals("isExitDueToInterrupt", true, handler
075: .isExitDueToInterrupt());
076: }
077:
078: public void testShutdownWhileThreadStillGoingAndNotInteruptible()
079: throws Exception {
080: final DelayingRequestHandler handler = new DelayingRequestHandler(
081: 2000, false);
082:
083: final ThreadPerRequestHandler requestHandler = new ThreadPerRequestHandler(
084: handler, new MockThreadPool());
085: requestHandler.handleConnection(new Socket());
086: Thread.sleep(50);
087:
088: requestHandler.shutdown(50);
089: assertEquals("isShutdown", true, handler.isShutdown());
090: assertEquals("isExited", false, handler.isExited());
091: assertEquals("isExitDueToInterrupt", false, handler
092: .isExitDueToInterrupt());
093: }
094:
095: public void testShutdownWhileThreadStillGoingAndWaitIndefinetly()
096: throws Exception {
097: final DelayingRequestHandler handler = new DelayingRequestHandler(
098: 200, false);
099:
100: final ThreadPerRequestHandler requestHandler = new ThreadPerRequestHandler(
101: handler, new MockThreadPool());
102: requestHandler.handleConnection(new Socket());
103: Thread.sleep(50);
104:
105: requestHandler.shutdown(0);
106: assertEquals("isShutdown", true, handler.isShutdown());
107: assertEquals("isExited", true, handler.isExited());
108: assertEquals("isExitDueToInterrupt", false, handler
109: .isExitDueToInterrupt());
110: }
111: }
|