Issue #6383 - Fix flaky test FileBufferedResponseHandlerTest

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-06-16 11:11:13 +10:00
parent 52a72eea85
commit c490a10621
3 changed files with 35 additions and 4 deletions

View File

@ -209,7 +209,7 @@ public class BufferedResponseHandler extends HandlerWrapper
{ {
} }
private class ArrayBufferedInterceptor implements BufferedInterceptor protected class ArrayBufferedInterceptor implements BufferedInterceptor
{ {
private final Interceptor _next; private final Interceptor _next;
private final HttpChannel _channel; private final HttpChannel _channel;

View File

@ -76,7 +76,7 @@ public class FileBufferedResponseHandler extends BufferedResponseHandler
return new FileBufferedInterceptor(httpChannel, interceptor); return new FileBufferedInterceptor(httpChannel, interceptor);
} }
private class FileBufferedInterceptor implements BufferedResponseHandler.BufferedInterceptor protected class FileBufferedInterceptor implements BufferedResponseHandler.BufferedInterceptor
{ {
private static final int MAX_MAPPED_BUFFER_SIZE = Integer.MAX_VALUE / 2; private static final int MAX_MAPPED_BUFFER_SIZE = Integer.MAX_VALUE / 2;
@ -111,7 +111,7 @@ public class FileBufferedResponseHandler extends BufferedResponseHandler
BufferedInterceptor.super.resetBuffer(); BufferedInterceptor.super.resetBuffer();
} }
private void dispose() protected void dispose()
{ {
IO.close(_fileOutputStream); IO.close(_fileOutputStream);
_fileOutputStream = null; _fileOutputStream = null;

View File

@ -30,6 +30,7 @@ import java.nio.file.Path;
import java.time.Duration; import java.time.Duration;
import java.util.Random; import java.util.Random;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -63,6 +64,7 @@ public class FileBufferedResponseHandlerTest
{ {
private static final Logger LOG = Log.getLogger(FileBufferedResponseHandlerTest.class); private static final Logger LOG = Log.getLogger(FileBufferedResponseHandlerTest.class);
private final CountDownLatch _disposeLatch = new CountDownLatch(1);
private Server _server; private Server _server;
private LocalConnector _localConnector; private LocalConnector _localConnector;
private ServerConnector _serverConnector; private ServerConnector _serverConnector;
@ -86,7 +88,22 @@ public class FileBufferedResponseHandlerTest
_serverConnector = new ServerConnector(_server, new HttpConnectionFactory(config)); _serverConnector = new ServerConnector(_server, new HttpConnectionFactory(config));
_server.addConnector(_serverConnector); _server.addConnector(_serverConnector);
_bufferedHandler = new FileBufferedResponseHandler(); _bufferedHandler = new FileBufferedResponseHandler()
{
@Override
protected BufferedInterceptor newBufferedInterceptor(HttpChannel httpChannel, HttpOutput.Interceptor interceptor)
{
return new FileBufferedInterceptor(httpChannel, interceptor)
{
@Override
protected void dispose()
{
super.dispose();
_disposeLatch.countDown();
}
};
}
};
_bufferedHandler.setTempDir(_testDir); _bufferedHandler.setTempDir(_testDir);
_bufferedHandler.getPathIncludeExclude().include("/include/*"); _bufferedHandler.getPathIncludeExclude().include("/include/*");
_bufferedHandler.getPathIncludeExclude().exclude("*.exclude"); _bufferedHandler.getPathIncludeExclude().exclude("*.exclude");
@ -157,6 +174,8 @@ public class FileBufferedResponseHandlerTest
assertThat(response.getStatus(), is(HttpStatus.OK_200)); assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(responseContent, containsString("Committed: false")); assertThat(responseContent, containsString("Committed: false"));
assertThat(responseContent, containsString("NumFiles: 1")); assertThat(responseContent, containsString("NumFiles: 1"));
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -249,6 +268,8 @@ public class FileBufferedResponseHandlerTest
assertThat(responseContent, containsString("NumFilesBeforeFlush: 0")); assertThat(responseContent, containsString("NumFilesBeforeFlush: 0"));
assertThat(responseContent, containsString("Committed: false")); assertThat(responseContent, containsString("Committed: false"));
assertThat(responseContent, containsString("NumFiles: 1")); assertThat(responseContent, containsString("NumFiles: 1"));
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -279,6 +300,8 @@ public class FileBufferedResponseHandlerTest
assertThat(response.getStatus(), is(HttpStatus.OK_200)); assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(responseContent, not(containsString("writtenAfterClose"))); assertThat(responseContent, not(containsString("writtenAfterClose")));
assertThat(responseContent, containsString("NumFiles: 1")); assertThat(responseContent, containsString("NumFiles: 1"));
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -339,6 +362,8 @@ public class FileBufferedResponseHandlerTest
// The flush should not create the file unless there is content to write. // The flush should not create the file unless there is content to write.
assertThat(response.getStatus(), is(HttpStatus.OK_200)); assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(responseContent, containsString("NumFiles: 0")); assertThat(responseContent, containsString("NumFiles: 0"));
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -378,6 +403,8 @@ public class FileBufferedResponseHandlerTest
assertThat(responseContent, containsString("NumFilesBeforeReset: 1")); assertThat(responseContent, containsString("NumFilesBeforeReset: 1"));
assertThat(responseContent, containsString("NumFilesAfterReset: 0")); assertThat(responseContent, containsString("NumFilesAfterReset: 0"));
assertThat(responseContent, containsString("NumFilesAfterWrite: 1")); assertThat(responseContent, containsString("NumFilesAfterWrite: 1"));
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -451,6 +478,8 @@ public class FileBufferedResponseHandlerTest
assertThat(response.get("NumFiles"), is("1")); assertThat(response.get("NumFiles"), is("1"));
assertThat(response.get("FileSize"), is(Long.toString(fileSize))); assertThat(response.get("FileSize"), is(Long.toString(fileSize)));
assertThat(received.get(), is(fileSize)); assertThat(received.get(), is(fileSize));
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -531,6 +560,7 @@ public class FileBufferedResponseHandlerTest
assertThat(error.getMessage(), containsString("intentionally throwing from interceptor")); assertThat(error.getMessage(), containsString("intentionally throwing from interceptor"));
// All files were deleted. // All files were deleted.
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }
@ -579,6 +609,7 @@ public class FileBufferedResponseHandlerTest
assertThat(error, instanceOf(NoSuchFileException.class)); assertThat(error, instanceOf(NoSuchFileException.class));
// No files were created. // No files were created.
assertTrue(_disposeLatch.await(5, TimeUnit.SECONDS));
assertThat(getNumFiles(), is(0)); assertThat(getNumFiles(), is(0));
} }