Java Code Examples for com.linecorp.armeria.common.HttpResponse#subscribe()

The following examples show how to use com.linecorp.armeria.common.HttpResponse#subscribe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcurrencyLimitingClientTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the case where a delegate raises an exception rather than returning a response.
 */
@Test
void testFaultyDelegate() throws Exception {
    final ClientRequestContext ctx = newContext();
    final HttpRequest req = newReq();

    when(delegate.execute(ctx, req)).thenThrow(Exception.class);

    final ConcurrencyLimitingClient client = newDecorator(1).apply(delegate);
    assertThat(client.numActiveRequests()).isZero();

    final HttpResponse res = client.execute(ctx, req);

    // Consume everything from the returned response so its close future is completed.
    res.subscribe(NoopSubscriber.get());

    assertThat(res.isOpen()).isFalse();
    assertThatThrownBy(() -> res.whenComplete().get()).hasCauseInstanceOf(Exception.class);
    await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
 
Example 2
Source File: PooledResponseBufferBenchmark.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    final HttpResponse res = unwrap().serve(ctx, req);
    final HttpResponseWriter decorated = HttpResponse.streaming();
    res.subscribe(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            decorated.write(httpObject);
        }

        @Override
        public void onError(Throwable t) {
            decorated.close(t);
        }

        @Override
        public void onComplete() {
            decorated.close();
        }
    });
    return decorated;
}
 
Example 3
Source File: ConcurrencyLimitingClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the request is not delegated but closed when the timeout is reached before delegation.
 */
@Test
void testTimeout() throws Exception {
    final ClientRequestContext ctx1 = newContext();
    final ClientRequestContext ctx2 = newContext();
    final HttpRequest req1 = newReq();
    final HttpRequest req2 = newReq();
    final HttpResponseWriter actualRes1 = HttpResponse.streaming();
    final HttpResponseWriter actualRes2 = HttpResponse.streaming();

    when(delegate.execute(ctx1, req1)).thenReturn(actualRes1);

    final ConcurrencyLimitingClient client =
            newDecorator(1, 500, TimeUnit.MILLISECONDS).apply(delegate);

    // Send two requests, where only the first one is delegated.
    final HttpResponse res1 = client.execute(ctx1, req1);
    final HttpResponse res2 = client.execute(ctx2, req2);

    // Let req2 time out.
    Thread.sleep(1000);
    res2.subscribe(NoopSubscriber.get());
    assertThatThrownBy(() -> res2.whenComplete().join())
            .hasCauseInstanceOf(UnprocessedRequestException.class)
            .hasRootCauseInstanceOf(RequestTimeoutException.class);
    assertThat(res2.isOpen()).isFalse();

    // req1 should not time out because it's been delegated already.
    res1.subscribe(NoopSubscriber.get());
    assertThat(res1.isOpen()).isTrue();
    assertThat(res1.whenComplete()).isNotDone();

    // Close req1 and make sure req2 does not affect numActiveRequests.
    actualRes1.close();
    await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
 
Example 4
Source File: ConcurrencyLimitingClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the response returned by the delegate and consumes everything from it, so that its close future
 * is completed.
 */
private static void closeAndDrain(HttpResponseWriter actualRes, HttpResponse deferredRes) {
    actualRes.close();
    deferredRes.subscribe(NoopSubscriber.get());
    deferredRes.whenComplete().join();
    waitForEventLoop();
}
 
Example 5
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    final HttpResponse res = unwrap().serve(ctx, req);
    final HttpResponseWriter decorated = HttpResponse.streaming();
    res.subscribe(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            decorated.write(httpObject);
        }

        @Override
        public void onError(Throwable t) {
            decorated.close(t);
        }

        @Override
        public void onComplete() {
            decorated.close();
        }
    });
    return decorated;
}
 
Example 6
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void testCloseClientFactory() throws Exception {
    final ClientFactory factory = ClientFactory.builder().build();
    final WebClient client = WebClient.builder(server.httpUri()).factory(factory).build();
    final HttpRequestWriter req = HttpRequest.streaming(RequestHeaders.of(HttpMethod.GET,
                                                                          "/stream-closed"));
    final HttpResponse res = client.execute(req);
    final AtomicReference<HttpObject> obj = new AtomicReference<>();
    res.subscribe(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            obj.set(httpObject);
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    req.write(HttpData.ofUtf8("not finishing this stream, sorry."));
    await().untilAsserted(() -> assertThat(obj).hasValue(ResponseHeaders.of(HttpStatus.OK)));
    factory.close();
    await().until(() -> completed);
}
 
Example 7
Source File: HttpServerStreamingTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void runStreamingResponseTest(WebClient client, boolean slowClient)
        throws InterruptedException, ExecutionException {
    final HttpResponse res = client.get("/zeroes/" + STREAMING_CONTENT_LENGTH);
    final AtomicReference<HttpStatus> status = new AtomicReference<>();

    final StreamConsumer consumer = new StreamConsumer(GlobalEventExecutor.INSTANCE, slowClient) {

        @Override
        public void onNext(HttpObject obj) {
            if (obj instanceof ResponseHeaders) {
                status.compareAndSet(null, ((ResponseHeaders) obj).status());
            }
            super.onNext(obj);
        }

        @Override
        public void onError(Throwable cause) {
            // Will be notified via the 'awaitClose().get()' below.
        }

        @Override
        public void onComplete() {}
    };

    res.subscribe(consumer);

    res.whenComplete().get();
    assertThat(status.get()).isEqualTo(HttpStatus.OK);
    assertThat(consumer.numReceivedBytes()).isEqualTo(STREAMING_CONTENT_LENGTH);
}
 
Example 8
Source File: ArmeriaHttpClientResponseSubscriber.java    From armeria with Apache License 2.0 4 votes vote down vote up
ArmeriaHttpClientResponseSubscriber(HttpResponse httpResponse) {
    completionFuture = httpResponse.whenComplete();
    httpResponse.subscribe(this, eventLoop, SubscriptionOption.NOTIFY_CANCELLATION);
}