001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.http;
022:
023: import java.io.IOException;
024: import java.net.SocketTimeoutException;
025:
026: import org.junit.Assert;
027: import org.junit.Test;
028:
029: import org.xsocket.Execution;
030: import org.xsocket.ILifeCycle;
031: import org.xsocket.connection.IServer;
032: import org.xsocket.connection.ConnectionUtils;
033: import org.xsocket.connection.http.HttpResponse;
034: import org.xsocket.connection.http.client.GetRequest;
035: import org.xsocket.connection.http.client.HttpClient;
036: import org.xsocket.connection.http.client.HttpClientConnection;
037: import org.xsocket.connection.http.client.IHttpResponseTimeoutHandler;
038: import org.xsocket.connection.http.client.IHttpResponseHandler;
039: import org.xsocket.connection.http.server.HttpServer;
040: import org.xsocket.connection.http.server.HttpServerConnection;
041: import org.xsocket.connection.http.server.IHttpRequestTimeoutHandler;
042: import org.xsocket.connection.http.server.IHttpResponseContext;
043: import org.xsocket.connection.http.server.IHttpRequestHandler;
044:
045: /**
046: *
047: * @author grro@xsocket.org
048: */
049: public final class ServerHandlerTest {
050:
051: @Test
052: public void testLiveCycle() throws Exception {
053:
054: MultithreadedServerHandler hdl = new MultithreadedServerHandler();
055:
056: IServer server = new HttpServer(hdl);
057: ConnectionUtils.start(server);
058:
059: Assert.assertEquals(1, hdl.getCountOnInitCalled());
060: Assert.assertEquals("xServer", hdl.getOnInitThreadname());
061:
062: server.close();
063:
064: QAUtil.sleep(200);
065:
066: Assert.assertEquals(1, hdl.getCountOnDestroyCalled());
067: Assert.assertEquals(Thread.currentThread().getName(), hdl
068: .getOnDestroyThreadname());
069: }
070:
071: @Test
072: public void testConnectionMultithreaded() throws Exception {
073:
074: MultithreadedServerHandler hdl = new MultithreadedServerHandler();
075:
076: IServer server = new HttpServer(hdl);
077: server.setConnectionTimeoutMillis(1 * 1000);
078: ConnectionUtils.start(server);
079:
080: HttpClientConnection con = new HttpClientConnection(
081: "localhost", server.getLocalPort());
082:
083: QAUtil.sleep(500);
084:
085: Assert.assertEquals(1, hdl.getCountOnConnectCalled());
086: Assert.assertFalse(hdl.getOnConnectThreadName().startsWith(
087: "xServer:"));
088:
089: QAUtil.sleep(1000);
090:
091: Assert.assertEquals(1, hdl.getCountOnConnectionTimeoutCalled());
092: Assert.assertFalse(hdl.getOnConnectionTimeoutThreadname()
093: .startsWith("xIoTimer"));
094:
095: Assert.assertEquals(1, hdl.getCountOnDisconnectCalled());
096: Assert.assertFalse(hdl.getOnDisconnectThreadName().startsWith(
097: "xIoTimer"));
098:
099: con.close();
100: server.close();
101: }
102:
103: @Test
104: public void testConnectionNonthreaded() throws Exception {
105:
106: NonThreadedServerHandler hdl = new NonThreadedServerHandler();
107:
108: IServer server = new HttpServer(hdl);
109: server.setConnectionTimeoutMillis(1 * 1000);
110: ConnectionUtils.start(server);
111:
112: HttpClientConnection con = new HttpClientConnection(
113: "localhost", server.getLocalPort());
114:
115: QAUtil.sleep(500);
116:
117: Assert.assertEquals(1, hdl.getCountOnConnectCalled());
118: Assert.assertTrue(hdl.getOnConnectThreadName().startsWith(
119: "xServer:"));
120:
121: QAUtil.sleep(1000);
122:
123: Assert.assertEquals(1, hdl.getCountOnConnectionTimeoutCalled());
124: Assert.assertTrue(hdl.getOnConnectionTimeoutThreadname()
125: .startsWith("xIoTimer"));
126:
127: Assert.assertEquals(1, hdl.getCountOnDisconnectCalled());
128: Assert.assertTrue(hdl.getOnDisconnectThreadName().startsWith(
129: "xIoTimer"));
130:
131: con.close();
132: server.close();
133: }
134:
135: @Test
136: public void testRequestMultithreaded() throws Exception {
137:
138: MultithreadedServerHandler hdl = new MultithreadedServerHandler();
139:
140: HttpServer server = new HttpServer(hdl);
141: server.setRequestTimeoutMillis(500);
142: ConnectionUtils.start(server);
143:
144: HttpClientConnection con = new HttpClientConnection(
145: "localhost", server.getLocalPort());
146: con.call(new GetRequest("/"));
147:
148: QAUtil.sleep(250);
149:
150: Assert.assertEquals(1, hdl.getCountOnRequestCalled());
151: Assert.assertTrue(hdl.getOnRequestThreadname().startsWith(
152: "xServerPool"));
153:
154: QAUtil.sleep(1500);
155:
156: Assert.assertEquals(1, hdl.getCountOnRequestTimeoutCalled());
157: Assert.assertTrue(hdl.getOnRequestTimeoutThreadname()
158: .startsWith("xServerPool"));
159:
160: Assert.assertEquals(1, hdl.getCountOnDisconnectCalled());
161: Assert.assertTrue(hdl.getOnDisconnectThreadName().startsWith(
162: "xServerPool"));
163:
164: con.close();
165: server.close();
166: }
167:
168: @Test
169: public void testRequestNonthreaded() throws Exception {
170:
171: NonThreadedServerHandler hdl = new NonThreadedServerHandler();
172:
173: HttpServer server = new HttpServer(hdl);
174: server.setRequestTimeoutMillis(500);
175: ConnectionUtils.start(server);
176:
177: HttpClientConnection con = new HttpClientConnection(
178: "localhost", server.getLocalPort());
179: con.call(new GetRequest("/"));
180:
181: QAUtil.sleep(250);
182:
183: Assert.assertEquals(1, hdl.getCountOnRequestCalled());
184: Assert.assertTrue(hdl.getOnRequestThreadname().startsWith(
185: "xDispatcher"));
186:
187: QAUtil.sleep(1500);
188:
189: Assert.assertEquals(1, hdl.getCountOnRequestTimeoutCalled());
190: Assert.assertTrue(hdl.getOnRequestTimeoutThreadname().equals(
191: "xHttpTimer"));
192:
193: Assert.assertEquals(1, hdl.getCountOnDisconnectCalled());
194: Assert.assertTrue(hdl.getOnDisconnectThreadName().startsWith(
195: "xHttpTimer"));
196:
197: con.close();
198: server.close();
199: }
200:
201: private static final class MultithreadedServerHandler implements
202: IHttpRequestHandler, IHttpConnectHandler,
203: IHttpDisconnectHandler, IHttpConnectionTimeoutHandler,
204: IHttpRequestTimeoutHandler, ILifeCycle {
205:
206: private int countOnInitCalled = 0;
207: private String onInitThreadname = null;
208: private int countOnDestroyCalled = 0;
209: private String onDestroyThreadname = null;
210: private int countOnConnectCalled = 0;
211: private String onConnectThreadName = null;
212: private int countOnDisconnectCalled = 0;
213: private String onDisconnectThreadName = null;
214: private int countOnRequestCalled = 0;
215: private String onRequestThreadname = null;
216: private int countOnRequestTimeoutCalled = 0;
217: private String onRequestTimeoutThreadname = null;
218: private int countOnConnectionTimeoutCalled = 0;
219: private String onConnectionTimeoutThreadname = null;
220:
221: public void onInit() {
222: countOnInitCalled++;
223: onInitThreadname = Thread.currentThread().getName();
224: }
225:
226: public void onDestroy() throws IOException {
227: countOnDestroyCalled++;
228: onDestroyThreadname = Thread.currentThread().getName();
229: }
230:
231: public boolean onConnect(IHttpConnection httpConnection)
232: throws IOException {
233: countOnConnectCalled++;
234: onConnectThreadName = Thread.currentThread().getName();
235: return true;
236: }
237:
238: public boolean onDisconnect(IHttpConnection httpConnection)
239: throws IOException {
240: countOnDisconnectCalled++;
241: onDisconnectThreadName = Thread.currentThread().getName();
242: return true;
243: }
244:
245: public void onRequest(HttpRequest request,
246: IHttpResponseContext responseContext)
247: throws IOException {
248: countOnRequestCalled++;
249: onRequestThreadname = Thread.currentThread().getName();
250:
251: responseContext.send(new HttpResponse(200));
252: }
253:
254: public boolean onRequestTimeout(IHttpConnection connection)
255: throws IOException {
256: countOnRequestTimeoutCalled++;
257: onRequestTimeoutThreadname = Thread.currentThread()
258: .getName();
259: return false;
260: }
261:
262: public boolean onConnectionTimeout(IHttpConnection connection)
263: throws IOException {
264: countOnConnectionTimeoutCalled++;
265: onConnectionTimeoutThreadname = Thread.currentThread()
266: .getName();
267: return false;
268: }
269:
270: public int getCountOnInitCalled() {
271: return countOnInitCalled;
272: }
273:
274: public String getOnInitThreadname() {
275: return onInitThreadname;
276: }
277:
278: public int getCountOnDestroyCalled() {
279: return countOnDestroyCalled;
280: }
281:
282: public String getOnDestroyThreadname() {
283: return onDestroyThreadname;
284: }
285:
286: public int getCountOnConnectCalled() {
287: return countOnConnectCalled;
288: }
289:
290: public String getOnConnectThreadName() {
291: return onConnectThreadName;
292: }
293:
294: public int getCountOnDisconnectCalled() {
295: return countOnDisconnectCalled;
296: }
297:
298: public String getOnDisconnectThreadName() {
299: return onDisconnectThreadName;
300: }
301:
302: public int getCountOnRequestCalled() {
303: return countOnRequestCalled;
304: }
305:
306: public String getOnRequestThreadname() {
307: return onRequestThreadname;
308: }
309:
310: public int getCountOnRequestTimeoutCalled() {
311: return countOnRequestTimeoutCalled;
312: }
313:
314: public String getOnRequestTimeoutThreadname() {
315: return onRequestTimeoutThreadname;
316: }
317:
318: public int getCountOnConnectionTimeoutCalled() {
319: return countOnConnectionTimeoutCalled;
320: }
321:
322: public String getOnConnectionTimeoutThreadname() {
323: return onConnectionTimeoutThreadname;
324: }
325: }
326:
327: @Execution(Execution.NONTHREADED)
328: private static final class NonThreadedServerHandler implements
329: IHttpRequestHandler, IHttpConnectHandler,
330: IHttpDisconnectHandler, IHttpConnectionTimeoutHandler,
331: IHttpRequestTimeoutHandler, ILifeCycle {
332:
333: private int countOnInitCalled = 0;
334: private String onInitThreadname = null;
335: private int countOnDestroyCalled = 0;
336: private String onDestroyThreadname = null;
337: private int countOnConnectCalled = 0;
338: private String onConnectThreadName = null;
339: private int countOnDisconnectCalled = 0;
340: private String onDisconnectThreadName = null;
341: private int countOnRequestCalled = 0;
342: private String onRequestThreadname = null;
343: private int countOnRequestTimeoutCalled = 0;
344: private String onRequestTimeoutThreadname = null;
345: private int countOnConnectionTimeoutCalled = 0;
346: private String onConnectionTimeoutThreadname = null;
347:
348: public void onInit() {
349: countOnInitCalled++;
350: onInitThreadname = Thread.currentThread().getName();
351: }
352:
353: public void onDestroy() throws IOException {
354: countOnDestroyCalled++;
355: onDestroyThreadname = Thread.currentThread().getName();
356: }
357:
358: public boolean onConnect(IHttpConnection httpConnection)
359: throws IOException {
360: countOnConnectCalled++;
361: onConnectThreadName = Thread.currentThread().getName();
362: return true;
363: }
364:
365: public boolean onDisconnect(IHttpConnection httpConnection)
366: throws IOException {
367: countOnDisconnectCalled++;
368: onDisconnectThreadName = Thread.currentThread().getName();
369: return false;
370: }
371:
372: public void onRequest(HttpRequest request,
373: IHttpResponseContext responseContext)
374: throws IOException {
375: countOnRequestCalled++;
376: onRequestThreadname = Thread.currentThread().getName();
377:
378: responseContext.send(new HttpResponse(200));
379: }
380:
381: public boolean onRequestTimeout(IHttpConnection connection)
382: throws IOException {
383: countOnRequestTimeoutCalled++;
384: onRequestTimeoutThreadname = Thread.currentThread()
385: .getName();
386: return false;
387: }
388:
389: public boolean onConnectionTimeout(IHttpConnection connection)
390: throws IOException {
391: countOnConnectionTimeoutCalled++;
392: onConnectionTimeoutThreadname = Thread.currentThread()
393: .getName();
394: return false;
395: }
396:
397: public int getCountOnInitCalled() {
398: return countOnInitCalled;
399: }
400:
401: public String getOnInitThreadname() {
402: return onInitThreadname;
403: }
404:
405: public int getCountOnDestroyCalled() {
406: return countOnDestroyCalled;
407: }
408:
409: public String getOnDestroyThreadname() {
410: return onDestroyThreadname;
411: }
412:
413: public int getCountOnConnectCalled() {
414: return countOnConnectCalled;
415: }
416:
417: public String getOnConnectThreadName() {
418: return onConnectThreadName;
419: }
420:
421: public int getCountOnDisconnectCalled() {
422: return countOnDisconnectCalled;
423: }
424:
425: public String getOnDisconnectThreadName() {
426: return onDisconnectThreadName;
427: }
428:
429: public int getCountOnRequestCalled() {
430: return countOnRequestCalled;
431: }
432:
433: public String getOnRequestThreadname() {
434: return onRequestThreadname;
435: }
436:
437: public int getCountOnRequestTimeoutCalled() {
438: return countOnRequestTimeoutCalled;
439: }
440:
441: public String getOnRequestTimeoutThreadname() {
442: return onRequestTimeoutThreadname;
443: }
444:
445: public int getCountOnConnectionTimeoutCalled() {
446: return countOnConnectionTimeoutCalled;
447: }
448:
449: public String getOnConnectionTimeoutThreadname() {
450: return onConnectionTimeoutThreadname;
451: }
452: }
453:
454: }
|