You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by jo...@apache.org on 2011/07/27 15:54:52 UTC

svn commit: r1151465 - /httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java

Author: jonm
Date: Wed Jul 27 13:54:49 2011
New Revision: 1151465

URL: http://svn.apache.org/viewvc?rev=1151465&view=rev
Log:
HTTPCLIENT-1108: much better unit test coverage of ConnPoolByRoute

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java?rev=1151465&r1=1151464&r2=1151465&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java Wed Jul 27 13:54:49 2011
@@ -29,6 +29,7 @@ package org.apache.http.impl.conn.tsccm;
 
 import static org.junit.Assert.*;
 
+import java.io.IOException;
 import java.util.Random;
 import java.util.concurrent.TimeUnit;
 
@@ -53,11 +54,11 @@ public class TestConnPoolByRoute extends
 
     private ConnPoolByRoute impl;
     private HttpRoute route = new HttpRoute(new HttpHost("localhost"));
+    private HttpRoute route2 = new HttpRoute(new HttpHost("localhost:8080"));
     
-    @Mock
-    private OperatedClientConnection mockConnection;
-    @Mock
-    private ClientConnectionOperator mockOperator;
+    @Mock private OperatedClientConnection mockConnection;
+    @Mock private OperatedClientConnection mockConnection2;
+    @Mock private ClientConnectionOperator mockOperator;
 
     @Before
     @Override
@@ -69,13 +70,12 @@ public class TestConnPoolByRoute extends
     }
     
     private void useMockOperator() {
+        reset(mockOperator);
         impl = new ConnPoolByRoute(
-                mockOperator,
-                new ConnPerRouteBean(), 1, -1, TimeUnit.MILLISECONDS);
+                mockOperator, new ConnPerRouteBean(), 1, -1, TimeUnit.MILLISECONDS);
         when(mockOperator.createConnection()).thenReturn(mockConnection);
     }
-    
-    
+
     @Test
     public void testStatelessConnections() throws Exception {
         final HttpHost target = getServerHttp();
@@ -213,11 +213,25 @@ public class TestConnPoolByRoute extends
     }
     
     @Test
+    public void poolHasOneConnectionAfterRequestingOne() throws Exception {
+        useMockOperator();
+        impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        assertEquals(1, impl.getConnectionsInPool());
+    }
+    
+    @Test
     public void emptyPoolHasNoRouteSpecificConnections() {
         assertEquals(0, impl.getConnectionsInPool(route));
     }
     
     @Test
+    public void routeSpecificPoolHasOneConnectionAfterRequestingOne() throws Exception {
+        useMockOperator();
+        impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        assertEquals(1, impl.getConnectionsInPool(route));
+    }
+    
+    @Test
     public void abortingPoolEntryRequestEarlyDoesNotCreateConnection() {
         PoolEntryRequest req = impl.requestPoolEntry(route, new Object());
         req.abortRequest();
@@ -328,5 +342,114 @@ public class TestConnPoolByRoute extends
         impl.freeEntry(entry, false, 0, TimeUnit.MILLISECONDS);
         verify(mockConnection, atLeastOnce()).close();        
     }
+    
+    @Test
+    public void handlesExceptionsWhenClosingConnections() throws Exception {
+        useMockOperator();
+        BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        doThrow(new IOException()).when(mockConnection).close();
+        impl.freeEntry(entry, false, 0, TimeUnit.MILLISECONDS);
+    }
 
