smengcl commented on code in PR #6859:
URL: https://github.com/apache/ozone/pull/6859#discussion_r1676500232
##########
hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBufferPool.java:
##########
@@ -20,38 +20,106 @@
import org.apache.hadoop.ozone.common.ChunkBuffer;
+import org.apache.ozone.test.GenericTestUtils;
+import org.apache.ozone.test.GenericTestUtils.LogCapturer;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.slf4j.event.Level;
import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link BufferPool}.
*/
class TestBufferPool {
+ @BeforeAll
+ static void init() {
+ GenericTestUtils.setLogLevel(BufferPool.LOG, Level.DEBUG);
+ }
+
@Test
- void testBufferPool() {
+ void testBufferPool() throws Exception {
testBufferPool(BufferPool.empty());
testBufferPool(1, 1);
testBufferPool(3, 1 << 20);
testBufferPool(10, 1 << 10);
}
- private static void testBufferPool(final int capacity, final int bufferSize)
{
+ @Test
+ void testBufferPoolConcurrently() throws Exception {
+ final BufferPool pool = new BufferPool(1 << 20, 10);
+ final Deque<ChunkBuffer> buffers = assertAllocate(pool);
+
+ assertAllocationBlocked(pool);
+ assertAllocationBlockedUntilReleased(pool, buffers);
+ }
+
+ private void assertAllocationBlockedUntilReleased(BufferPool pool,
Deque<ChunkBuffer> buffers) throws Exception {
+ // As the pool is full, allocation will need to wait until a buffer is
released.
+ assertFull(pool);
+
+ LogCapturer logCapturer = LogCapturer.captureLogs(BufferPool.LOG);
+ AtomicReference<ChunkBuffer> allocated = new AtomicReference<>();
+ Thread allocator = new Thread(() -> {
+ try {
+ allocated.set(pool.allocateBuffer(0));
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ });
+ ChunkBuffer toRelease = buffers.removeFirst();
+ Thread releaser = new Thread(() -> {
+ pool.releaseBuffer(toRelease);
+ });
+ allocator.start();
+ Thread.sleep(100);
Review Comment:
```suggestion
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]