Fixes #337896 (HttpExchange.timeout does not override HttpClient.timeout)

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2817 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Simone Bordet 2011-02-22 20:46:24 +00:00
parent 373574cbcc
commit f092ddbe0d
3 changed files with 126 additions and 126 deletions

View File

@ -16,6 +16,7 @@ jetty-7.3.1-SNAPSHOT
+ 337784 Improve HashSessionManager for session migrations
+ JETTY-1331 Allow alternate XML configuration processors (eg spring)
+ JETTY-1335 HttpClient's SelectConnector clean-up
+ 337896 HttpExchange.timeout does not override HttpClient.timeout
jetty-7.3.0.v20110203 3 February 2011
+ JETTY-1259 NullPointerException in JDBCSessionIdManager when invalidating session (further update)

View File

@ -248,7 +248,7 @@ public class HttpClient extends HttpBuffers implements Attributes
public void schedule(Timeout.Task task, long timeout)
{
_timeoutQ.schedule(task, timeout);
_timeoutQ.schedule(task, timeout - _timeoutQ.getDuration());
}
/* ------------------------------------------------------------ */

View File

@ -1,27 +1,26 @@
package org.eclipse.jetty.client;
// ========================================================================
//All rights reserved. This program and the accompanying materials
//are made available under the terms of the Eclipse Public License v1.0
//and Apache License v2.0 which accompanies this distribution.
//The Eclipse Public License is available at
//http://www.eclipse.org/legal/epl-v10.html
//The Apache License v2.0 is available at
//http://www.opensource.org/licenses/apache2.0.php
//You may elect to redistribute this code under either of these licenses.
// Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.client;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeaders;
import junit.framework.Assert;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteArrayBuffer;
@ -31,32 +30,26 @@ import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import junit.framework.Assert;
import junit.framework.TestCase;
public class TimeoutExchangeTest extends TestCase
public class TimeoutExchangeTest
{
private HttpClient _httpClient;
private Server _server;
private int _port;
protected HttpClient _httpClient;
protected int _maxConnectionsPerAddress = 2;
protected String _scheme = "http://";
protected Server _server;
protected int _port;
protected Connector _connector;
@Before
public void setUp() throws Exception
{
startServer();
createClient();
}
@After
public void tearDown() throws Exception
{
_httpClient.stop();
Thread.sleep(500);
stopClient();
stopServer();
}
@ -64,100 +57,137 @@ public class TimeoutExchangeTest extends TestCase
{
_server = new Server();
_server.setGracefulShutdown(500);
_connector = new SelectChannelConnector();
_connector.setPort(0);
_server.setConnectors(new Connector[]
{ _connector });
Connector _connector = new SelectChannelConnector();
_server.addConnector(_connector);
Handler handler = new AbstractHandler()
{
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
try
{
// let's sleep for 0.7 sec as the default timeout is 0.5 sec
Thread.sleep(700);
Long sleep = Long.parseLong(request.getParameter("sleep"));
Thread.sleep(sleep);
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello</h1>");
baseRequest.setHandled(true);
}
catch (Exception e)
catch (InterruptedException x)
{
e.printStackTrace();
Thread.currentThread().interrupt();
throw new ServletException(x);
}
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello</h1>");
((Request)request).setHandled(true);
}
};
_server.setHandler(handler);
_server.start();
_port = _connector.getLocalPort();
}
private void stopServer() throws Exception
{
if (_server != null)
{
_server.stop();
_server = null;
}
_server.stop();
_server.join();
_server = null;
}
private void createClient() throws Exception
private void startClient(long clientTimeout) throws Exception
{
_httpClient = new HttpClient();
_httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
_httpClient.setMaxConnectionsPerAddress(_maxConnectionsPerAddress);
// default timeout = 500 ms
_httpClient.setTimeout(500);
_httpClient.setMaxConnectionsPerAddress(2);
_httpClient.setTimeout(clientTimeout);
_httpClient.start();
}
public void testTimeouts() throws Exception
private void stopClient() throws Exception
{
CustomContentExchange httpExchange = new CustomContentExchange();
httpExchange.setURL(_scheme + "localhost:" + _port);
httpExchange.setMethod(HttpMethods.POST);
httpExchange.setRequestContent(new ByteArrayBuffer("<h1>??</h1>"));
// let's use the default timeout - the one set on the HttpClient
// (500 ms)
_httpClient.send(httpExchange);
httpExchange.getDoneLatch().await(900,TimeUnit.MILLISECONDS);
// we should get a timeout - the server sleeps for 700 ms
Assert.assertTrue(httpExchange.isTimeoutOccurred());
Assert.assertFalse(httpExchange.isResponseReceived());
// let's do it again - with a custom timeout
httpExchange = new CustomContentExchange();
httpExchange.setURL(_scheme + "localhost:" + _port);
httpExchange.setMethod(HttpMethods.POST);
httpExchange.setRequestContent(new ByteArrayBuffer("<h1>??</h1>"));
httpExchange.setTimeout(500);
// let's use a custom timeout - 500 ms (the default one) + 500 ms
// delay = 1000 ms
_httpClient.send(httpExchange);
httpExchange.getDoneLatch().await(1200,TimeUnit.MILLISECONDS);
// we should not get a timeout - the server sleeps for 700 ms
// while we wait for 1200 ms
Assert.assertFalse(httpExchange.isTimeoutOccurred());
Assert.assertTrue(httpExchange.isResponseReceived());
_httpClient.stop();
// Thread.sleep(500);
}
class CustomContentExchange extends ContentExchange
@Test
public void testDefaultTimeoutNotExpiring() throws Exception
{
startClient(1000);
long serverSleep = 500;
protected final CountDownLatch _doneLatch = new CountDownLatch(1);
protected boolean _errorOccurred = false;
protected boolean _timeoutOccurred = false;
protected boolean _responseReceived = false;
CustomContentExchange httpExchange = new CustomContentExchange();
httpExchange.setURL("http://localhost:" + _port + "/?sleep=" + serverSleep);
httpExchange.setMethod(HttpMethods.POST);
httpExchange.setRequestContent(new ByteArrayBuffer("<h1>??</h1>"));
_httpClient.send(httpExchange);
Assert.assertTrue(httpExchange.getDoneLatch().await(4 * serverSleep, TimeUnit.MILLISECONDS));
Assert.assertFalse(httpExchange.isTimeoutOccurred());
Assert.assertTrue(httpExchange.isResponseReceived());
Assert.assertFalse(httpExchange.isErrorOccurred());
}
@Test
public void testDefaultTimeoutExpiring() throws Exception
{
startClient(500);
long serverSleep = 1000;
CustomContentExchange httpExchange = new CustomContentExchange();
httpExchange.setURL("http://localhost:" + _port + "/?sleep=" + serverSleep);
httpExchange.setMethod(HttpMethods.POST);
httpExchange.setRequestContent(new ByteArrayBuffer("<h1>??</h1>"));
_httpClient.send(httpExchange);
Assert.assertTrue(httpExchange.getDoneLatch().await(2 * serverSleep, TimeUnit.MILLISECONDS));
Assert.assertTrue(httpExchange.isTimeoutOccurred());
Assert.assertFalse(httpExchange.isResponseReceived());
Assert.assertFalse(httpExchange.isErrorOccurred());
}
@Test
public void testExchangeTimeoutNotExpiring() throws Exception
{
startClient(500);
long serverSleep = 1000;
long exchangeTimeout = 1500;
CustomContentExchange httpExchange = new CustomContentExchange();
httpExchange.setURL("http://localhost:" + _port + "/?sleep=" + serverSleep);
httpExchange.setMethod(HttpMethods.POST);
httpExchange.setRequestContent(new ByteArrayBuffer("<h1>??</h1>"));
httpExchange.setTimeout(exchangeTimeout);
_httpClient.send(httpExchange);
Assert.assertTrue(httpExchange.getDoneLatch().await(2 * exchangeTimeout, TimeUnit.MILLISECONDS));
Assert.assertFalse(httpExchange.isTimeoutOccurred());
Assert.assertTrue(httpExchange.isResponseReceived());
Assert.assertFalse(httpExchange.isErrorOccurred());
}
@Test
public void testExchangeTimeoutExpiring() throws Exception
{
startClient(2000);
long serverSleep = 1000;
long exchangeTimeout = 500;
CustomContentExchange httpExchange = new CustomContentExchange();
httpExchange.setURL("http://localhost:" + _port + "/?sleep=" + serverSleep);
httpExchange.setMethod(HttpMethods.POST);
httpExchange.setRequestContent(new ByteArrayBuffer("<h1>??</h1>"));
httpExchange.setTimeout(exchangeTimeout);
_httpClient.send(httpExchange);
Assert.assertTrue(httpExchange.getDoneLatch().await(2 * serverSleep, TimeUnit.MILLISECONDS));
Assert.assertTrue(httpExchange.isTimeoutOccurred());
Assert.assertFalse(httpExchange.isResponseReceived());
Assert.assertFalse(httpExchange.isErrorOccurred());
}
private class CustomContentExchange extends ContentExchange
{
private final CountDownLatch _doneLatch = new CountDownLatch(1);
private boolean _errorOccurred = false;
private boolean _timeoutOccurred = false;
private boolean _responseReceived = false;
public boolean isErrorOccurred()
{
@ -179,24 +209,6 @@ public class TimeoutExchangeTest extends TestCase
super(true);
}
@Override
protected void onRequestComplete() throws IOException
{
// close the input stream when its not needed anymore
InputStream is = getRequestContentSource();
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
protected void onResponseComplete() throws IOException
{
@ -249,20 +261,8 @@ public class TimeoutExchangeTest extends TestCase
}
}
public String getBody() throws UnsupportedEncodingException
{
return super.getResponseContent();
}
public String getUrl()
{
String params = getRequestFields().getStringField(HttpHeaders.CONTENT_ENCODING);
return getScheme() + "//" + getAddress().toString() + getURI() + (params != null?"?" + params:"");
}
protected void doTaskCompleted()
{
int exchangeState = getStatus();
try
@ -270,7 +270,7 @@ public class TimeoutExchangeTest extends TestCase
if (exchangeState == HttpExchange.STATUS_COMPLETED)
{
// process the response as the state is ok
try
try
{
int responseCode = getResponseStatus();
@ -323,5 +323,4 @@ public class TimeoutExchangeTest extends TestCase
return _doneLatch;
}
}
}