Search in sources :

Example 76 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestMIMEIntegrationReader method executeRequestAndAssert.

// /////////////////////////////////////////////////////////////////////////////////////
private void executeRequestAndAssert(final ByteString requestPayload, final int chunkSize, final MimeMultipart mimeMultipart) throws Exception {
    final VariableByteStringWriter variableByteStringWriter = new VariableByteStringWriter(requestPayload, chunkSize);
    final EntityStream entityStream = EntityStreams.newEntityStream(variableByteStringWriter);
    final StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, SERVER_URI));
    // We add additional parameters since MIME supports this and we want to make sure we can still extract boundary
    // properly.
    final String contentTypeHeader = mimeMultipart.getContentType() + ";somecustomparameter=somecustomvalue" + ";anothercustomparameter=anothercustomvalue";
    StreamRequest request = builder.setMethod("POST").setHeader(HEADER_CONTENT_TYPE, contentTypeHeader).build(entityStream);
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    Callback<StreamResponse> callback = expectSuccessCallback(latch, status, new HashMap<>());
    _client.streamRequest(request, callback);
    latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    List<SinglePartMIMEReaderCallbackImpl> singlePartMIMEReaderCallbacks = _mimeServerRequestHandler._testMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks;
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), mimeMultipart.getCount());
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        // Actual
        final SinglePartMIMEReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        // Expected
        final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
        // Construct expected headers and verify they match
        final Map<String, String> expectedHeaders = new HashMap<>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback._headers, expectedHeaders);
        // Verify the body matches
        if (currentExpectedPart.getContent() instanceof byte[]) {
            Assert.assertEquals(currentCallback._finishedData.copyBytes(), currentExpectedPart.getContent());
        } else {
            // Default is String
            Assert.assertEquals(new String(currentCallback._finishedData.copyBytes()), currentExpectedPart.getContent());
        }
    }
}
Also used : VariableByteStringWriter(com.linkedin.multipart.utils.VariableByteStringWriter) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) Header(javax.mail.Header) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 77 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestHttp2NettyStreamClient method testRequestTimeout.

/**
 * Tests the condition that when a client request times out before the request is processed
 * by the server, the servlet implementation throws when attempting to read the request entity.
 */
@Test(enabled = false)
public void testRequestTimeout() throws Exception {
    final AtomicInteger serverIOExceptions = new AtomicInteger(0);
    final CountDownLatch exceptionLatch = new CountDownLatch(1);
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final CountDownLatch serverLatch = new CountDownLatch(1);
    final HttpServerBuilder serverBuilder = new HttpServerBuilder();
    final Server server = serverBuilder.exceptionListener(throwable -> {
        if (throwable instanceof IOException) {
            serverIOExceptions.incrementAndGet();
            exceptionLatch.countDown();
        }
    }).responseLatch(serverLatch).build();
    final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
    final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(500).buildHttp2StreamClient();
    try {
        server.start();
        // Sends the stream request
        final StreamRequestBuilder builder = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request = builder.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
        client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> responseLatch.countDown());
        // Waits for request to timeout
        Thread.sleep(1000);
        // Allows server to process request
        serverLatch.countDown();
    } finally {
        if (!responseLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
            Assert.fail("Timeout waiting for response latch");
        }
        if (!exceptionLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
            Assert.fail("Timeout waiting for exception latch");
        }
        server.stop();
    }
    // Expects two IOExceptions thrown by the server. One for the initial OPTIONS upgrade request and one for
    // the actual GET request.
    Assert.assertEquals(serverIOExceptions.get(), 2);
}
Also used : Server(org.eclipse.jetty.server.Server) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) IOException(java.io.IOException) HttpClientBuilder(com.linkedin.r2.transport.http.client.HttpClientBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 78 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class StreamRequestGenerator method nextMessage.

@Override
public StreamRequest nextMessage() {
    if (_msgCounter.getAndDecrement() > 0) {
        StreamRequestBuilder builder = new StreamRequestBuilder(_uri);
        builder.setMethod(HTTP_POST_METHOD);
        for (int i = 0; i < _numHeaders; i++) {
            builder.setHeader(STATIC_HEADER_PREFIX + i, _headerContent);
        }
        return builder.build(EntityStreams.newEntityStream(new PerfStreamWriter(_msgSize)));
    } else {
        return null;
    }
}
Also used : StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) PerfStreamWriter(test.r2.perf.PerfStreamWriter)

