Issue #1893 - Jetty HttpClient Connection TTL.
Removed test for TTL functionality. Added test for the round robin behavior.
This commit is contained in:
parent
6e8242d1a4
commit
76e744979b
|
@ -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));
|
||||||
{
|
|
||||||
RoundRobinConnectionPool pool = new RoundRobinConnectionPool(destination, 1, destination);
|
|
||||||
pool.setLiveTimeout(liveTimeout);
|
|
||||||
return pool;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Make a request to create the initial connection.
|
int requests = 2 * maxConnections - 1;
|
||||||
|
for (int i = 0; i < requests; ++i)
|
||||||
|
{
|
||||||
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.timeout(5, TimeUnit.SECONDS)
|
.timeout(5, TimeUnit.SECONDS)
|
||||||
.send();
|
.send();
|
||||||
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
|
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
// Wait a bit.
|
Assert.assertThat(remotePorts.size(), Matchers.equalTo(requests));
|
||||||
Thread.sleep(liveTimeout / 2);
|
for (int i = 0; i < requests; ++i)
|
||||||
|
{
|
||||||
// Live timeout will expire during this request.
|
int base = i % maxConnections;
|
||||||
response = client.newRequest("localhost", connector.getLocalPort())
|
int expected = remotePorts.get(base);
|
||||||
.scheme(scheme)
|
int candidate = remotePorts.get(i);
|
||||||
.path("/wait")
|
Assert.assertThat(expected, Matchers.equalTo(candidate));
|
||||||
.param("time", String.valueOf(liveTimeout))
|
if (i > 0)
|
||||||
.timeout(5, TimeUnit.SECONDS)
|
Assert.assertThat(remotePorts.get(i - 1), Matchers.not(Matchers.equalTo(candidate)));
|
||||||
.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))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue