Search in sources :

Example 6 with TestingResponse

use of com.facebook.airlift.http.client.testing.TestingResponse in project presto by prestodb.

the class MockExchangeRequestProcessor method handle.

@Override
public Response handle(Request request) {
    if (request.getMethod().equalsIgnoreCase("DELETE")) {
        return new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]);
    }
    // verify we got a data size and it parses correctly
    assertTrue(!request.getHeaders().get(PrestoHeaders.PRESTO_MAX_SIZE).isEmpty());
    DataSize maxSize = DataSize.valueOf(request.getHeader(PrestoHeaders.PRESTO_MAX_SIZE));
    assertTrue(maxSize.compareTo(expectedMaxSize) <= 0);
    requestMaxSizes.add(maxSize);
    RequestLocation requestLocation = new RequestLocation(request.getUri());
    URI location = requestLocation.getLocation();
    BufferResult result = buffers.getUnchecked(location).getPages(requestLocation.getSequenceId(), maxSize);
    byte[] bytes = new byte[0];
    HttpStatus status;
    if (!result.getSerializedPages().isEmpty()) {
        DynamicSliceOutput sliceOutput = new DynamicSliceOutput(64);
        PagesSerdeUtil.writeSerializedPages(sliceOutput, result.getSerializedPages());
        bytes = dataChanger.apply(sliceOutput.slice().getBytes());
        status = HttpStatus.OK;
    } else {
        status = HttpStatus.NO_CONTENT;
    }
    return new TestingResponse(status, ImmutableListMultimap.of(CONTENT_TYPE, PRESTO_PAGES, PRESTO_TASK_INSTANCE_ID, String.valueOf(result.getTaskInstanceId()), PRESTO_PAGE_TOKEN, String.valueOf(result.getToken()), PRESTO_PAGE_NEXT_TOKEN, String.valueOf(result.getNextToken()), PRESTO_BUFFER_COMPLETE, String.valueOf(result.isBufferComplete())), bytes);
}
Also used : TestingResponse(com.facebook.airlift.http.client.testing.TestingResponse) BufferResult(com.facebook.presto.execution.buffer.BufferResult) HttpStatus(com.facebook.airlift.http.client.HttpStatus) DataSize(io.airlift.units.DataSize) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) URI(java.net.URI)

Example 7 with TestingResponse

use of com.facebook.airlift.http.client.testing.TestingResponse in project presto by prestodb.

the class TestingExchangeHttpClientHandler method handle.

@Override
public Response handle(Request request) {
    ImmutableList<String> parts = ImmutableList.copyOf(Splitter.on("/").omitEmptyStrings().split(request.getUri().getPath()));
    if (request.getMethod().equals("DELETE")) {
        assertEquals(parts.size(), 1);
        return new TestingResponse(HttpStatus.OK, ImmutableListMultimap.of(), new byte[0]);
    }
    assertEquals(parts.size(), 2);
    String taskId = parts.get(0);
    int pageToken = Integer.parseInt(parts.get(1));
    ImmutableListMultimap.Builder<String, String> headers = ImmutableListMultimap.builder();
    headers.put(PRESTO_TASK_INSTANCE_ID, "task-instance-id");
    headers.put(PRESTO_PAGE_TOKEN, String.valueOf(pageToken));
    TestingTaskBuffer taskBuffer = taskBuffers.getUnchecked(taskId);
    Page page = taskBuffer.getPage(pageToken);
    headers.put(CONTENT_TYPE, PRESTO_PAGES);
    if (page != null) {
        headers.put(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(pageToken + 1));
        headers.put(PRESTO_BUFFER_COMPLETE, String.valueOf(false));
        DynamicSliceOutput output = new DynamicSliceOutput(256);
        PagesSerdeUtil.writePages(PAGES_SERDE, output, page);
        return new TestingResponse(HttpStatus.OK, headers.build(), output.slice().getInput());
    } else if (taskBuffer.isFinished()) {
        headers.put(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(pageToken));
        headers.put(PRESTO_BUFFER_COMPLETE, String.valueOf(true));
        return new TestingResponse(HttpStatus.OK, headers.build(), new byte[0]);
    } else {
        headers.put(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(pageToken));
        headers.put(PRESTO_BUFFER_COMPLETE, String.valueOf(false));
        return new TestingResponse(HttpStatus.NO_CONTENT, headers.build(), new byte[0]);
    }
}
Also used : TestingResponse(com.facebook.airlift.http.client.testing.TestingResponse) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Page(com.facebook.presto.common.Page)

Aggregations

TestingResponse (com.facebook.airlift.http.client.testing.TestingResponse)7 URI (java.net.URI)5 Test (org.testng.annotations.Test)5 TestingHttpClient (com.facebook.airlift.http.client.testing.TestingHttpClient)4 DataSize (io.airlift.units.DataSize)4 Duration (io.airlift.units.Duration)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)2 ServiceDescriptor (com.facebook.airlift.discovery.client.ServiceDescriptor)1 ServiceDescriptor.serviceDescriptor (com.facebook.airlift.discovery.client.ServiceDescriptor.serviceDescriptor)1 ServiceSelector (com.facebook.airlift.discovery.client.ServiceSelector)1 DEFAULT_POOL (com.facebook.airlift.discovery.client.ServiceSelectorConfig.DEFAULT_POOL)1 HttpClient (com.facebook.airlift.http.client.HttpClient)1 HttpStatus (com.facebook.airlift.http.client.HttpStatus)1 OK (com.facebook.airlift.http.client.HttpStatus.OK)1 NodeConfig (com.facebook.airlift.node.NodeConfig)1 NodeInfo (com.facebook.airlift.node.NodeInfo)1 Assertions.assertEqualsIgnoreOrder (com.facebook.airlift.testing.Assertions.assertEqualsIgnoreOrder)1 NodeVersion (com.facebook.presto.client.NodeVersion)1 Page (com.facebook.presto.common.Page)1