hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r535592 - in /jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http: impl/conn/TestTSCCMWithServer.java localserver/ServerTestBase.java
Date Sun, 06 May 2007 10:15:43 GMT
Author: rolandw
Date: Sun May  6 03:15:39 2007
New Revision: 535592

URL: http://svn.apache.org/viewvc?view=rev&rev=535592
Log:
first server-based test case for TSCCM

Modified:
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/conn/TestTSCCMWithServer.java
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/localserver/ServerTestBase.java

Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/conn/TestTSCCMWithServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/conn/TestTSCCMWithServer.java?view=diff&rev=535592&r1=535591&r2=535592
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/conn/TestTSCCMWithServer.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/conn/TestTSCCMWithServer.java
Sun May  6 03:15:39 2007
@@ -42,6 +42,7 @@
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.SchemeRegistry;
 import org.apache.http.conn.params.HttpConnectionManagerParams;
+import org.apache.http.conn.ConnectionPoolTimeoutException;
 import org.apache.http.localserver.ServerTestBase;
 import org.apache.http.message.BasicHttpRequest;
 import org.apache.http.params.BasicHttpParams;
@@ -110,19 +111,18 @@
     /**
      * Tests releasing and re-using a connection after a response is read.
      */
-    // public void testReleaseConnection() throws Exception {
-    public void testSkeleton() throws Exception {
-        //@@@ this testcase is not yet complete
+    public void testReleaseConnection() throws Exception {
+        //public void testSkeleton() throws Exception {
 
         HttpParams mgrpar = createManagerParams();
-        HttpConnectionManagerParams.setDefaultMaxConnectionsPerHost(mgrpar, 1);
-        HttpConnectionManagerParams.setMaxTotalConnections(mgrpar, 3);
+        HttpConnectionManagerParams.setMaxTotalConnections(mgrpar, 1);
 
         ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
 
-        HttpHost target = getServerHttp();
+        final HttpHost target = getServerHttp();
         final HttpRoute route = new HttpRoute(target, null, false);
-        final String    uri   = "/random/8"; // read 8 bytes
+        final int      rsplen = 8;
+        final String      uri = "/random/" + rsplen;
 
         HttpRequest request =
             new BasicHttpRequest("GET", uri, HttpVersion.HTTP_1_1);
@@ -130,13 +130,14 @@
         ManagedClientConnection conn = mgr.getConnection(route);
         conn.open(route, httpContext, defaultParams);
 
+        // a new context is created for each testcase, no need to reset
         httpContext.setAttribute(
-                HttpExecutionContext.HTTP_CONNECTION, conn);
+            HttpExecutionContext.HTTP_CONNECTION, conn);
         httpContext.setAttribute(
-                HttpExecutionContext.HTTP_TARGET_HOST, target);
+            HttpExecutionContext.HTTP_TARGET_HOST, target);
         httpContext.setAttribute(
-                HttpExecutionContext.HTTP_REQUEST, request);
-        
+            HttpExecutionContext.HTTP_REQUEST, request);
+
         httpExecutor.preProcess
             (request, httpProcessor, httpContext);
         HttpResponse response =
@@ -144,18 +145,65 @@
         httpExecutor.postProcess
             (response, httpProcessor, httpContext);
 
