org.apache.http.nio.util.SimpleInputBuffer Java Examples

The following examples show how to use org.apache.http.nio.util.SimpleInputBuffer. 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: PoolingAsyncResponseConsumerTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void releaseResourcesNullifiesTheResponse() throws IOException {

    // given
    GenericItemSourcePool<SimpleInputBuffer> itemSourcePool = createDefaultTestGenericItemSourcePool(
            GenericItemSourcePoolTest.DEFAULT_TEST_INITIAL_POOL_SIZE,
            false
    );

    PoolingAsyncResponseConsumer consumer = createDefaultTestObject(itemSourcePool);
    consumer.onResponseReceived(mock(HttpResponse.class));

    HttpEntity httpEntity = mock(HttpEntity.class);
    consumer.onEntityEnclosed(httpEntity, ContentType.create("application/json"));

    HttpResponse before = consumer.buildResult(null);
    assertNotNull(before);

    // when
    consumer.releaseResources();

    // then
    HttpResponse response = consumer.buildResult(null);
    assertNull(response);

}
 
Example #2
Source File: PoolingAsyncResponseConsumerTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void onEntityEnclosedPoolsTheBufferOnce() throws IOException, PoolResourceException {

    // given
    GenericItemSourcePool<SimpleInputBuffer> itemSourcePool = spy(createDefaultTestGenericItemSourcePool(
            GenericItemSourcePoolTest.DEFAULT_TEST_INITIAL_POOL_SIZE,
            false
    ));

    PoolingAsyncResponseConsumer consumer = spy(createDefaultTestObject(itemSourcePool));
    consumer.onResponseReceived(mock(HttpResponse.class));

    HttpEntity httpEntity = mock(HttpEntity.class);

    // when
    consumer.onEntityEnclosed(httpEntity, ContentType.create("application/json"));
    consumer.onEntityEnclosed(httpEntity, ContentType.create("application/json"));

    // then
    verify(consumer, times(1)).getPooled();

}
 
Example #3
Source File: PoolingAsyncResponseConsumerTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void onEntityEnclosedSetsResponseInputStream() throws IOException {

    // given
    GenericItemSourcePool<SimpleInputBuffer> itemSourcePool = createDefaultTestGenericItemSourcePool(
                    GenericItemSourcePoolTest.DEFAULT_TEST_INITIAL_POOL_SIZE,
                    false
            );

    PoolingAsyncResponseConsumer consumer = createDefaultTestObject(itemSourcePool);
    consumer.onResponseReceived(mock(HttpResponse.class));

    HttpEntity httpEntity = mock(HttpEntity.class);

    // when
    consumer.onEntityEnclosed(httpEntity, ContentType.create("application/json"));

    // then
    HttpResponse response = consumer.buildResult(null);
    verify(response, times(1)).setEntity(any());

}
 
Example #4
Source File: SimpleInputBufferObjectOpsTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
public static GenericItemSourcePool<SimpleInputBuffer> createDefaultTestGenericItemSourcePool(
        int initialSize,
        boolean monitored,
        ResizePolicy resizePolicy
) {

    SimpleInputBufferPooledObjectOps pooledObjectOps = new SimpleInputBufferPooledObjectOps(
            HeapByteBufferAllocator.INSTANCE,
            DEFAULT_TEST_ITEM_SIZE_IN_BYTES);

    return new GenericItemSourcePool<>(
            DEFAULT_TEST_ITEM_POOL_NAME,
            pooledObjectOps,
            resizePolicy,
            DEFAULT_TEST_RESIZE_TIMEOUT,
            monitored,
            DEFAULT_TEST_MONITOR_TASK_INTERVAL,
            initialSize
    );
}
 
Example #5
Source File: SimpleInputBufferObjectOpsTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void purgeHasNoSideEffects() {

    // given
    SimpleInputBufferPooledObjectOps pooledObjectOps = spy(createDefaultTestObject());
    SimpleInputBuffer inputBuffer = mock(SimpleInputBuffer.class);

    ItemSource<SimpleInputBuffer> itemSourceMock = mock(ItemSource.class);
    when(pooledObjectOps.createItemSource(any())).thenReturn(itemSourceMock);

    ItemSource<SimpleInputBuffer> itemSource = pooledObjectOps.createItemSource(null);
    when(itemSourceMock.getSource()).thenReturn(inputBuffer);

    // when
    pooledObjectOps.purge(itemSource);

    // then
    verifyZeroInteractions(itemSourceMock);
    verifyZeroInteractions(inputBuffer);

}
 
