Merged branch 'jetty-9.4.x' into 'master'.

This commit is contained in:
Simone Bordet 2017-10-25 11:23:00 +02:00
commit 52a585a2ea
1 changed files with 22 additions and 50 deletions

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.client;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -44,7 +43,7 @@ public class RoundRobinConnectionPoolTest extends AbstractHttpClientServerTest
}
@Test
public void testLiveTimeout() throws Exception
public void testRoundRobin() throws Exception
{
List<Integer> remotePorts = new ArrayList<>();
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
{
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;
client.getTransport().setConnectionPoolFactory(destination ->
int maxConnections = 3;
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);
pool.setLiveTimeout(liveTimeout);
return pool;
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
// Make a request to create the initial connection.
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
// Wait a bit.
Thread.sleep(liveTimeout / 2);
// 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))));
Assert.assertThat(remotePorts.size(), Matchers.equalTo(requests));
for (int i = 0; i < requests; ++i)
{
int base = i % maxConnections;
int expected = remotePorts.get(base);
int candidate = remotePorts.get(i);
Assert.assertThat(expected, Matchers.equalTo(candidate));
if (i > 0)
Assert.assertThat(remotePorts.get(i - 1), Matchers.not(Matchers.equalTo(candidate)));
}
}
}