-        assertEquals("wrong status in response",
+        assertEquals("wrong status in first response",
                      HttpStatus.SC_OK,
                      response.getStatusLine().getStatusCode());
         byte[] data = EntityUtils.toByteArray(response.getEntity());
+        assertEquals("wrong length of first response entity",
+                     rsplen, data.length);
+        // ignore data, but it must be read
+
+        // check that there is no auto-release by default
+        try {
+            // this should fail quickly, connection has not been released
+            mgr.getConnection(route, 10L);
+            fail("ConnectionPoolTimeoutException should have been thrown");
+        } catch (ConnectionPoolTimeoutException e) {
+            // expected
+        }
+
+        // release connection without marking for re-use
+        // expect the next connection obtained to be closed
+        mgr.releaseConnection(conn);
+        conn = mgr.getConnection(route);
+        assertFalse("connection should have been closed", conn.isOpen());
+
+        // repeat the communication, no need to prepare the request again
+        conn.open(route, httpContext, defaultParams);
+        httpContext.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+        response = httpExecutor.execute(request, conn, httpContext);
+        httpExecutor.postProcess(response, httpProcessor, httpContext);
+
+        assertEquals("wrong status in second response",
+                     HttpStatus.SC_OK,
+                     response.getStatusLine().getStatusCode());
+        data = EntityUtils.toByteArray(response.getEntity());
+        assertEquals("wrong length of second response entity",
+                     rsplen, data.length);
         // ignore data, but it must be read
 
+        // release connection after marking it for re-use
+        // expect the next connection obtained to be open
+        conn.markReusable();
         mgr.releaseConnection(conn);
+        conn = mgr.getConnection(route);
+        assertTrue("connection should have been open", conn.isOpen());
+
+        // repeat the communication, no need to prepare the request again
+        httpContext.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+        response = httpExecutor.execute(request, conn, httpContext);
+        httpExecutor.postProcess(response, httpProcessor, httpContext);
+
+        assertEquals("wrong status in third response",
+                     HttpStatus.SC_OK,
+                     response.getStatusLine().getStatusCode());
+        data = EntityUtils.toByteArray(response.getEntity());
+        assertEquals("wrong length of third response entity",
+                     rsplen, data.length);
+        // ignore data, but it must be read
 
-        //@@@ to be checked:
-        // - connection is NOT re-used if not marked before release
-        // - connection is re-used if marked before release
-        // - re-using the connections works
+        mgr.releaseConnection(conn);
+        mgr.shutdown();
     }
 
 
@@ -166,7 +214,7 @@
     // testConnectMethodFailureRelease
     // testDroppedThread
     // testWriteRequestReleaseConnection, depends on execution framework
-    // testReleaseConnection, depends on execution framework
+    // + testReleaseConnection, depends on execution framework
     // testResponseAutoRelease
     // testMaxConnectionsPerServer - what's the server used/needed for?
     // testReclaimUnusedConnection, depends on execution framework

Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/localserver/ServerTestBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/localserver/ServerTestBase.java?view=diff&rev=535592&r1=535591&r2=535592
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/localserver/ServerTestBase.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/localserver/ServerTestBase.java
Sun May  6 03:15:39 2007
@@ -98,15 +98,19 @@
      * This method will re-use the helper objects from a previous run
      * if they are still available. For example, the local test server
      * will be re-started rather than re-created.
-     * Tests that modify the helper objects should afterwards
-     * set the respective attributes to <code>null</code> to force
-     * re-creation for subsequent tests. Of course that shouldn't
-     * be done to the test server, or only after shutting that down.
+     * {@link #httpContext httpContext} will always be re-created.
+     * Tests that modify the other helper objects should afterwards
+     * set the respective attributes to <code>null</code> in a
+     * <code>finally{}</code> block to force re-creation for
+     * subsequent tests.
+     * Of course that shouldn't be done with the test server,
+     * or only after shutting that down.
      *
      * @throws Exception        in case of a problem
      */
     protected void setUp() throws Exception {
 
+        boolean newparams = false;
         if (defaultParams == null) {
             defaultParams = new BasicHttpParams(null);
             HttpProtocolParams.setVersion
@@ -117,6 +121,7 @@
                 (defaultParams, "Jakarta-HttpComponents-Test/1.1");
             HttpProtocolParams.setUseExpectContinue
                 (defaultParams, false);
+            newparams = true;
         }
 
         if (supportedSchemes == null) {
@@ -131,11 +136,10 @@
             httpProcessor.addInterceptor(new RequestConnControl()); // optional
         }
 
-        if (httpContext == null) {
-            httpContext = new HttpExecutionContext(null);
-        }
+        // the context is created each time, it may get modified by test cases
+        httpContext = new HttpExecutionContext(null);
 
-        if (httpExecutor == null) {
+        if ((httpExecutor == null) || newparams) {
             httpExecutor = new HttpRequestExecutor(defaultParams);
         }
 



Mime
View raw message