server-based multithreaded test of TSCCM
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@539880 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7f4b4d2caa
commit
d54f742c36
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
* ====================================================================
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.impl.conn;
|
||||
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.HttpProcessor;
|
||||
import org.apache.http.protocol.HttpRequestExecutor;
|
||||
import org.apache.http.protocol.HttpExecutionContext;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import org.apache.http.conn.HttpRoute;
|
||||
import org.apache.http.conn.ManagedClientConnection;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
|
||||
|
||||
/**
|
||||
* Executes a request from a new thread.
|
||||
*
|
||||
* @author Michael Becke
|
||||
* @author Roland Weber (rolandw at apache.org)
|
||||
*/
|
||||
public class ExecReqThread extends GetConnThread {
|
||||
|
||||
protected RequestSpec request_spec;
|
||||
protected HttpResponse response;
|
||||
protected byte[] response_data;
|
||||
|
||||
|
||||
/**
|
||||
* Executes a request.
|
||||
* This involves the following steps:
|
||||
* <ol>
|
||||
* <li>obtain a connection (see base class)</li>
|
||||
* <li>open the connection</li>
|
||||
* <li>prepare context and request</li>
|
||||
* <li>execute request to obtain the response</li>
|
||||
* <li>consume the response entity (if there is one)</li>
|
||||
* <li>release the connection</li>
|
||||
* </ol>
|
||||
*/
|
||||
public ExecReqThread(ClientConnectionManager mgr,
|
||||
HttpRoute route, long timeout,
|
||||
RequestSpec reqspec) {
|
||||
super(mgr, route, timeout);
|
||||
|
||||
request_spec = reqspec;
|
||||
}
|
||||
|
||||
|
||||
public HttpResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public byte[] getResponseData() {
|
||||
return response_data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method is invoked when the thread is started.
|
||||
* It invokes the base class implementation.
|
||||
*/
|
||||
public void run() {
|
||||
super.run(); // obtain connection
|
||||
if (connection == null)
|
||||
return; // problem obtaining connection
|
||||
|
||||
try {
|
||||
request_spec.context.setAttribute
|
||||
(HttpExecutionContext.HTTP_CONNECTION, connection);
|
||||
|
||||
doOpenConnection();
|
||||
|
||||
HttpRequest request = (HttpRequest) request_spec.context.
|
||||
getAttribute(HttpExecutionContext.HTTP_REQUEST);
|
||||
request_spec.executor.preProcess
|
||||
(request, request_spec.processor, request_spec.context);
|
||||
|
||||
response = request_spec.executor.execute
|
||||
(request, connection, request_spec.context);
|
||||
|
||||
request_spec.executor.postProcess
|
||||
(response, request_spec.processor, request_spec.context);
|
||||
|
||||
doConsumeResponse();
|
||||
|
||||
} catch (Throwable dart) {
|
||||
dart.printStackTrace(System.out);
|
||||
if (exception != null)
|
||||
exception = dart;
|
||||
|
||||
} finally {
|
||||
conn_manager.releaseConnection(connection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Opens the connection after it has been obtained.
|
||||
*/
|
||||
protected void doOpenConnection() throws Exception {
|
||||
connection.open
|
||||
(conn_route, request_spec.context, request_spec.params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the response entity, if there is one.
|
||||
*/
|
||||
protected void doConsumeResponse() throws Exception {
|
||||
if (response.getEntity() != null)
|
||||
response_data = EntityUtils.toByteArray(response.getEntity());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper class collecting request data.
|
||||
* The request and target are expected in the context.
|
||||
*/
|
||||
public static class RequestSpec {
|
||||
public HttpRequestExecutor executor;
|
||||
public HttpProcessor processor;
|
||||
public HttpContext context;
|
||||
public HttpParams params;
|
||||
}
|
||||
|
||||
}
|
|
@ -61,6 +61,20 @@ import org.apache.http.util.EntityUtils;
|
|||
*/
|
||||
public class TestTSCCMWithServer extends ServerTestBase {
|
||||
|
||||
// Server-based tests not ported from 3.x TestHttpConnectionManager
|
||||
//
|
||||
// testWriteRequestReleaseConnection
|
||||
// This tests auto-release in case of an I/O error while writing.
|
||||
// It's a test of the execution framework, not of the manager.
|
||||
// testConnectMethodFailureRelease
|
||||
// This tests method.fakeResponse() and auto-release. It's a
|
||||
// test of a 3.x specific hack and the execution framework.
|
||||
// testResponseAutoRelease
|
||||
// Auto-release is not part of the connection manager anymore.
|
||||
// testMaxConnectionsPerServer
|
||||
// Connection limits are already tested without a server.
|
||||
|
||||
|
||||
public TestTSCCMWithServer(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
@ -96,6 +110,70 @@ public class TestTSCCMWithServer extends ServerTestBase {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tests executing several requests in parallel.
|
||||
*/
|
||||
public void testParallelRequests() throws Exception {
|
||||
// 3.x: TestHttpConnectionManager.testGetFromMultipleThreads
|
||||
|
||||
final int COUNT = 8; // adjust to execute more requests
|
||||
|
||||
HttpParams mgrpar = defaultParams.copy();
|
||||
HttpConnectionManagerParams.setMaxTotalConnections
|
||||
(mgrpar, COUNT/2);
|
||||
HttpConnectionManagerParams.setDefaultMaxConnectionsPerHost
|
||||
(mgrpar, COUNT/2);
|
||||
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
|
||||
|
||||
final HttpHost target = getServerHttp();
|
||||
final HttpRoute route = new HttpRoute(target, null, false);
|
||||
final int rsplen = 8;
|
||||
final String uri = "/random/" + rsplen;
|
||||
|
||||
ExecReqThread[] threads = new ExecReqThread [COUNT];
|
||||
for (int i=0; i<COUNT; i++) {
|
||||
|
||||
HttpRequest request = new BasicHttpRequest
|
||||
("GET", uri, HttpVersion.HTTP_1_1);
|
||||
|
||||
ExecReqThread.RequestSpec ertrs = new ExecReqThread.RequestSpec();
|
||||
ertrs.executor = httpExecutor;
|
||||
ertrs.processor = httpProcessor;
|
||||
ertrs.context = new HttpExecutionContext(null);
|
||||
ertrs.params = defaultParams;
|
||||
|
||||
ertrs.context.setAttribute
|
||||
(HttpExecutionContext.HTTP_TARGET_HOST, target);
|
||||
ertrs.context.setAttribute
|
||||
(HttpExecutionContext.HTTP_REQUEST, request);
|
||||
|
||||
threads[i] = new ExecReqThread(mgr, route, 5000L, ertrs);
|
||||
}
|
||||
|
||||
for (int i=0; i<threads.length; i++) {
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
for (int i=0; i<threads.length; i++) {
|
||||
threads[i].join(10000);
|
||||
assertNull("exception in thread " + i,
|
||||
threads[i].getException());
|
||||
assertNotNull("no response in thread " + i,
|
||||
threads[i].getResponse());
|
||||
assertEquals("wrong status code in thread " + i, 200,
|
||||
threads[i].getResponse()
|
||||
.getStatusLine().getStatusCode());
|
||||
assertNotNull("no response data in thread " + i,
|
||||
threads[i].getResponseData());
|
||||
assertEquals("wrong length of data in thread" + i, rsplen,
|
||||
threads[i].getResponseData().length);
|
||||
}
|
||||
|
||||
mgr.shutdown();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests releasing and re-using a connection after a response is read.
|
||||
*/
|
||||
|
@ -295,26 +373,4 @@ public class TestTSCCMWithServer extends ServerTestBase {
|
|||
}
|
||||
|
||||
|
||||
// List of server-based tests in 3.x TestHttpConnectionManager
|
||||
//
|
||||
// + testReleaseConnection
|
||||
// + testDroppedThread
|
||||
// + testReclaimUnusedConnection
|
||||
// testGetFromMultipleThreads, depends on extra threads
|
||||
|
||||
|
||||
// Server-based tests not ported from 3.x TestHttpConnectionManager
|
||||
//
|
||||
// testWriteRequestReleaseConnection
|
||||
// This tests auto-release in case of an I/O error while writing.
|
||||
// It's a test of the execution framework, not of the manager.
|
||||
// testConnectMethodFailureRelease
|
||||
// This tests method.fakeResponse() and auto-release. It's a
|
||||
// test of a 3.x specific hack and the execution framework.
|
||||
// testResponseAutoRelease
|
||||
// Auto-release is not part of the connection manager.
|
||||
// testMaxConnectionsPerServer
|
||||
// Connection limits are already tested without a server.
|
||||
|
||||
|
||||
} // class TestTSCCMWithServer
|
||||
|
|
Loading…
Reference in New Issue