426750 Handle autoclose on async writes

This commit is contained in:
Greg Wilkins 2014-02-03 17:59:31 +11:00
parent d4368d1018
commit 8cc9d71e20
2 changed files with 48 additions and 14 deletions

View File

@ -680,11 +680,12 @@ public class HttpOutput extends ServletOutputStream implements Runnable
{ {
Throwable th=_onError; Throwable th=_onError;
_onError=null; _onError=null;
_writeListener.onError(th); _writeListener.onError(new IOException(th));
close(); close();
} }
if (_state.get()==OutputState.READY) switch(_state.get())
{ {
case READY:
try try
{ {
_writeListener.onWritePossible(); _writeListener.onWritePossible();
@ -694,6 +695,24 @@ public class HttpOutput extends ServletOutputStream implements Runnable
_writeListener.onError(e); _writeListener.onError(e);
close(); close();
} }
break;
case CLOSED:
try
{
// even though a write is not possible, because a close has
// occurred, we need to call onWritePossible to tell async
// producer that the last write completed.
_writeListener.onWritePossible();
}
catch (Throwable e)
{
_writeListener.onError(e);
}
break;
default:
} }
} }
@ -722,7 +741,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
break; break;
case CLOSED: case CLOSED:
_onError=new EofException("Closed"); _channel.getState().onWritePossible();
break; break;
default: default:

View File

@ -547,6 +547,22 @@ public class HttpOutputTest
assertThat(response,containsString("400\tThis is a big file")); assertThat(response,containsString("400\tThis is a big file"));
} }
@Test
public void testAsyncWriteSimpleKnown() throws Exception
{
final Resource big = Resource.newClassPathResource("simple/simple.txt");
_handler._async=true;
_handler._writeLengthIfKnown=true;
_handler._content=BufferUtil.toBuffer(big,false);
_handler._arrayBuffer=new byte[4000];
String response=_connector.getResponses("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response,containsString("HTTP/1.1 200 OK"));
assertThat(response,containsString("Content-Length: 11"));
assertThat(response,containsString("simple text"));
}
static class ContentHandler extends AbstractHandler static class ContentHandler extends AbstractHandler
{ {
boolean _writeLengthIfKnown=true; boolean _writeLengthIfKnown=true;
@ -664,7 +680,6 @@ public class HttpOutputTest
BufferUtil.flipToFlush(_byteBuffer,0); BufferUtil.flipToFlush(_byteBuffer,0);
out.write(_byteBuffer); out.write(_byteBuffer);
} }
Assert.assertFalse(out.isReady());
} }
@Override @Override