Example #6
Source File: SimpleInputBufferObjectOpsTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void resetDelegatesToSimpleInputBuffer() {

    // given
    SimpleInputBufferPooledObjectOps pooledObjectOps = spy(createDefaultTestObject());
    SimpleInputBuffer inputBuffer = mock(SimpleInputBuffer.class);

    ItemSource<SimpleInputBuffer> itemSourceMock = mock(ItemSource.class);
    when(pooledObjectOps.createItemSource(any())).thenReturn(itemSourceMock);

    ItemSource<SimpleInputBuffer> itemSource = pooledObjectOps.createItemSource(null);
    when(itemSourceMock.getSource()).thenReturn(inputBuffer);

    // when
    pooledObjectOps.reset(itemSource);

    // then
    Mockito.verify(inputBuffer, times(1)).reset();

}
 
Example #7
Source File: ItemSourceContentInputStreamTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void closeReleasesBufferOnlyOnce() throws IOException {

    // given
    ItemSource itemSource = mock(ItemSource.class);
    when(itemSource.getSource()).thenReturn(mock(SimpleInputBuffer.class));

    InputStream inputStream = new ItemSourceContentInputStream(itemSource);

    // when
    inputStream.close();
    inputStream.close();

    // then
    verify(itemSource, times(1)).release();

}
 
Example #8
Source File: SimpleInputBufferPooledObjectOpsTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void resetDelegatesToUnderlyingItem() {

    // given
    SimpleInputBufferPooledObjectOps ops = new SimpleInputBufferPooledObjectOps(
            HeapByteBufferAllocator.INSTANCE,
            TEST_BUFFER_SIZE
    );

    ItemSource<SimpleInputBuffer> result = spy(ops.createItemSource(source -> {}));
    SimpleInputBuffer simpleInputBuffer = mock(SimpleInputBuffer.class);
    when(result.getSource()).thenReturn(simpleInputBuffer);

    // when
    ops.reset(result);

    // then
    verify(simpleInputBuffer).reset();

}
 
Example #9
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
private GenericItemSourcePool<SimpleInputBuffer> createPool() {
    GenericItemSourcePool<SimpleInputBuffer> bufferPool = new GenericItemSourcePool<>(
            "hc-responseBufferPool",
            new SimpleInputBufferPooledObjectOps(
                    HeapByteBufferAllocator.INSTANCE,
                    pooledResponseBuffersSizeInBytes
            ),
            new UnlimitedResizePolicy.Builder().withResizeFactor(0.5).build(),
            1000L,
            false,
            30000,
            maxTotalConnections
    );
    return bufferPool;
}
 
Example #10
Source File: ResponseConsumer.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
@Override
protected void onEntityEnclosed(final HttpEntity entity,
        final ContentType contentType) throws IOException {
    long len = entity.getContentLength();
    if (len > Integer.MAX_VALUE) {
        throw new ContentTooLongException("Entity content is too long: "
                + len);
    }
    if (len < 0) {
        len = BUFFER_SIZE;
    }
    this.buf = new SimpleInputBuffer((int) len,
            new HeapByteBufferAllocator());
    this.httpResponse.setEntity(new ContentBufferEntity(entity, this.buf));
}
 
Example #11
Source File: InputBufferItemSourceTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void releaseDelegatesToReleaseCallback() {

    // given
    SimpleInputBuffer buffer = mock(SimpleInputBuffer.class);
    ReleaseCallback releaseCallback = mock(ReleaseCallback.class);
    ItemSource<SimpleInputBuffer> itemSource = new InputBufferItemSource(buffer, releaseCallback);

    // when
    itemSource.release();

    // then
    verify(releaseCallback, times(1)).completed(eq(itemSource));

}
 
Example #12
Source File: PoolingAsyncResponseConsumer.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
ItemSource<SimpleInputBuffer> getPooled() throws IOException {
    try {
        return itemSourcePool.getPooled();
    } catch (PoolResourceException e) {
        throw new IOException("Unable get pooled response buffer: " + e.getMessage());
    }
}
 
Example #13
Source File: AbstractResponseConsumer.java    From fc-java-sdk with MIT License 5 votes vote down vote up
protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException {
    long len = entity.getContentLength();
    if (len > 2147483647L) {
        throw new ContentTooLongException("Entity content is too long: " + len);
    } else {
        if (len < 0L) {
            len = 4096L;
        }

        this.buf = new SimpleInputBuffer((int)len, new HeapByteBufferAllocator());
        this.httpResponse.setEntity(new ContentBufferEntity(entity, this.buf));
    }
}
 
