use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.
the class HttpRequestAbortTest method testAbortOnContent.
@Test
public void testAbortOnContent() throws Exception {
try (StacklessLogging suppressor = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
CountDownLatch serverLatch = new CountDownLatch(1);
start(new EmptyServerHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
super.handle(target, baseRequest, request, response);
if (request.getDispatcherType() != DispatcherType.ERROR)
IO.copy(request.getInputStream(), response.getOutputStream());
} finally {
serverLatch.countDown();
}
}
});
final Throwable cause = new Exception();
final AtomicBoolean aborted = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestContent((request, content) -> {
aborted.set(request.abort(cause));
latch.countDown();
}).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {
@Override
public long getLength() {
return -1;
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
if (aborted.get())
Assert.assertSame(cause, x.getCause());
}
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
}
use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.
the class ServerConnectorTimeoutTest method testHttpWriteIdleTimeout.
@Test
public void testHttpWriteIdleTimeout() throws Exception {
_httpConfiguration.setBlockingTimeout(500);
configureServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
client.setSoTimeout(10000);
Assert.assertFalse(client.isClosed());
final OutputStream os = client.getOutputStream();
final InputStream is = client.getInputStream();
final StringBuilder response = new StringBuilder();
CompletableFuture<Void> responseFuture = CompletableFuture.runAsync(() -> {
try (InputStreamReader reader = new InputStreamReader(is, UTF_8)) {
int c;
while ((c = reader.read()) != -1) {
response.append((char) c);
}
} catch (IOException e) {
// Valid path (as connection is forcibly closed)
// t.printStackTrace(System.err);
}
});
CompletableFuture<Void> requestFuture = CompletableFuture.runAsync(() -> {
try {
os.write(("POST /echo HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 20\r\n" + "\r\n").getBytes("utf-8"));
os.flush();
os.write("123456789\n".getBytes("utf-8"));
os.flush();
TimeUnit.SECONDS.sleep(1);
os.write("=========\n".getBytes("utf-8"));
os.flush();
} catch (InterruptedException | IOException e) {
// Valid path, as write of second half of content can fail
// e.printStackTrace(System.err);
}
});
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
requestFuture.get(2, TimeUnit.SECONDS);
responseFuture.get(3, TimeUnit.SECONDS);
Assert.assertThat(response.toString(), containsString(" 500 "));
Assert.assertThat(response.toString(), Matchers.not(containsString("=========")));
}
}
use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.
the class ServerConnectorTest method testExceptionWhileAccepting.
@Test
public void testExceptionWhileAccepting() throws Exception {
Server server = new Server();
try (StacklessLogging stackless = new StacklessLogging(AbstractConnector.class)) {
AtomicLong spins = new AtomicLong();
ServerConnector connector = new ServerConnector(server) {
@Override
public void accept(int acceptorID) throws IOException {
spins.incrementAndGet();
throw new IOException("explicitly_thrown_by_test");
}
};
server.addConnector(connector);
server.start();
Thread.sleep(1000);
assertThat(spins.get(), Matchers.lessThan(5L));
} finally {
server.stop();
}
}
use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.
the class RequestLogHandlerTest method testLogHandlerWrapped.
@Test(timeout = 4000)
public void testLogHandlerWrapped() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
CaptureLog captureLog = new CaptureLog();
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
requestLog.setHandler(testHandler);
server.setHandler(requestLog);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class, HttpChannelState.class)) {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.info("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.info("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.
the class MisbehavingClassTest method testAnnotatedRuntimeOnOpen.
@SuppressWarnings("Duplicates")
@Test
public void testAnnotatedRuntimeOnOpen() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
AnnotatedRuntimeOnOpen socket = new AnnotatedRuntimeOnOpen();
try (StacklessLogging logging = new StacklessLogging(AnnotatedRuntimeOnOpen.class, WebSocketSession.class)) {
// expecting IOException during onOpen
expectedException.expect(IOException.class);
expectedException.expectCause(instanceOf(RuntimeException.class));
container.connectToServer(socket, serverUri);
expectedException.reportMissingExceptionWithMessage("Should have failed .connectToServer()");
assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
Throwable cause = socket.errors.pop();
assertThat("Error", cause, instanceOf(ArrayIndexOutOfBoundsException.class));
}
}
Aggregations