Search in sources :

Example 61 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project elasticsearch by elastic.

the class ElasticsearchUncaughtExceptionHandlerTests method testUncaughtError.

public void testUncaughtError() throws InterruptedException {
    final Error error = randomFrom(new InternalError(), new OutOfMemoryError(), new StackOverflowError(), new UnknownError(), new IOError(new IOException("fatal")), new Error() {
    });
    final Thread thread = new Thread(() -> {
        throw error;
    });
    final String name = randomAsciiOfLength(10);
    thread.setName(name);
    final AtomicBoolean halt = new AtomicBoolean();
    final AtomicInteger observedStatus = new AtomicInteger();
    final AtomicReference<String> threadNameReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    thread.setUncaughtExceptionHandler(new ElasticsearchUncaughtExceptionHandler(() -> "testUncaughtError") {

        @Override
        void halt(int status) {
            halt.set(true);
            observedStatus.set(status);
        }

        @Override
        void onFatalUncaught(String threadName, Throwable t) {
            threadNameReference.set(threadName);
            throwableReference.set(t);
        }

        @Override
        void onNonFatalUncaught(String threadName, Throwable t) {
            fail();
        }
    });
    thread.start();
    thread.join();
    assertTrue(halt.get());
    final int status;
    if (expectedStatus.containsKey(error.getClass())) {
        status = expectedStatus.get(error.getClass());
    } else {
        status = 1;
    }
    assertThat(observedStatus.get(), equalTo(status));
    assertThat(threadNameReference.get(), equalTo(name));
    assertThat(throwableReference.get(), equalTo(error));
}
Also used : IOError(java.io.IOError) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOError(java.io.IOError) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 62 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HttpClientTest method testRequestListenerForMultipleEventsIsInvokedOncePerEvent.

@Test
public void testRequestListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception {
    start(new EmptyServerHandler());
    final AtomicInteger counter = new AtomicInteger();
    Request.Listener listener = new Request.Listener() {

        @Override
        public void onQueued(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onBegin(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onHeaders(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onCommit(Request request) {
            counter.incrementAndGet();
        }

        @Override
        public void onContent(Request request, ByteBuffer content) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onSuccess(Request request) {
            counter.incrementAndGet();
        }
    };
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestQueued(listener).onRequestBegin(listener).onRequestHeaders(listener).onRequestCommit(listener).onRequestContent(listener).onRequestSuccess(listener).onRequestFailure(listener).listener(listener).send();
    Assert.assertEquals(200, response.getStatus());
    int expectedEventsTriggeredByOnRequestXXXListeners = 5;
    int expectedEventsTriggeredByListener = 5;
    int expected = expectedEventsTriggeredByOnRequestXXXListeners + expectedEventsTriggeredByListener;
    Assert.assertEquals(expected, counter.get());
}
Also used : FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Test(org.junit.Test)

Example 63 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HttpClientAsyncContentTest method testSmallAsyncContent.

@Test
public void testSmallAsyncContent() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            ServletOutputStream output = response.getOutputStream();
            output.write(65);
            output.flush();
            output.write(66);
        }
    });
    final AtomicInteger contentCount = new AtomicInteger();
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    final AtomicReference<CountDownLatch> contentLatch = new AtomicReference<>(new CountDownLatch(1));
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContentAsync(new Response.AsyncContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            contentCount.incrementAndGet();
            callbackRef.set(callback);
            contentLatch.get().countDown();
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completeLatch.countDown();
        }
    });
    Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
    Callback callback = callbackRef.get();
    // Wait a while to be sure that the parsing does not proceed.
    TimeUnit.MILLISECONDS.sleep(1000);
    Assert.assertEquals(1, contentCount.get());
    // Succeed the content callback to proceed with parsing.
    callbackRef.set(null);
    contentLatch.set(new CountDownLatch(1));
    callback.succeeded();
    Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
    callback = callbackRef.get();
    // Wait a while to be sure that the parsing does not proceed.
    TimeUnit.MILLISECONDS.sleep(1000);
    Assert.assertEquals(2, contentCount.get());
    Assert.assertEquals(1, completeLatch.getCount());
    // Succeed the content callback to proceed with parsing.
    callbackRef.set(null);
    contentLatch.set(new CountDownLatch(1));
    callback.succeeded();
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(2, contentCount.get());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 64 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HttpClientProxyTest method testProxyAuthenticationWithServerAuthentication.

@Test
public void testProxyAuthenticationWithServerAuthentication() throws Exception {
    String proxyRealm = "proxyRealm";
    String serverRealm = "serverRealm";
    int status = HttpStatus.NO_CONTENT_204;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            String authorization = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
            if (authorization == null) {
                response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
                response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + proxyRealm + "\"");
            } else {
                authorization = request.getHeader(HttpHeader.AUTHORIZATION.asString());
                if (authorization == null) {
                    response.setStatus(HttpStatus.UNAUTHORIZED_401);
                    response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"" + serverRealm + "\"");
                } else {
                    response.setStatus(status);
                }
            }
        }
    });
    String proxyHost = "localhost";
    int proxyPort = connector.getLocalPort();
    String serverHost = "server";
    int serverPort = proxyPort + 1;
    URI proxyURI = URI.create(scheme + "://" + proxyHost + ":" + proxyPort);
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(proxyURI, proxyRealm, "proxyUser", "proxyPassword"));
    URI serverURI = URI.create(scheme + "://" + serverHost + ":" + serverPort);
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(serverURI, serverRealm, "serverUser", "serverPassword"));
    client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
    final AtomicInteger requests = new AtomicInteger();
    client.getRequestListeners().add(new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.incrementAndGet();
        }
    });
    // Make a request, expect 407 + 401 + 204.
    ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(status, response1.getStatus());
    Assert.assertEquals(3, requests.get());
    // Make again the request, authentication is cached, expect 204.
    requests.set(0);
    ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(status, response2.getStatus());
    Assert.assertEquals(1, requests.get());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Test(org.junit.Test)

Example 65 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HttpClientRedirectTest method testMethodRedirect.

private void testMethodRedirect(final HttpMethod requestMethod, final HttpMethod redirectMethod, int redirectCode) throws Exception {
    start(new RedirectHandler());
    final AtomicInteger passes = new AtomicInteger();
    client.getRequestListeners().add(new org.eclipse.jetty.client.api.Request.Listener.Adapter() {

        @Override
        public void onBegin(org.eclipse.jetty.client.api.Request request) {
            int pass = passes.incrementAndGet();
            if (pass == 1) {
                if (!requestMethod.is(request.getMethod()))
                    request.abort(new Exception());
            } else if (pass == 2) {
                if (!redirectMethod.is(request.getMethod()))
                    request.abort(new Exception());
            } else {
                request.abort(new Exception());
            }
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(requestMethod).path("/" + redirectCode + "/localhost/done").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7986 Test (org.junit.Test)3775 CountDownLatch (java.util.concurrent.CountDownLatch)1072 ArrayList (java.util.ArrayList)1018 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)849 List (java.util.List)740 IOException (java.io.IOException)719 AtomicReference (java.util.concurrent.atomic.AtomicReference)574 HashMap (java.util.HashMap)499 Map (java.util.Map)460 Test (org.testng.annotations.Test)419 File (java.io.File)337 ExecutorService (java.util.concurrent.ExecutorService)337 Test (org.junit.jupiter.api.Test)334 AtomicLong (java.util.concurrent.atomic.AtomicLong)329 TimeUnit (java.util.concurrent.TimeUnit)323 HashSet (java.util.HashSet)315 Arrays (java.util.Arrays)308 Set (java.util.Set)284 Collections (java.util.Collections)266