Search in sources :

Example 1 with ThreadContext

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

the class RestControllerTests method testDispatchWithContentStream.

public void testDispatchWithContentStream() {
    final String mimeType = randomFrom("application/json", "application/smile");
    String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray(content), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList(mimeType))).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);
    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 : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 2 with ThreadContext

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

the class RestControllerTests method testApplyRelevantHeaders.

public void testApplyRelevantHeaders() throws Exception {
    final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
    Set<String> headers = new HashSet<>(Arrays.asList("header.1", "header.2"));
    final RestController restController = new RestController(Settings.EMPTY, headers, null, null, circuitBreakerService);
    Map<String, List<String>> restHeaders = new HashMap<>();
    restHeaders.put("header.1", Collections.singletonList("true"));
    restHeaders.put("header.2", Collections.singletonList("true"));
    restHeaders.put("header.3", Collections.singletonList("false"));
    restController.dispatchRequest(new FakeRestRequest.Builder(xContentRegistry()).withHeaders(restHeaders).build(), null, null, threadContext, (RestRequest request, RestChannel channel, NodeClient client) -> {
        assertEquals("true", threadContext.getHeader("header.1"));
        assertEquals("true", threadContext.getHeader("header.2"));
        assertNull(threadContext.getHeader("header.3"));
    });
    // the rest controller relies on the caller to stash the context, so we should expect these values here as we didn't stash the
    // context in this test
    assertEquals("true", threadContext.getHeader("header.1"));
    assertEquals("true", threadContext.getHeader("header.2"));
    assertNull(threadContext.getHeader("header.3"));
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) HashMap(java.util.HashMap) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) List(java.util.List) HashSet(java.util.HashSet)

Example 3 with ThreadContext

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

the class RestControllerTests method testDispatchRequestAddsAndFreesBytesOnSuccess.

public void testDispatchRequestAddsAndFreesBytesOnSuccess() {
    int contentLength = BREAKER_LIMIT.bytesAsInt();
    String content = randomAsciiOfLength(contentLength);
    TestRestRequest request = new TestRestRequest("/", content, XContentType.JSON);
    AssertingChannel channel = new AssertingChannel(request, true, RestStatus.OK);
    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)

Example 4 with ThreadContext

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

the class RestControllerTests method testDispatchWithContentStreamNoContentType.

public void testDispatchWithContentStreamNoContentType() {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray("{}"), null).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 : BytesArray(org.elasticsearch.common.bytes.BytesArray) 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 5 with ThreadContext

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

the class RestControllerTests method testDispatchRequestAddsAndFreesBytesOnError.

public void testDispatchRequestAddsAndFreesBytesOnError() {
    int contentLength = BREAKER_LIMIT.bytesAsInt();
    String content = randomAsciiOfLength(contentLength);
    TestRestRequest request = new TestRestRequest("/error", content, XContentType.JSON);
    AssertingChannel channel = new AssertingChannel(request, true, RestStatus.BAD_REQUEST);
    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