Example #14
Source File: InputBufferItemSource.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleInputBuffer getSource() {
    return source;
}
 
Example #15
Source File: InputBufferItemSource.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public InputBufferItemSource(SimpleInputBuffer source, ReleaseCallback<SimpleInputBuffer> releaseCallback) {
    this.source = source;
    this.releaseCallback = releaseCallback;
}
 
Example #16
Source File: SimpleInputBufferPooledObjectOps.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public boolean purge(ItemSource<SimpleInputBuffer> pooled) {
    return true;
}
 
Example #17
Source File: SimpleInputBufferPooledObjectOps.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void reset(ItemSource<SimpleInputBuffer> pooled) {
    pooled.getSource().reset();
}
 
Example #18
Source File: SimpleInputBufferPooledObjectOps.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ItemSource<SimpleInputBuffer> createItemSource(ReleaseCallback<SimpleInputBuffer> releaseCallback) {
    SimpleInputBuffer buffer = new SimpleInputBuffer(bufferSizeInBytes, byteBufAllocator);
    return new InputBufferItemSource(buffer, releaseCallback);
}
 
Example #19
Source File: SimpleInputBufferObjectOpsTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public static GenericItemSourcePool<SimpleInputBuffer> createDefaultTestGenericItemSourcePool(int initialSize, boolean monitored) {
    ResizePolicy resizePolicy = UnlimitedResizePolicy.newBuilder().build();
    return createDefaultTestGenericItemSourcePool(initialSize, monitored, resizePolicy);
}
 
Example #20
Source File: PoolingAsyncResponseConsumerFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public PoolingAsyncResponseConsumerFactory(GenericItemSourcePool<SimpleInputBuffer> pool) {
    this.pool = pool;
}
 
Example #21
Source File: PoolingAsyncResponseConsumer.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public PoolingAsyncResponseConsumer(ItemSourcePool<SimpleInputBuffer> bufferPool) {
    this.itemSourcePool = bufferPool;
}
 
Example #22
Source File: ItemSourceContentInputStream.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public ItemSourceContentInputStream(ItemSource<SimpleInputBuffer> buffer) {
    super(buffer.getSource());
    this.buffer = buffer;
}
 
Example #23
Source File: PoolingAsyncResponseConsumerTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private PoolingAsyncResponseConsumer createDefaultTestObject(ItemSourcePool<SimpleInputBuffer> itemSourcePool) {
    return new PoolingAsyncResponseConsumer(itemSourcePool);
}
 
Example #24
Source File: PoolingAsyncResponseConsumerTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private PoolingAsyncResponseConsumer createDefaultTestObject() {
    ItemSourcePool<SimpleInputBuffer> itemSourcePool = mock(ItemSourcePool.class);
    return createDefaultTestObject(itemSourcePool);
}
 
Example #25
Source File: InputBufferItemSourceTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void getSourceReturnsSameItemSourceInstance() {

    // given
    SimpleInputBuffer expected = mock(SimpleInputBuffer.class);

    // when
    ItemSource<SimpleInputBuffer> itemSource = new InputBufferItemSource(expected, null);

    // then
    Assert.assertTrue(expected == itemSource.getSource());

}
 
Example #26
Source File: PoolingAsyncResponseConsumerTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void onContentReceivedPassedDecoderToBuffer() throws IOException, PoolResourceException {

    // given
    ItemSourcePool<SimpleInputBuffer> itemSourcePool = mock(ItemSourcePool.class);

    ItemSource<SimpleInputBuffer> itemSource = mock(ItemSource.class);
    when(itemSourcePool.getPooled()).thenReturn(itemSource);

    SimpleInputBuffer buffer = mock(SimpleInputBuffer.class);
    when(itemSource.getSource()).thenReturn(buffer);

    PoolingAsyncResponseConsumer consumer = createDefaultTestObject(itemSourcePool);
    consumer.onResponseReceived(mock(HttpResponse.class));

    HttpEntity httpEntity = mock(HttpEntity.class);
    consumer.onEntityEnclosed(httpEntity, ContentType.create("application/json"));

    ContentDecoder contentDecoder = mock(ContentDecoder.class);

    // when
    consumer.onContentReceived(contentDecoder, mock(IOControl.class));

    // then
    verify(buffer, times(1)).consumeContent(eq(contentDecoder));

}