001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.net.test;
027:
028: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
029: import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
030: import junit.framework.TestCase;
031: import org.apache.commons.pool.BasePoolableObjectFactory;
032: import org.apache.commons.pool.ObjectPool;
033: import org.apache.commons.pool.impl.SoftReferenceObjectPool;
034: import org.jicarilla.lang.ExceptionListener;
035: import org.jicarilla.lang.NoopExceptionListener;
036: import org.jicarilla.plumbing.Sink;
037: import org.jicarilla.net.Event;
038: import org.jicarilla.net.EventFactory;
039: import org.jicarilla.net.SocketServerConfig;
040: import org.jicarilla.net.SocketServerException;
041: import org.jicarilla.net.SocketServerImpl;
042:
043: import java.net.InetSocketAddress;
044: import java.nio.ByteBuffer;
045: import java.nio.channels.SocketChannel;
046:
047: /**
048: * <a href="http://www.junit.org/">JUnit</a>
049: * {@link TestCase testcase} for
050: * SocketServerImpl.
051: *
052: * @todo a lot more tests....
053: *
054: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
055: * @version $Id: SocketServerImplTestCase.java,v 1.7 2004/03/23 13:37:50 lsimons Exp $
056: */
057: public class SocketServerImplTestCase extends TestCase {
058: public final static int port = 8088;
059: public final static int backlog = 50;
060: public final static int threads = 50;
061: protected SocketServerConfig conf;
062: protected ExceptionListener exceptionListener;
063: protected ObjectPool eventPool;
064: protected ObjectPool channelPool;
065: protected Sink errorHandler;
066: protected PooledExecutor threadPool;
067:
068: public void setUp() throws Exception {
069: conf = new SocketServerConfig("localhost", port, backlog,
070: threads);
071: exceptionListener = new NoopExceptionListener();
072: eventPool = new SoftReferenceObjectPool(new EventFactory());
073: channelPool = new SoftReferenceObjectPool(
074: new BasePoolableObjectFactory() {
075: public Object makeObject() {
076: return new LinkedQueue();
077: }
078: });
079: errorHandler = new Sink() {
080: public void put(final Object o) {
081: final Event e = (Event) o;
082: e.getChannel();
083: }
084:
085: public boolean offer(final Object o, final long l) {
086: return true;
087: }
088: };
089: threadPool = new PooledExecutor();
090: }
091:
092: public void tearDown() throws Exception {
093: }
094:
095: private final static int lifecycleSleepTime = 200;
096:
097: public void testLifecycle() throws Throwable {
098: final SocketServerImpl server = new SocketServerImpl(conf,
099: exceptionListener, eventPool, channelPool,
100: errorHandler, threadPool);
101:
102: server.initialize();
103: Thread.sleep(lifecycleSleepTime);
104: server.dispose();
105: }
106:
107: /*protected class ErrorSink implements Sink
108: {
109: public SocketChannel s;
110:
111: public void put( Object o ) throws InterruptedException
112: {
113: Event e = (Event)o;
114: s = e.getChannel();
115: }
116:
117: public boolean offer( Object o, long l ) throws InterruptedException
118: {
119: Event e = (Event)o;
120: s = e.getChannel();
121: return true;
122: }
123: }*/
124: protected static class SavingQueue extends LinkedQueue {
125: public static SocketChannel s;
126: public static int received = 0;
127:
128: public void put(final Object o) throws InterruptedException {
129: received++;
130: final Event e = (Event) o;
131: s = e.getChannel();
132: }
133:
134: public boolean offer(final Object o, final long l)
135: throws InterruptedException {
136: received++;
137: final Event e = (Event) o;
138: s = e.getChannel();
139: return true;
140: }
141: }
142:
143: protected class SavingSocketServerImpl extends SocketServerImpl {
144: protected SavingSocketServerImpl(final SocketServerConfig conf,
145: final ExceptionListener exceptionListener,
146: final ObjectPool eventPool,
147: final ObjectPool channelPool, final Sink errorHandler,
148: final PooledExecutor threadPool) throws Exception {
149: super (conf, exceptionListener, eventPool, channelPool,
150: errorHandler, threadPool);
151: }
152:
153: public SocketChannel s;
154:
155: public void handleRequest(final SocketChannel c)
156: throws SocketServerException, InterruptedException {
157: s = c;
158: super .handleRequest(c);
159: }
160: }
161:
162: public final static int sleepTime = 500;
163:
164: public void testStageRecievesCorrectChannel() throws Throwable {
165: //ErrorSink errorHander = new ErrorSink();
166: channelPool = new SoftReferenceObjectPool(
167: new BasePoolableObjectFactory() {
168: public Object makeObject() {
169: return new SavingQueue();
170: }
171: });
172: final SavingSocketServerImpl server = new SavingSocketServerImpl(
173: conf, exceptionListener, eventPool, channelPool,
174: errorHandler, threadPool);
175:
176: server.initialize();
177:
178: final SocketChannel sc = SocketChannel.open();
179: sc.socket().connect(new InetSocketAddress("localhost", port));
180: final char[] str = "Hi!".toCharArray();
181: final ByteBuffer msg = ByteBuffer.allocate(str.length);
182: for (int i = 0; i < str.length; i++)
183: msg.put((byte) str[i]);
184: msg.rewind();
185: sc.write(msg);
186:
187: Thread.sleep(sleepTime);
188:
189: server.handleRequest(sc);
190:
191: Thread.sleep(sleepTime);
192:
193: assertSame(sc, SavingQueue.s);
194: assertEquals(2, SavingQueue.received);
195:
196: SavingQueue.s = null;
197: SavingQueue.received = 0;
198:
199: server.dispose();
200: }
201: }
|