blocking client connection passing more tests

This commit is contained in:
Greg Wilkins 2011-11-04 16:04:48 +11:00
parent c19e1de62f
commit 9b60678fbf
7 changed files with 69 additions and 39 deletions

View File

@ -119,16 +119,6 @@ public abstract class AbstractHttpConnection extends AbstractConnection implemen
_exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_COMMIT);
if (_endp.isBlocking())
{
this.notify();
}
else
{
AsyncEndPoint scep = (AsyncEndPoint)_endp;
scep.asyncDispatch();
}
adjustIdleTimeout();
return true;
@ -366,7 +356,8 @@ public abstract class AbstractHttpConnection extends AbstractConnection implemen
public String toString()
{
return "HttpConnection@" + hashCode() + "//" +
(_destination==null?"?.?.?.?:??":(_destination.getAddress().getHost() + ":" + _destination.getAddress().getPort()));
(_destination==null?"?.?.?.?:??":(_destination.getAddress().getHost() + ":" + _destination.getAddress().getPort()))+
",g="+_generator.getState()+",p="+_parser.getState();
}
public String toDetailString()
@ -431,12 +422,6 @@ public abstract class AbstractHttpConnection extends AbstractConnection implemen
protected void exchangeExpired(HttpExchange exchange)
{
System.err.println("exchangeEXPIRED "+this);
System.err.println(exchange);
System.err.println(_endp);
System.err.println(_generator);
System.err.println(_parser);
synchronized (this)
{
// We are expiring an exchange, but the exchange is pending

View File

@ -254,4 +254,13 @@ public class AsyncHttpConnection extends AbstractHttpConnection implements Async
if (_generator.isIdle())
_endp.shutdownOutput();
}
@Override
public boolean send(HttpExchange ex) throws IOException
{
boolean sent=super.send(ex);
if (sent)
_asyncEndp.asyncDispatch();
return sent;
}
}

View File

@ -1,6 +1,7 @@
package org.eclipse.jetty.client;
import java.io.IOException;
import java.io.InterruptedIOException;
import org.eclipse.jetty.http.AbstractGenerator;
import org.eclipse.jetty.http.HttpParser;
@ -46,9 +47,25 @@ public class BlockingHttpConnection extends AbstractHttpConnection
while (_endp.isOpen() && connection==this)
{
LOG.debug("open={} more={} buffering={}",_endp.isOpen(),_parser.isMoreInBuffer(),_endp.isBufferingInput());
HttpExchange exchange=_exchange;
HttpExchange exchange;
synchronized (this)
{
exchange=_exchange;
while (exchange == null)
{
try
{
this.wait();
exchange=_exchange;
}
catch (InterruptedException e)
{
throw new InterruptedIOException();
}
}
}
LOG.debug("exchange {}",exchange);
try
@ -222,4 +239,19 @@ public class BlockingHttpConnection extends AbstractHttpConnection
if (_generator.isIdle())
_endp.shutdownOutput();
}
@Override
public boolean send(HttpExchange ex) throws IOException
{
boolean sent=super.send(ex);
if (sent)
{
synchronized (this)
{
notifyAll();
}
}
return sent;
}
}

View File

@ -246,12 +246,16 @@ public class HttpDestination implements Dumpable
}
if (connection == null)
{
return null;
}
// Check if the connection was idle,
// but it expired just a moment ago
if (connection.cancelIdleTimeout())
{
return connection;
}
}
}

View File

@ -54,7 +54,7 @@ class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout());
EndPoint endpoint=new SocketEndPoint(socket);
final EndPoint endpoint=new SocketEndPoint(socket);
final AbstractHttpConnection connection=new BlockingHttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint);
connection.setDestination(destination);

View File

@ -362,36 +362,33 @@ public abstract class AbstractConnectionTest
HttpDestination dest = httpClient.getDestination(new Address("localhost", port),false);
httpClient.send(exchange);
Socket s = serverSocket.accept();
s.setSoTimeout(10000);
Socket server = serverSocket.accept();
server.setSoTimeout(5000);
byte[] buf = new byte[4096];
int len=s.getInputStream().read(buf);
System.err.println(new String(buf,0,len));
int len=server.getInputStream().read(buf);
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
s.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
System.err.println("--");
server.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
Thread.sleep(300);
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
Thread.sleep(200); // TODO get rid of this
assertEquals(1,dest.getConnections());
assertEquals(1,dest.getIdleConnections());
exchange = new ConnectionExchange();
exchange.setAddress(new Address("localhost", port));
exchange.setRequestURI("/");
httpClient.send(exchange);
System.err.println(">>");
Thread.sleep(100);
assertEquals(1,dest.getConnections());
len=s.getInputStream().read(buf);
System.err.println(new String(buf,0,len));
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
s.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
System.err.println("==");
len=server.getInputStream().read(buf);
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
server.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
Thread.sleep(500);
@ -403,12 +400,10 @@ public abstract class AbstractConnectionTest
assertEquals(0,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
System.err.println("closing");
serverSocket.close();
}
finally
{
System.err.println("stopping");
httpClient.stop();
}
}

View File

@ -88,4 +88,9 @@ public class SocketConnectionTest extends AbstractConnectionTest
httpClient.stop();
}
}
public void testIdleConnection() throws Exception
{
super.testIdleConnection();
}
}