Search in sources :

Example 1 with TimedBytesWriter

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

the class TestStreamRequest method testBackPressure.

@Test
public void testBackPressure() throws Exception {
    final long totalBytes = SMALL_BYTES_NUM;
    TimedBytesWriter writer = new TimedBytesWriter(totalBytes, BYTE);
    EntityStream entityStream = EntityStreams.newEntityStream(writer);
    StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
    StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
    _client.streamRequest(request, callback);
    latch.await(60000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    TimedBytesReader reader = _rateLimitedRequestHandler.getReader();
    Assert.assertNotNull(reader);
    Assert.assertEquals(totalBytes, reader.getTotalBytes());
    Assert.assertTrue(reader.allBytesCorrect());
    long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
    long serverReceiveTimespan = reader.getStopTime() - reader.getStartTime();
    Assert.assertTrue(serverReceiveTimespan > 1000);
    double diff = Math.abs(serverReceiveTimespan - clientSendTimespan);
    double diffRatio = diff / clientSendTimespan;
    // make it generous to reduce the chance occasional test failures
    Assert.assertTrue(diffRatio < 0.2);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimedBytesWriter(test.r2.integ.helper.TimedBytesWriter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) TimedBytesReader(test.r2.integ.helper.TimedBytesReader) 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)

Example 2 with TimedBytesWriter

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

the class TestStreamEcho method testBackPressureEcho.

@Test(groups = { "ci-flaky" })
public void testBackPressureEcho() throws Exception {
    TimedBytesWriter writer = new TimedBytesWriter(SMALL_BYTES_NUM, BYTE);
    StreamRequest request = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, ECHO_URI)).build(EntityStreams.newEntityStream(writer));
    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 TimedBytesReader reader = new TimedBytesReader(BYTE, readerCallback) {

        int count = 0;

        @Override
        protected void requestMore(final ReadHandle rh) {
            count++;
            if (count % 16 == 0) {
                _scheduler.schedule(new Runnable() {

                    @Override
                    public void run() {
                        rh.request(1);
                    }
                }, INTERVAL, TimeUnit.MILLISECONDS);
            } else {
                rh.request(1);
            }
        }
    };
    Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
    _client.streamRequest(request, callback);
    latch.await(60000, TimeUnit.MILLISECONDS);
    Assert.assertNull(error.get());
    Assert.assertEquals(status.get(), RestStatus.OK);
    Assert.assertEquals(reader.getTotalBytes(), SMALL_BYTES_NUM);
    Assert.assertTrue(reader.allBytesCorrect());
    long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
    long clientReceiveTimespan = reader.getStopTime() - reader.getStartTime();
    double diff = Math.abs(clientReceiveTimespan - clientSendTimespan);
    double diffRatio = diff / clientSendTimespan;
    // make it generous to reduce the chance occasional test failures
    Assert.assertTrue(diffRatio < 0.5, "Send/receive time delta is " + diff + " but expected to be less than 0.5. Send time span is " + clientSendTimespan + " and receive time span is " + clientReceiveTimespan);
}
Also used : TimedBytesWriter(test.r2.integ.helper.TimedBytesWriter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimedBytesReader(test.r2.integ.helper.TimedBytesReader) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Aggregations

StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)2 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.testng.annotations.Test)2 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)2 TimedBytesReader (test.r2.integ.helper.TimedBytesReader)2 TimedBytesWriter (test.r2.integ.helper.TimedBytesWriter)2 None (com.linkedin.common.util.None)1 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)1 ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1