use of javax.servlet.ReadListener in project druid by druid-io.
the class AsyncQueryForwardingServletTest method verifyServletCallsForQuery.
/**
* Verifies that the Servlet calls the right methods the right number of times.
*/
private void verifyServletCallsForQuery(Object query, boolean isSql, QueryHostFinder hostFinder, Properties properties) throws Exception {
final ObjectMapper jsonMapper = TestHelper.makeJsonMapper();
final HttpServletRequest requestMock = EasyMock.createMock(HttpServletRequest.class);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonMapper.writeValueAsBytes(query));
final ServletInputStream servletInputStream = new ServletInputStream() {
private boolean finished;
@Override
public boolean isFinished() {
return finished;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(final ReadListener readListener) {
// do nothing
}
@Override
public int read() {
final int b = inputStream.read();
if (b < 0) {
finished = true;
}
return b;
}
};
EasyMock.expect(requestMock.getContentType()).andReturn("application/json").times(2);
requestMock.setAttribute("org.apache.druid.proxy.objectMapper", jsonMapper);
EasyMock.expectLastCall();
EasyMock.expect(requestMock.getRequestURI()).andReturn(isSql ? "/druid/v2/sql" : "/druid/v2/");
EasyMock.expect(requestMock.getMethod()).andReturn("POST");
EasyMock.expect(requestMock.getInputStream()).andReturn(servletInputStream);
requestMock.setAttribute(isSql ? "org.apache.druid.proxy.sqlQuery" : "org.apache.druid.proxy.query", query);
requestMock.setAttribute("org.apache.druid.proxy.to.host", "1.2.3.4:9999");
requestMock.setAttribute("org.apache.druid.proxy.to.host.scheme", "http");
EasyMock.expectLastCall();
EasyMock.replay(requestMock);
final AtomicLong didService = new AtomicLong();
final AsyncQueryForwardingServlet servlet = new AsyncQueryForwardingServlet(new MapQueryToolChestWarehouse(ImmutableMap.of()), jsonMapper, TestHelper.makeSmileMapper(), hostFinder, null, null, new NoopServiceEmitter(), new NoopRequestLogger(), new DefaultGenericQueryMetricsFactory(), new AuthenticatorMapper(ImmutableMap.of()), properties, new ServerConfig()) {
@Override
protected void doService(final HttpServletRequest request, final HttpServletResponse response) {
didService.incrementAndGet();
}
};
servlet.service(requestMock, null);
// This test is mostly about verifying that the servlet calls the right methods the right number of times.
EasyMock.verify(hostFinder, requestMock);
Assert.assertEquals(1, didService.get());
}
use of javax.servlet.ReadListener in project jetty.project by eclipse.
the class ServerTimeoutsTest method testAsyncReadIdleTimeoutFires.
@Test
public void testAsyncReadIdleTimeoutFires() throws Exception {
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
ServletInputStream input = request.getInputStream();
input.setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
Assert.assertEquals(0, input.read());
Assert.assertFalse(input.isReady());
}
@Override
public void onAllDataRead() throws IOException {
}
@Override
public void onError(Throwable failure) {
if (failure instanceof TimeoutException) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
asyncContext.complete();
handlerLatch.countDown();
}
}
});
}
});
long idleTimeout = 2500;
setServerIdleTimeout(idleTimeout);
DeferredContentProvider contentProvider = new DeferredContentProvider(ByteBuffer.allocate(1));
CountDownLatch resultLatch = new CountDownLatch(1);
client.POST(newURI()).content(contentProvider).send(result -> {
if (result.getResponse().getStatus() == HttpStatus.INTERNAL_SERVER_ERROR_500)
resultLatch.countDown();
});
// Async read should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// Complete the request.
contentProvider.close();
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ReadListener in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncReadThrows.
private void testAsyncReadThrows(Throwable throwable) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
AsyncContext asyncContext = request.startAsync(request, response);
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
if (throwable instanceof RuntimeException)
throw (RuntimeException) throwable;
if (throwable instanceof Error)
throw (Error) throwable;
throw new IOException(throwable);
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
}
@Override
public void onError(Throwable t) {
assertScope();
Assert.assertThat("onError type", t, instanceOf(throwable.getClass()));
Assert.assertThat("onError message", t.getMessage(), is(throwable.getMessage()));
latch.countDown();
response.setStatus(500);
asyncContext.complete();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(new StringContentProvider("0123456789")).timeout(5, TimeUnit.SECONDS).send();
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
}
use of javax.servlet.ReadListener in project jetty.project by eclipse.
the class AsyncIOServletTest method testOnErrorThrows.
@Test
public void testOnErrorThrows() throws Exception {
AtomicInteger errors = new AtomicInteger();
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
if (request.getDispatcherType() == DispatcherType.ERROR) {
response.flushBuffer();
return;
}
request.startAsync(request, response);
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
throw new NullPointerException("explicitly_thrown_by_test_1");
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
}
@Override
public void onError(Throwable t) {
assertScope();
errors.incrementAndGet();
throw new NullPointerException("explicitly_thrown_by_test_2") {
{
this.initCause(t);
}
};
}
});
}
});
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
ContentResponse response = client.newRequest(newURI()).path(servletPath).content(new StringContentProvider("0123456789")).timeout(5, TimeUnit.SECONDS).send();
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
Assert.assertEquals(1, errors.get());
}
}
use of javax.servlet.ReadListener in project jetty.project by eclipse.
the class AsyncIOServletTest method testIsReadyAtEOF.
@Test
public void testIsReadyAtEOF() throws Exception {
String text = "TEST\n";
byte[] data = text.getBytes(StandardCharsets.UTF_8);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
response.flushBuffer();
AsyncContext async = request.startAsync();
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
input.setReadListener(new ReadListener() {
transient int _i = 0;
transient boolean _minusOne = false;
transient boolean _finished = false;
@Override
public void onDataAvailable() throws IOException {
assertScope();
while (input.isReady() && !input.isFinished()) {
int b = input.read();
if (b == -1)
_minusOne = true;
else if (data[_i++] != b)
throw new IllegalStateException();
}
if (input.isFinished())
_finished = true;
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
output.write(String.format("i=%d eof=%b finished=%b", _i, _minusOne, _finished).getBytes(StandardCharsets.UTF_8));
async.complete();
}
@Override
public void onError(Throwable t) {
assertScope();
t.printStackTrace();
async.complete();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).header(HttpHeader.CONNECTION, "close").content(new StringContentProvider(text)).timeout(5, TimeUnit.SECONDS).send();
String responseContent = response.getContentAsString();
assertThat(responseContent, containsString("i=" + data.length + " eof=true finished=true"));
}
Aggregations