/*
* Copyright (c) xlightweb.org, 2006 - 2009. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
import org.xlightweb.QAUtil;
import org.xlightweb.client.HttpClient;
/**
*
* @author grro@xlightweb.org
*/
public final class HttpClientRetryTest {
public static void main(String[] args) throws Exception {
Logger LOG = Logger.getLogger(HttpClientRetryTest.class.getName());
for (int i = 0; i < 10000; i++) {
LOG.info("loop " + i);
new HttpClientRetryTest().testPutRetry2();
}
}
@Test
public void testSimpleGetRetryBulk() throws Exception {
for (int i = 0; i < 100; i++) {
testSimpleGetRetry();
}
}
@Test
public void testSimpleGetRetry() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
String header = connection.readStringByDelimiter("\r\n\r\n");
counter++;
System.out.println(counter + " call");
if (counter < 3) {
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
IHttpResponse response = httpClient.call(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
@Test
public void testSimpleGetRetry2() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
String header = connection.readStringByDelimiter("\r\n");
counter++;
if (counter < 3) {
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
IHttpResponse response = httpClient.call(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
@Test
public void testSimpleGetRetry3() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
counter++;
System.out.println(counter + " call");
if (counter < 3) {
connection.close();
} else {
String header = connection.readStringByDelimiter("\r\n\r\n");
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
IHttpResponse response = httpClient.call(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
@Test
public void testSimpleGetRetry4() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
connection.readStringByDelimiter("\r\n\r\n");
counter++;
if (counter < 3) {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n");
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
ResponseHandler respHdl = new ResponseHandler();
httpClient.send(request, respHdl);
while (respHdl.getResponse() == null) {
QAUtil.sleep(100);
}
IHttpResponse response = respHdl.getResponse();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
@InvokeOn(InvokeOn.MESSAGE_RECEIVED)
private static final class ResponseHandler implements IHttpResponseHandler {
private final AtomicReference<IHttpResponse> responseRef = new AtomicReference<IHttpResponse>();
public void onResponse(IHttpResponse response) throws IOException {
responseRef.set(response);
}
public void onException(IOException ioe) throws IOException {
}
public IHttpResponse getResponse() {
return responseRef.get();
}
}
@Test
public void testSimpleGetMaxRetryExceeded() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
String header = connection.readStringByDelimiter("\r\n\r\n");
counter++;
if (counter < 8) {
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
httpClient.setMaxRetries(6);
GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
try {
IHttpResponse response = httpClient.call(request);
Assert.fail("IOException expected");
} catch (IOException expected) { }
httpClient.close();
server.close();
}
@Test
public void testNoRetry() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
String header = connection.readStringByDelimiter("\r\n\r\n");
counter++;
if (counter < 2) {
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
httpClient.setMaxRetries(0);
GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
try {
IHttpResponse response = httpClient.call(request);
Assert.fail("IOException expected");
} catch (IOException expected) { }
httpClient.close();
server.close();
}
@Test
public void testSimpleDeleteRetry() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
String header = connection.readStringByDelimiter("\r\n\r\n");
counter++;
System.out.println(counter + " call");
if (counter < 3) {
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
DeleteRequest request = new DeleteRequest("http://localhost:" + server.getLocalPort() + "/test");
IHttpResponse response = httpClient.call(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
@Test
public void testPutRetry() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
connection.readStringByDelimiter("\r\n\r\n");
counter++;
if (counter < 3) {
connection.close();
} else {
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: 5\r\n" +
"\r\n" +
"12345");
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
PutRequest request = new PutRequest("http://localhost:" + server.getLocalPort() + "/test", "text/plain", "12345");
IHttpResponse response = httpClient.call(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
@Test
public void testPutRetry2() throws Exception {
IDataHandler dh = new IDataHandler() {
private int counter = 0;
private int state = 0;
private int size = 0;
public boolean onData(INonBlockingConnection connection) throws IOException {
switch (state) {
case 0:
String header = connection.readStringByDelimiter("\r\n\r\n");
counter++;
if (counter < 3) {
connection.close();
break;
} else {
size = QAUtil.readContentLength(header);
state = 1;
}
default:
String body = connection.readStringByLength(size);
connection.write("HTTP/1.1 200 OK\r\n" +
"Server: me\r\n" +
"Content-length: " + size + "\r\n" +
"\r\n" +
body);
break;
}
return true;
}
};
IServer server = new Server(dh);
server.start();
HttpClient httpClient = new HttpClient();
PutRequest request = new PutRequest("http://localhost:" + server.getLocalPort() + "/test", "text/plain", "12345");
IHttpResponse response = httpClient.call(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("12345", response.getBlockingBody().readString());
httpClient.close();
server.close();
}
}