Issue #1893 - Jetty HttpClient Connection TTL.

Removed test for TTL functionality.
Added test for the round robin behavior.
This commit is contained in:
Simone Bordet 2017-10-25 11:22:46 +02:00
parent 6e8242d1a4
commit 76e744979b
1 changed files with 22 additions and 50 deletions

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.client; package org.eclipse.jetty.client;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -44,7 +43,7 @@ public class RoundRobinConnectionPoolTest extends AbstractHttpClientServerTest
} }
@Test @Test
public void testLiveTimeout() throws Exception public void testRoundRobin() throws Exception
{ {
List<Integer> remotePorts = new ArrayList<>(); List<Integer> remotePorts = new ArrayList<>();
start(new EmptyServerHandler() start(new EmptyServerHandler()
@ -53,58 +52,31 @@ public class RoundRobinConnectionPoolTest extends AbstractHttpClientServerTest
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ {
remotePorts.add(request.getRemotePort()); remotePorts.add(request.getRemotePort());
if (target.equals("/wait"))
{
long time = Long.parseLong(request.getParameter("time"));
try
{
Thread.sleep(time);
}
catch (InterruptedException x)
{
throw new InterruptedIOException();
}
}
} }
}); });
long liveTimeout = 1000; int maxConnections = 3;
client.getTransport().setConnectionPoolFactory(destination -> client.getTransport().setConnectionPoolFactory(destination -> new RoundRobinConnectionPool(destination, maxConnections, destination));
int requests = 2 * maxConnections - 1;
for (int i = 0; i < requests; ++i)
{ {
RoundRobinConnectionPool pool = new RoundRobinConnectionPool(destination, 1, destination); ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
pool.setLiveTimeout(liveTimeout); .scheme(scheme)
return pool; .timeout(5, TimeUnit.SECONDS)
}); .send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
// Make a request to create the initial connection. Assert.assertThat(remotePorts.size(), Matchers.equalTo(requests));
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) for (int i = 0; i < requests; ++i)
.scheme(scheme) {
.timeout(5, TimeUnit.SECONDS) int base = i % maxConnections;
.send(); int expected = remotePorts.get(base);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); int candidate = remotePorts.get(i);
Assert.assertThat(expected, Matchers.equalTo(candidate));
// Wait a bit. if (i > 0)
Thread.sleep(liveTimeout / 2); Assert.assertThat(remotePorts.get(i - 1), Matchers.not(Matchers.equalTo(candidate)));
}
// Live timeout will expire during this request.
response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/wait")
.param("time", String.valueOf(liveTimeout))
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
// Make another request to be sure another connection will be used.
response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertThat(remotePorts.size(), Matchers.equalTo(3));
Assert.assertThat(remotePorts.get(0), Matchers.equalTo(remotePorts.get(1)));
// Third request must be on a different connection.
Assert.assertThat(remotePorts.get(1), Matchers.not(Matchers.equalTo(remotePorts.get(2))));
} }
} }