Search in sources :

Example 6 with BytesReader

use of test.r2.integ.helper.BytesReader in project rest.li by linkedin.

the class TestStreamResponse method testErrorWhileStreaming.

@Test
public void testErrorWhileStreaming() throws Exception {
    HttpClientFactory clientFactory = new HttpClientFactory.Builder().build();
    Map<String, Object> clientProperties = new HashMap<>();
    clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, "1000");
    Client client = _clientProvider.createClient(FilterChains.empty(), clientProperties);
    StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, SERVER_ERROR_URI));
    StreamRequest request = builder.build(EntityStreams.emptyStream());
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final Callback<None> readerCallback = getReaderCallback(latch, error);
    final BytesReader reader = new BytesReader(BYTE, readerCallback);
    Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
    client.streamRequest(request, callback);
    latch.await(2000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    Throwable throwable = error.get();
    System.out.println(_clientProvider.createHttpURI(_port, SERVER_ERROR_URI));
    System.out.println(_serverProvider.getClass());
    Assert.assertNotNull(throwable);
    final FutureCallback<None> clientShutdownCallback = new FutureCallback<>();
    client.shutdown(clientShutdownCallback);
    clientShutdownCallback.get();
    final FutureCallback<None> factoryShutdownCallback = new FutureCallback<>();
    clientFactory.shutdown(factoryShutdownCallback);
    factoryShutdownCallback.get();
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) BytesReader(test.r2.integ.helper.BytesReader) TimedBytesReader(test.r2.integ.helper.TimedBytesReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 7 with BytesReader

use of test.r2.integ.helper.BytesReader in project rest.li by linkedin.

the class TestStreamingTimeout method testStreamSuccessWithoutStreamingTimeout.

@Test
public void testStreamSuccessWithoutStreamingTimeout() throws Exception {
    final long totalBytes = TINY_BYTES_NUM;
    final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
    final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, NON_RATE_LIMITED_URI));
    final StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    final Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
    _client.streamRequest(request, callback);
    latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    final BytesReader reader = _requestHandler.getReader();
    Assert.assertNotNull(reader);
    Assert.assertEquals(totalBytes, reader.getTotalBytes());
    Assert.assertTrue(reader.allBytesCorrect());
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) BytesWriter(test.r2.integ.helper.BytesWriter) BytesReader(test.r2.integ.helper.BytesReader) TimedBytesReader(test.r2.integ.helper.TimedBytesReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Aggregations

StreamRequest (com.linkedin.r2.message.stream.StreamRequest)7 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)7 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)7 BytesReader (test.r2.integ.helper.BytesReader)7 None (com.linkedin.common.util.None)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TimedBytesReader (test.r2.integ.helper.TimedBytesReader)5 Test (org.testng.annotations.Test)4 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)4 BytesWriter (test.r2.integ.helper.BytesWriter)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 TimedBytesWriter (test.r2.integ.helper.TimedBytesWriter)2 RequestContext (com.linkedin.r2.message.RequestContext)1 Client (com.linkedin.r2.transport.common.Client)1 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)1 HashMap (java.util.HashMap)1