|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.client.nio; |
| 10 | + |
| 11 | +import com.carrotsearch.randomizedtesting.RandomizedTest; |
| 12 | + |
| 13 | +import org.apache.hc.core5.http.ContentTooLongException; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.ByteBuffer; |
| 20 | + |
| 21 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.junit.Assert.assertThrows; |
| 24 | + |
| 25 | +public class HeapBufferedAsyncEntityConsumerTest extends RandomizedTest { |
| 26 | + private static final int BUFFER_LIMIT = 100 * 1024 * 1024 /* 100Mb */; |
| 27 | + private HeapBufferedAsyncEntityConsumer consumer; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setUp() { |
| 31 | + consumer = new HeapBufferedAsyncEntityConsumer(BUFFER_LIMIT); |
| 32 | + } |
| 33 | + |
| 34 | + @After |
| 35 | + public void tearDown() { |
| 36 | + consumer.releaseResources(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void consumerAllocatesBufferLimit() throws IOException { |
| 41 | + consumer.consume(randomByteBufferOfLength(1000).flip()); |
| 42 | + assertThat(consumer.getBuffer().capacity(), equalTo(1000)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void consumerAllocatesEmptyBuffer() throws IOException { |
| 47 | + consumer.consume(ByteBuffer.allocate(0).flip()); |
| 48 | + assertThat(consumer.getBuffer().capacity(), equalTo(0)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void consumerExpandsBufferLimits() throws IOException { |
| 53 | + consumer.consume(randomByteBufferOfLength(1000).flip()); |
| 54 | + consumer.consume(randomByteBufferOfLength(2000).flip()); |
| 55 | + consumer.consume(randomByteBufferOfLength(3000).flip()); |
| 56 | + assertThat(consumer.getBuffer().capacity(), equalTo(6000)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void consumerAllocateLimit() throws IOException { |
| 61 | + consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip()); |
| 62 | + assertThat(consumer.getBuffer().capacity(), equalTo(BUFFER_LIMIT)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void consumerFailsToOverAllocateLimit() throws IOException { |
| 67 | + consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip()); |
| 68 | + assertThrows(ContentTooLongException.class, () -> consumer.consume(randomByteBufferOfLength(1).flip())); |
| 69 | + } |
| 70 | + |
| 71 | + private ByteBuffer randomByteBufferOfLength(int length) { |
| 72 | + return ByteBuffer.allocate(length).put(randomBytesOfLength(length)); |
| 73 | + } |
| 74 | +} |
0 commit comments