+    @Test
+    public void wakesUpWaitingThreadsWhenEntryAvailable() throws Exception {
+        useMockOperator();
+        when(mockOperator.createConnection()).thenReturn(mockConnection);
+        impl.setMaxTotalConnections(1);
+        BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        final Flag f = new Flag(false);
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+                    f.flag = true;
+                } catch (ConnectionPoolTimeoutException e) {
+                } catch (InterruptedException e) {
+                }
+            }
+        });
+        t.start();
+        Thread.sleep(1);
+        impl.freeEntry(entry, true, 1000, TimeUnit.MILLISECONDS);
+        Thread.sleep(1);
+        assertTrue(f.flag);
+    }
+    
+    @Test
+    public void wakesUpWaitingThreadsOnOtherRoutesWhenEntryAvailable() throws Exception {
+        useMockOperator();
+        when(mockOperator.createConnection()).thenReturn(mockConnection);
+        impl.setMaxTotalConnections(1);
+        BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        final Flag f = new Flag(false);
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    impl.requestPoolEntry(route2, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+                    f.flag = true;
+                } catch (ConnectionPoolTimeoutException e) {
+                } catch (InterruptedException e) {
+                }
+            }
+        });
+        t.start();
+        Thread.sleep(1);
+        impl.freeEntry(entry, true, 1000, TimeUnit.MILLISECONDS);
+        Thread.sleep(1);
+        assertTrue(f.flag);
+    }
+    
+    @Test
+    public void doesNotRecycleExpiredConnections() throws Exception {
+        useMockOperator();
+        when(mockOperator.createConnection()).thenReturn(mockConnection, mockConnection2);
+        BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        impl.freeEntry(entry, true, 1, TimeUnit.MILLISECONDS);
+        Thread.sleep(2);
+        BasicPoolEntry entry2 = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        assertNotSame(mockConnection, entry2.getConnection());
+    }
+    
+    @Test
+    public void closesExpiredConnectionsWhenNotReusingThem() throws Exception {
+        useMockOperator();
+        when(mockOperator.createConnection()).thenReturn(mockConnection, mockConnection2);
+        BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        impl.freeEntry(entry, true, 1, TimeUnit.MILLISECONDS);
+        Thread.sleep(2);
+        impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        verify(mockConnection, atLeastOnce()).close();
+    }
+
+    
+    @Test
+    public void wakesUpWaitingThreadsOnShutdown() throws Exception {
+        useMockOperator();
+        when(mockOperator.createConnection()).thenReturn(mockConnection);
+        when(mockOperator.createConnection()).thenReturn(mockConnection);
+        impl.setMaxTotalConnections(1);
+        impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+        final Flag f = new Flag(false);
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+                } catch (IllegalStateException expected) {
+                    f.flag = true;
+                } catch (ConnectionPoolTimeoutException e) {
+                } catch (InterruptedException e) {
+                }
+            }
+        });
+        t.start();
+        Thread.sleep(1);
+        impl.shutdown();
+        Thread.sleep(1);
+        assertTrue(f.flag);
+    }
+
+    private static class Flag {
+        public boolean flag;
+        public Flag(boolean init) { flag = init; }
+    }
 }



Re: svn commit: r1151465

Posted by Jon Moore <jo...@apache.org>.
Hmm, it passed on my pre-commit test run. This is probably a race
condition in the test, I'll take a look.

On Wed, Jul 27, 2011 at 1:33 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> On Wed, 2011-07-27 at 13:54 +0000, jonm@apache.org wrote:
>> Author: jonm
>> Date: Wed Jul 27 13:54:49 2011
>> New Revision: 1151465
>>
>> URL: http://svn.apache.org/viewvc?rev=1151465&view=rev
>> Log:
>> HTTPCLIENT-1108: much better unit test coverage of ConnPoolByRoute
>>
>
> Jon
>
> One test case fails for me
>
> -------------------------------------------------------------------------------
> Test set: org.apache.http.impl.conn.tsccm.TestConnPoolByRoute
> -------------------------------------------------------------------------------
> Tests run: 28, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.206
> sec <<< FAILURE!
> wakesUpWaitingThreadsOnOtherRoutesWhenEntryAvailable(org.apache.http.impl.conn.tsccm.TestConnPoolByRoute)  Time elapsed: 0.016 sec  <<< FAILURE!
> java.lang.AssertionError:
>        at org.junit.Assert.fail(Assert.java:91)
>        at org.junit.Assert.assertTrue(Assert.java:43)
>        at org.junit.Assert.assertTrue(Assert.java:54)
>        at
> org.apache.http.impl.conn.tsccm.TestConnPoolByRoute.wakesUpWaitingThreadsOnOtherRoutesWhenEntryAvailable(TestConnPoolByRoute.java:399)
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
> For additional commands, e-mail: dev-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: svn commit: r1151465

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2011-07-27 at 13:54 +0000, jonm@apache.org wrote:
> Author: jonm
> Date: Wed Jul 27 13:54:49 2011
> New Revision: 1151465
> 
> URL: http://svn.apache.org/viewvc?rev=1151465&view=rev
> Log:
> HTTPCLIENT-1108: much better unit test coverage of ConnPoolByRoute
> 

Jon

One test case fails for me

-------------------------------------------------------------------------------
Test set: org.apache.http.impl.conn.tsccm.TestConnPoolByRoute
-------------------------------------------------------------------------------
Tests run: 28, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.206
sec <<< FAILURE!
wakesUpWaitingThreadsOnOtherRoutesWhenEntryAvailable(org.apache.http.impl.conn.tsccm.TestConnPoolByRoute)  Time elapsed: 0.016 sec  <<< FAILURE!
java.lang.AssertionError: 
	at org.junit.Assert.fail(Assert.java:91)
	at org.junit.Assert.assertTrue(Assert.java:43)
	at org.junit.Assert.assertTrue(Assert.java:54)
	at
org.apache.http.impl.conn.tsccm.TestConnPoolByRoute.wakesUpWaitingThreadsOnOtherRoutesWhenEntryAvailable(TestConnPoolByRoute.java:399)

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org