Search in sources :

Example 6 with ByteStringWriter

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

the class TestBackupRequestsClient method testStreamRequestWithIsFullRequest.

@Test(invocationCount = 3, dataProvider = "isD2Async")
public void testStreamRequestWithIsFullRequest(boolean isD2Async) throws Exception {
    // 5s till response comes back
    int responseDelayNano = 500000000;
    // make backup request after 1 second
    int backupDelayNano = 100000000;
    Deque<URI> hostsReceivingRequest = new ConcurrentLinkedDeque<>();
    BackupRequestsClient client = createAlwaysBackupClientWithHosts(Arrays.asList("http://test1.com:123", "http://test2.com:123"), hostsReceivingRequest, responseDelayNano, backupDelayNano, isD2Async);
    URI uri = URI.create("d2://testService");
    // if there is IS_FULL_REQUEST set, backup requests will happen
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.OPERATION, "get");
    context.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
    RequestContext context1 = context.clone();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<AssertionError> failure = new AtomicReference<>();
    client.streamRequest(streamRequest, context1, new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            failure.set(new AssertionError("Callback onError"));
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            try {
                assertEquals(result.getStatus(), 200);
                assertEquals(result.getHeader("buffered"), "true");
                assertEquals(hostsReceivingRequest.size(), 2);
                assertEquals(new HashSet<>(hostsReceivingRequest).size(), 2);
                hostsReceivingRequest.clear();
            } catch (AssertionError e) {
                failure.set(e);
            }
            latch.countDown();
        }
    });
    latch.await(6, TimeUnit.SECONDS);
    if (failure.get() != null) {
        throw failure.get();
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with ByteStringWriter

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

the class TestMessages method testToStreamTransportCallbackSuccess.

@Test
public void testToStreamTransportCallbackSuccess() {
    TransportCallback<RestResponse> restCallback = response -> {
        Assert.assertFalse(response.hasError());
        Assert.assertNotNull(response.getResponse());
        Assert.assertSame(response.getResponse().getEntity(), DATA);
        Assert.assertNotNull(response.getWireAttributes());
        Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
    };
    TransportCallback<StreamResponse> streamCallback = Messages.toStreamTransportCallback(restCallback);
    StreamResponseBuilder builder = new StreamResponseBuilder();
    StreamResponse streamResponse = builder.build(EntityStreams.newEntityStream(new ByteStringWriter(DATA)));
    streamCallback.onResponse(TransportResponseImpl.success(streamResponse, WIRE_ATTR));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 8 with ByteStringWriter

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

the class TestNettyRequestAdapter method testStreamToHttp2HeadersCookies.

@Test
public void testStreamToHttp2HeadersCookies() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    IntStream.range(0, 10).forEach(i -> streamRequestBuilder.addCookie(ANY_COOKIE));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    List<CharSequence> cookies = headers.getAll(HttpHeaderNames.COOKIE);
    Assert.assertNotNull(cookies);
    Assert.assertEquals(cookies.size(), 10);
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 9 with ByteStringWriter

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

the class TestNettyRequestAdapter method testStreamToNettyRequestWithMultipleCookies.

@Test
public void testStreamToNettyRequestWithMultipleCookies() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    streamRequestBuilder.setCookies(ANY_COOKIES);
    StreamRequest streamRequest = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(streamRequest);
    Assert.assertEquals(nettyRequest.headers().get("Cookie"), ENCODED_COOKIES_HEADER_VALUE);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 10 with ByteStringWriter

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

the class TestNettyRequestAdapter method testStreamToHttp2HeadersBlacklist.

@Test
public void testStreamToHttp2HeadersBlacklist() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    HEADER_BLACKLIST.forEach(header -> streamRequestBuilder.addHeaderValue(header, ANY_HEADER));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    HEADER_BLACKLIST.forEach(header -> Assert.assertFalse(headers.contains(header), header));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Aggregations

ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)38 Test (org.testng.annotations.Test)34 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)32 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)31 URI (java.net.URI)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)17 RequestContext (com.linkedin.r2.message.RequestContext)15 ByteString (com.linkedin.data.ByteString)10 Callback (com.linkedin.common.callback.Callback)8 Http2Headers (io.netty.handler.codec.http2.Http2Headers)8 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)7 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Server (org.eclipse.jetty.server.Server)6 AfterTest (org.testng.annotations.AfterTest)6 BeforeTest (org.testng.annotations.BeforeTest)6 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5