Example 79 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestUnstructuredDataClient method testClientWithStreamResponse.

@Test(dataProvider = "goodURLs")
public void testClientWithStreamResponse(String resourceURL) throws Throwable {
    ByteString expectedContent = ByteString.copy(GreetingUnstructuredDataUtils.UNSTRUCTURED_DATA_BYTES);
    URI testURI = URI.create(RestLiIntegrationTest.URI_PREFIX + resourceURL);
    EntityStream emptyStream = EntityStreams.emptyStream();
    StreamRequest streamRequest = new StreamRequestBuilder(testURI).build(emptyStream);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean();
    _client.streamRequest(streamRequest, new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            fail("failed to get response", e);
        }

        @Override
        public void onSuccess(StreamResponse result) {
            assertEquals(result.getHeader(RestConstants.HEADER_CONTENT_TYPE), GreetingUnstructuredDataUtils.MIME_TYPE);
            FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

                @Override
                public void onError(Throwable e) {
                    success.set(false);
                    latch.countDown();
                }

                @Override
                public void onSuccess(ByteString result) {
                    // Won't fail the test, only use to print out error
                    assertEquals(result, expectedContent);
                    // Will fail the test if content is not identical
                    success.set(result.equals(expectedContent));
                    latch.countDown();
                }
            });
            result.getEntityStream().setReader(fullEntityReader);
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    if (!success.get())
        fail("Failed to read response data from stream!");
}
Also used : ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) Test(org.testng.annotations.Test)

Example 80 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestRestLiServer method testValidUnstructuredDataRequest.

@Test(dataProvider = "validClientProtocolVersionDataStreamOnly")
public void testValidUnstructuredDataRequest(RestLiServer server, ProtocolVersion clientProtocolVersion, String headerConstant) throws URISyntaxException, IOException {
    StreamRequest streamRequest = new StreamRequestBuilder(new URI("/feedDownloads/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
    final FeedDownloadResource resource = getMockResource(FeedDownloadResource.class);
    resource.get(eq(1L), anyObject(UnstructuredDataWriter.class));
    EasyMock.expectLastCall().andDelegateTo(new FeedDownloadResource()).once();
    replay(resource);
    @SuppressWarnings("unchecked") Callback<StreamResponse> r2Callback = createMock(Callback.class);
    final Capture<StreamResponse> streamResponse = EasyMock.newCapture();
    r2Callback.onSuccess(capture(streamResponse));
    expectLastCall().once();
    replay(r2Callback);
    RequestContext requestContext = new RequestContext();
    server.handleRequest(streamRequest, requestContext, r2Callback);
    verify(resource);
    verify(r2Callback);
    assertNotNull(streamResponse);
    assertEquals(streamResponse.getValue().getHeader(RestConstants.HEADER_CONTENT_TYPE), FeedDownloadResource.CONTENT_TYPE);
    FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            fail("Error inside callback!! Failed to read response data from stream!", e);
        }

        @Override
        public void onSuccess(ByteString result) {
            assertEquals(result.copyBytes(), FeedDownloadResource.CONTENT);
        }
    });
    streamResponse.getValue().getEntityStream().setReader(fullEntityReader);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) FeedDownloadResource(com.linkedin.restli.server.twitter.FeedDownloadResource) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)121 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)106 Test (org.testng.annotations.Test)102 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)87 URI (java.net.URI)68 RequestContext (com.linkedin.r2.message.RequestContext)56 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Callback (com.linkedin.common.callback.Callback)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ByteString (com.linkedin.data.ByteString)22 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)21 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)21 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)20 AfterTest (org.testng.annotations.AfterTest)19 BeforeTest (org.testng.annotations.BeforeTest)19 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 FutureCallback (com.linkedin.common.callback.FutureCallback)17