Search in sources :

Example 1 with TestingResponse

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

the class TestHttpPageBufferClient method testCloseDuringPendingRequest.

@Test
public void testCloseDuringPendingRequest() throws Exception {
    CyclicBarrier beforeRequest = new CyclicBarrier(2);
    CyclicBarrier afterRequest = new CyclicBarrier(2);
    StaticRequestProcessor processor = new StaticRequestProcessor(beforeRequest, afterRequest);
    processor.setResponse(new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]));
    CyclicBarrier requestComplete = new CyclicBarrier(2);
    TestingClientCallback callback = new TestingClientCallback(requestComplete);
    URI location = URI.create("http://localhost:8080");
    HttpPageBufferClient client = new HttpPageBufferClient(new TestingHttpClient(processor, executor), new DataSize(10, Unit.MEGABYTE), new Duration(1, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES), location, callback, executor);
    assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");
    // send request
    client.scheduleRequest();
    beforeRequest.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "running", 0, 1, 0, 0, "PROCESSING_REQUEST");
    assertEquals(client.isRunning(), true);
    // request is pending, now close it
    client.close();
    try {
        requestComplete.await(10, TimeUnit.SECONDS);
    } catch (BrokenBarrierException ignored) {
    }
    try {
        afterRequest.await(10, TimeUnit.SECONDS);
    } catch (BrokenBarrierException ignored) {
        afterRequest.reset();
    }
    // client.close() triggers a DELETE request, so wait for it to finish
    beforeRequest.await(10, TimeUnit.SECONDS);
    afterRequest.await(10, TimeUnit.SECONDS);
    requestComplete.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "closed", 0, 1, 2, 1, "not scheduled");
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) DataSize(io.airlift.units.DataSize) Duration(io.airlift.units.Duration) URI(java.net.URI) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 2 with TestingResponse

use of io.airlift.http.client.testing.TestingResponse in project airlift by airlift.

the class JaxrsTestingHttpProcessor method handle.

@Override
public Response handle(Request request) throws Exception {
    // prepare request to jax-rs resource
    MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
    for (Map.Entry<String, String> entry : request.getHeaders().entries()) {
        requestHeaders.add(entry.getKey(), entry.getValue());
    }
    Invocation.Builder invocationBuilder = client.target(request.getUri()).request().headers(requestHeaders);
    Invocation invocation;
    if (request.getBodyGenerator() == null) {
        invocation = invocationBuilder.build(request.getMethod());
    } else {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        request.getBodyGenerator().write(byteArrayOutputStream);
        byteArrayOutputStream.close();
        byte[] bytes = byteArrayOutputStream.toByteArray();
        Entity<byte[]> entity = Entity.entity(bytes, (String) requestHeaders.get("Content-Type").stream().collect(onlyElement()));
        invocation = invocationBuilder.build(request.getMethod(), entity);
    }
    // issue request, and handle exceptions
    javax.ws.rs.core.Response result;
    try {
        result = invocation.invoke(javax.ws.rs.core.Response.class);
    } catch (ProcessingException exception) {
        if (trace) {
            log.warn(exception.getCause(), "%-8s %s -> Exception", request.getMethod(), request.getUri());
        }
        // to facilitate testing exceptional paths
        if (exception.getCause() instanceof Exception) {
            throw (Exception) exception.getCause();
        }
        throw exception;
    } catch (Throwable throwable) {
        if (trace) {
            log.warn(throwable, "%-8s %s -> Fail", request.getMethod(), request.getUri());
        }
        throw throwable;
    }
    // process response from jax-rs resource
    ImmutableListMultimap.Builder<String, String> responseHeaders = ImmutableListMultimap.builder();
    for (Map.Entry<String, List<String>> headerEntry : result.getStringHeaders().entrySet()) {
        for (String value : headerEntry.getValue()) {
            responseHeaders.put(headerEntry.getKey(), value);
        }
    }
    if (trace) {
        log.warn("%-8s %s -> OK", request.getMethod(), request.getUri());
    }
    return new TestingResponse(HttpStatus.fromStatusCode(result.getStatus()), responseHeaders.build(), result.readEntity(byte[].class));
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) Invocation(javax.ws.rs.client.Invocation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) List(java.util.List) ProcessingException(javax.ws.rs.ProcessingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessingException(javax.ws.rs.ProcessingException) TestingResponse(io.airlift.http.client.testing.TestingResponse) Response(io.airlift.http.client.Response) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 3 with TestingResponse

use of io.airlift.http.client.testing.TestingResponse in project airlift by airlift.

the class TestFullJsonResponseHandler method testMissingContentType.

@Test
public void testMissingContentType() {
    JsonResponse<User> response = handler.handle(null, new TestingResponse(OK, ImmutableListMultimap.<String, String>of(), "hello".getBytes(UTF_8)));
    assertFalse(response.hasValue());
    assertNull(response.getException());
    assertNull(response.getJson());
    assertNull(response.getJsonBytes());
    assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8));
    assertEquals(response.getResponseBody(), "hello");
    assertTrue(response.getHeaders().isEmpty());
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) Test(org.testng.annotations.Test)

Example 4 with TestingResponse

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

the class TestHttpPageBufferClient method testInvalidResponses.

