Issue #6974 - improve testMaxMemory for Array & Mapped ByteBufferPools

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-11-29 14:41:51 +11:00
parent aa09b8b7bd
commit 74785dd023
2 changed files with 47 additions and 25 deletions

View File

@ -22,10 +22,9 @@ import org.eclipse.jetty.util.StringUtil;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -197,7 +196,7 @@ public class ArrayByteBufferPoolTest
public void testMaxMemory()
{
int factor = 1024;
int maxMemory = 11 * 1024;
int maxMemory = 11 * factor;
ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(-1, factor, -1, -1, -1, maxMemory);
Bucket[] buckets = bufferPool.bucketsFor(true);
@ -207,26 +206,38 @@ public class ArrayByteBufferPoolTest
{
int capacity = factor * i;
ByteBuffer buffer = bufferPool.acquire(capacity, true);
assertThat(buffer.capacity(), equalTo(capacity));
bufferPool.release(buffer);
}
// Check state of buckets.
assertThat(bufferPool.getMemory(true), equalTo(10L * factor));
assertThat(buckets[1].size(), equalTo(1));
assertThat(buckets[2].size(), equalTo(1));
assertThat(buckets[3].size(), equalTo(1));
assertThat(buckets[4].size(), equalTo(1));
// Create and release a buffer to exceed the max memory.
ByteBuffer buffer = bufferPool.newByteBuffer(2 * factor, true);
int capacity = 2 * factor;
ByteBuffer buffer = bufferPool.newByteBuffer(capacity, true);
assertThat(buffer.capacity(), equalTo(capacity));
bufferPool.release(buffer);
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
long memory = bufferPool.getMemory(true);
assertThat(memory, lessThan((long)maxMemory));
assertTrue(buckets[3].isEmpty());
assertThat(bufferPool.getMemory(true), equalTo(8L * factor));
assertThat(buckets[1].size(), equalTo(1));
assertThat(buckets[2].size(), equalTo(2));
assertThat(buckets[3].size(), equalTo(1));
// Create and release a large buffer.
// Max memory is exceeded and buckets 3 and 1 are cleared.
// We will have 2x2+7=11.
buffer = bufferPool.newByteBuffer(7 * factor, true);
capacity = 7 * factor;
buffer = bufferPool.newByteBuffer(capacity, true);
bufferPool.release(buffer);
memory = bufferPool.getMemory(true);
assertThat(memory, lessThanOrEqualTo((long)maxMemory));
assertTrue(buckets[0].isEmpty());
assertTrue(buckets[2].isEmpty());
assertThat(bufferPool.getMemory(true), equalTo(11L * factor));
assertThat(buckets[2].size(), equalTo(2));
assertThat(buckets[7].size(), equalTo(1));
}
}

View File

@ -23,11 +23,9 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
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.assertTrue;
@ -139,7 +137,7 @@ public class MappedByteBufferPoolTest
public void testMaxMemory()
{
int factor = 1024;
int maxMemory = 11 * 1024;
int maxMemory = 11 * factor;
MappedByteBufferPool bufferPool = new MappedByteBufferPool(factor, -1, null, -1, maxMemory);
ConcurrentMap<Integer, Bucket> buckets = bufferPool.bucketsFor(true);
@ -149,26 +147,39 @@ public class MappedByteBufferPoolTest
{
int capacity = factor * i;
ByteBuffer buffer = bufferPool.acquire(capacity, true);
assertThat(buffer.capacity(), equalTo(capacity));
bufferPool.release(buffer);
}
// Check state of buckets.
assertThat(bufferPool.getMemory(true), equalTo(10L * factor));
assertThat(buckets.get(1).size(), equalTo(1));
assertThat(buckets.get(2).size(), equalTo(1));
assertThat(buckets.get(3).size(), equalTo(1));
assertThat(buckets.get(4).size(), equalTo(1));
// Create and release a buffer to exceed the max memory.
ByteBuffer buffer = bufferPool.newByteBuffer(2 * factor, true);
int capacity = 2 * factor;
ByteBuffer buffer = bufferPool.newByteBuffer(capacity, true);
assertThat(buffer.capacity(), equalTo(capacity));
bufferPool.release(buffer);
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
long memory = bufferPool.getMemory(true);
assertThat(memory, lessThan((long)maxMemory));
assertNull(buckets.get(4));
assertThat(bufferPool.getMemory(true), equalTo(8L * factor));
assertThat(buckets.get(1).size(), equalTo(1));
assertThat(buckets.get(2).size(), equalTo(2));
assertThat(buckets.get(3).size(), equalTo(1));
// Create and release a large buffer.
// Max memory is exceeded and buckets 3 and 1 are cleared.
// We will have 2x2+7=11.
buffer = bufferPool.newByteBuffer(7 * factor, true);
capacity = 7 * factor;
buffer = bufferPool.newByteBuffer(capacity, true);
assertThat(buffer.capacity(), equalTo(capacity));
bufferPool.release(buffer);
memory = bufferPool.getMemory(true);
assertThat(memory, lessThanOrEqualTo((long)maxMemory));
assertNull(buckets.get(1));
assertNull(buckets.get(3));
assertThat(bufferPool.getMemory(true), equalTo(11L * factor));
assertThat(buckets.get(2).size(), equalTo(2));
assertThat(buckets.get(7).size(), equalTo(1));
}
}