Search in sources :

Example 21 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testDispatchBadRequestUnknownCause.

public void testDispatchBadRequestUnknownCause() {
    final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build();
    final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
    restController.dispatchBadRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY), null);
    assertTrue(channel.getSendResponseCalled());
    assertThat(channel.getRestResponse().content().utf8ToString(), containsString("unknown cause"));
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 22 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testDispatchBadRequest.

public void testDispatchBadRequest() {
    final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build();
    final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
    restController.dispatchBadRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY), randomBoolean() ? new IllegalStateException("bad request") : new Throwable("bad request"));
    assertTrue(channel.getSendResponseCalled());
    assertThat(channel.getRestResponse().content().utf8ToString(), containsString("bad request"));
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 23 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testDispatchRequiresContentTypeForRequestsWithContent.

public void testDispatchRequiresContentTypeForRequestsWithContent() {
    String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
    TestRestRequest request = new TestRestRequest("/", content, null);
    AssertingChannel channel = new AssertingChannel(request, true, RestStatus.NOT_ACCEPTABLE);
    restController = new RestController(Settings.builder().put(HttpTransportSettings.SETTING_HTTP_CONTENT_TYPE_REQUIRED.getKey(), true).build(), Collections.emptySet(), null, null, circuitBreakerService);
    restController.registerHandler(RestRequest.Method.GET, "/", (r, c, client) -> c.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)));
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(request, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 24 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testNonStreamingXContentCausesErrorResponse.

public void testNonStreamingXContentCausesErrorResponse() throws IOException {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(YamlXContent.contentBuilder().startObject().endObject().bytes(), XContentType.YAML).withPath("/foo").build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.NOT_ACCEPTABLE);
    restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }

        @Override
        public boolean supportsContentStream() {
            return true;
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException)

Example 25 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testDispatchRequestAddsAndFreesBytesOnlyOnceOnError.

public void testDispatchRequestAddsAndFreesBytesOnlyOnceOnError() {
    int contentLength = BREAKER_LIMIT.bytesAsInt();
    String content = randomAsciiOfLength(contentLength);
    // we will produce an error in the rest handler and one more when sending the error response
    TestRestRequest request = new TestRestRequest("/error", content, XContentType.JSON);
    ExceptionThrowingChannel channel = new ExceptionThrowingChannel(request, true);
    restController.dispatchRequest(request, channel, new ThreadContext(Settings.EMPTY));
    assertEquals(0, inFlightRequestsBreaker.getTrippedCount());
    assertEquals(0, inFlightRequestsBreaker.getUsed());
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)33 Matchers.containsString (org.hamcrest.Matchers.containsString)15 IOException (java.io.IOException)12 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)12 NodeClient (org.elasticsearch.client.node.NodeClient)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 List (java.util.List)7 ActionListener (org.elasticsearch.action.ActionListener)4 Before (org.junit.Before)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Settings (org.elasticsearch.common.settings.Settings)3 TransportAddress (org.elasticsearch.common.transport.TransportAddress)3 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HashMap (java.util.HashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Strings.collectionToDelimitedString (org.elasticsearch.common.Strings.collectionToDelimitedString)2