@Test
public void testInvalidResponses() throws Exception {
    CyclicBarrier beforeRequest = new CyclicBarrier(1);
    CyclicBarrier afterRequest = new CyclicBarrier(1);
    StaticRequestProcessor processor = new StaticRequestProcessor(beforeRequest, afterRequest);
    CyclicBarrier requestComplete = new CyclicBarrier(2);
    TestingClientCallback callback = new TestingClientCallback(requestComplete);
    URI location = URI.create("http://localhost:8080");
    HttpPageBufferClient client = new HttpPageBufferClient(new TestingHttpClient(processor, executor), new DataSize(10, Unit.MEGABYTE), new Duration(1, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES), location, callback, executor);
    assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");
    // send not found response and verify response was ignored
    processor.setResponse(new TestingResponse(HttpStatus.NOT_FOUND, ImmutableListMultimap.of(CONTENT_TYPE, PRESTO_PAGES), new byte[0]));
    client.scheduleRequest();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertEquals(callback.getPages().size(), 0);
    assertEquals(callback.getCompletedRequests(), 1);
    assertEquals(callback.getFinishedBuffers(), 0);
    assertEquals(callback.getFailedBuffers(), 1);
    assertInstanceOf(callback.getFailure(), PageTransportErrorException.class);
    assertContains(callback.getFailure().getMessage(), "Expected response code to be 200, but was 404 Not Found");
    assertStatus(client, location, "queued", 0, 1, 1, 1, "not scheduled");
    // send invalid content type response and verify response was ignored
    callback.resetStats();
    processor.setResponse(new TestingResponse(HttpStatus.OK, ImmutableListMultimap.of(CONTENT_TYPE, "INVALID_TYPE"), new byte[0]));
    client.scheduleRequest();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertEquals(callback.getPages().size(), 0);
    assertEquals(callback.getCompletedRequests(), 1);
    assertEquals(callback.getFinishedBuffers(), 0);
    assertEquals(callback.getFailedBuffers(), 1);
    assertInstanceOf(callback.getFailure(), PageTransportErrorException.class);
    assertContains(callback.getFailure().getMessage(), "Expected application/x-presto-pages response from server but got INVALID_TYPE");
    assertStatus(client, location, "queued", 0, 2, 2, 2, "not scheduled");
    // send unexpected content type response and verify response was ignored
    callback.resetStats();
    processor.setResponse(new TestingResponse(HttpStatus.OK, ImmutableListMultimap.of(CONTENT_TYPE, "text/plain"), new byte[0]));
    client.scheduleRequest();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertEquals(callback.getPages().size(), 0);
    assertEquals(callback.getCompletedRequests(), 1);
    assertEquals(callback.getFinishedBuffers(), 0);
    assertEquals(callback.getFailedBuffers(), 1);
    assertInstanceOf(callback.getFailure(), PageTransportErrorException.class);
    assertContains(callback.getFailure().getMessage(), "Expected application/x-presto-pages response from server but got text/plain");
    assertStatus(client, location, "queued", 0, 3, 3, 3, "not scheduled");
    // close client and verify
    client.close();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "closed", 0, 3, 4, 3, "not scheduled");
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) DataSize(io.airlift.units.DataSize) Duration(io.airlift.units.Duration) URI(java.net.URI) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 5 with TestingResponse

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

the class TestHttpPageBufferClient method testLifecycle.

@Test
public void testLifecycle() throws Exception {
    CyclicBarrier beforeRequest = new CyclicBarrier(2);
    CyclicBarrier afterRequest = new CyclicBarrier(2);
    StaticRequestProcessor processor = new StaticRequestProcessor(beforeRequest, afterRequest);
    processor.setResponse(new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]));
    CyclicBarrier requestComplete = new CyclicBarrier(2);
    TestingClientCallback callback = new TestingClientCallback(requestComplete);
    URI location = URI.create("http://localhost:8080");
    HttpPageBufferClient client = new HttpPageBufferClient(new TestingHttpClient(processor, executor), new DataSize(10, Unit.MEGABYTE), new Duration(1, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES), location, callback, executor);
    assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");
    client.scheduleRequest();
    beforeRequest.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "running", 0, 1, 0, 0, "PROCESSING_REQUEST");
    assertEquals(client.isRunning(), true);
    afterRequest.await(10, TimeUnit.SECONDS);
    requestComplete.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "queued", 0, 1, 1, 1, "not scheduled");
    client.close();
    beforeRequest.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "closed", 0, 1, 1, 1, "PROCESSING_REQUEST");
    afterRequest.await(10, TimeUnit.SECONDS);
    requestComplete.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "closed", 0, 1, 2, 1, "not scheduled");
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) DataSize(io.airlift.units.DataSize) Duration(io.airlift.units.Duration) URI(java.net.URI) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Aggregations

TestingResponse (io.airlift.http.client.testing.TestingResponse)5 Test (org.testng.annotations.Test)4 TestingHttpClient (io.airlift.http.client.testing.TestingHttpClient)3 DataSize (io.airlift.units.DataSize)3 Duration (io.airlift.units.Duration)3 URI (java.net.URI)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 Response (io.airlift.http.client.Response)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 List (java.util.List)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 ProcessingException (javax.ws.rs.ProcessingException)1 Invocation (javax.ws.rs.client.Invocation)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1