-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to co…
…nsume the response Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
- Loading branch information
Showing
3 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
client/rest/src/test/java/org/opensearch/client/nio/HeapBufferedAsyncEntityConsumerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.nio; | ||
|
||
import com.carrotsearch.randomizedtesting.RandomizedTest; | ||
|
||
import org.apache.hc.core5.http.ContentTooLongException; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
public class HeapBufferedAsyncEntityConsumerTest extends RandomizedTest { | ||
private static final int BUFFER_LIMIT = 100 * 1024 * 1024 /* 100Mb */; | ||
private HeapBufferedAsyncEntityConsumer consumer; | ||
|
||
@Before | ||
public void setUp() { | ||
consumer = new HeapBufferedAsyncEntityConsumer(BUFFER_LIMIT); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
consumer.releaseResources(); | ||
} | ||
|
||
@Test | ||
public void consumerAllocatesBufferLimit() throws IOException { | ||
consumer.consume(randomByteBufferOfLength(1000).flip()); | ||
assertThat(consumer.getBuffer().capacity(), equalTo(1000)); | ||
} | ||
|
||
@Test | ||
public void consumerAllocatesEmptyBuffer() throws IOException { | ||
consumer.consume(ByteBuffer.allocate(0).flip()); | ||
assertThat(consumer.getBuffer().capacity(), equalTo(0)); | ||
} | ||
|
||
@Test | ||
public void consumerExpandsBufferLimits() throws IOException { | ||
consumer.consume(randomByteBufferOfLength(1000).flip()); | ||
consumer.consume(randomByteBufferOfLength(2000).flip()); | ||
consumer.consume(randomByteBufferOfLength(3000).flip()); | ||
assertThat(consumer.getBuffer().capacity(), equalTo(6000)); | ||
} | ||
|
||
@Test | ||
public void consumerAllocateLimit() throws IOException { | ||
consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip()); | ||
assertThat(consumer.getBuffer().capacity(), equalTo(BUFFER_LIMIT)); | ||
} | ||
|
||
@Test | ||
public void consumerFailsToOverAllocateLimit() throws IOException { | ||
consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip()); | ||
assertThrows(ContentTooLongException.class, () -> consumer.consume(randomByteBufferOfLength(1).flip())); | ||
} | ||
|
||
private ByteBuffer randomByteBufferOfLength(int length) { | ||
return ByteBuffer.allocate(length).put(randomBytesOfLength(length)); | ||
} | ||
} |