Search in sources :

Example 61 with StacklessLogging

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());
    }
}
Also used : Result(org.eclipse.jetty.client.api.Result) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.Test) Request(org.eclipse.jetty.client.api.Request) IO(org.eclipse.jetty.util.IO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) DispatcherType(javax.servlet.DispatcherType) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 62 with StacklessLogging

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("=========")));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Socket(java.net.Socket) Test(org.junit.Test)

Example 63 with StacklessLogging

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();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IOException(java.io.IOException) Test(org.junit.Test)

Example 64 with StacklessLogging

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();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) URI(java.net.URI) Test(org.junit.Test)

Example 65 with StacklessLogging

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));
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Aggregations

StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)143 Test (org.junit.Test)134 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)55 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)51 ArrayList (java.util.ArrayList)46 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)45 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)35 ByteBuffer (java.nio.ByteBuffer)29 IOException (java.io.IOException)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)21 ServletException (javax.servlet.ServletException)20 CountDownLatch (java.util.concurrent.CountDownLatch)17 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)17 Matchers.containsString (org.hamcrest.Matchers.containsString)17 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)13 OutputStream (java.io.OutputStream)12 Socket (java.net.Socket)12