Issue #6974 - improve testMaxMemory for Array & Mapped ByteBufferPools
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
aa09b8b7bd
commit
74785dd023
|
@ -22,10 +22,9 @@ import org.eclipse.jetty.util.StringUtil;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
|
@ -197,7 +196,7 @@ public class ArrayByteBufferPoolTest
|
||||||
public void testMaxMemory()
|
public void testMaxMemory()
|
||||||
{
|
{
|
||||||
int factor = 1024;
|
int factor = 1024;
|
||||||
int maxMemory = 11 * 1024;
|
int maxMemory = 11 * factor;
|
||||||
ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(-1, factor, -1, -1, -1, maxMemory);
|
ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(-1, factor, -1, -1, -1, maxMemory);
|
||||||
Bucket[] buckets = bufferPool.bucketsFor(true);
|
Bucket[] buckets = bufferPool.bucketsFor(true);
|
||||||
|
|
||||||
|
@ -207,26 +206,38 @@ public class ArrayByteBufferPoolTest
|
||||||
{
|
{
|
||||||
int capacity = factor * i;
|
int capacity = factor * i;
|
||||||
ByteBuffer buffer = bufferPool.acquire(capacity, true);
|
ByteBuffer buffer = bufferPool.acquire(capacity, true);
|
||||||
|
assertThat(buffer.capacity(), equalTo(capacity));
|
||||||
bufferPool.release(buffer);
|
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.
|
// 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);
|
bufferPool.release(buffer);
|
||||||
|
|
||||||
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
|
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
|
||||||
long memory = bufferPool.getMemory(true);
|
assertThat(bufferPool.getMemory(true), equalTo(8L * factor));
|
||||||
assertThat(memory, lessThan((long)maxMemory));
|
assertThat(buckets[1].size(), equalTo(1));
|
||||||
assertTrue(buckets[3].isEmpty());
|
assertThat(buckets[2].size(), equalTo(2));
|
||||||
|
assertThat(buckets[3].size(), equalTo(1));
|
||||||
|
|
||||||
// Create and release a large buffer.
|
// Create and release a large buffer.
|
||||||
// Max memory is exceeded and buckets 3 and 1 are cleared.
|
// Max memory is exceeded and buckets 3 and 1 are cleared.
|
||||||
// We will have 2x2+7=11.
|
// We will have 2x2+7=11.
|
||||||
buffer = bufferPool.newByteBuffer(7 * factor, true);
|
capacity = 7 * factor;
|
||||||
|
buffer = bufferPool.newByteBuffer(capacity, true);
|
||||||
bufferPool.release(buffer);
|
bufferPool.release(buffer);
|
||||||
memory = bufferPool.getMemory(true);
|
|
||||||
assertThat(memory, lessThanOrEqualTo((long)maxMemory));
|
assertThat(bufferPool.getMemory(true), equalTo(11L * factor));
|
||||||
assertTrue(buckets[0].isEmpty());
|
assertThat(buckets[2].size(), equalTo(2));
|
||||||
assertTrue(buckets[2].isEmpty());
|
assertThat(buckets[7].size(), equalTo(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,9 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@ -139,7 +137,7 @@ public class MappedByteBufferPoolTest
|
||||||
public void testMaxMemory()
|
public void testMaxMemory()
|
||||||
{
|
{
|
||||||
int factor = 1024;
|
int factor = 1024;
|
||||||
int maxMemory = 11 * 1024;
|
int maxMemory = 11 * factor;
|
||||||
MappedByteBufferPool bufferPool = new MappedByteBufferPool(factor, -1, null, -1, maxMemory);
|
MappedByteBufferPool bufferPool = new MappedByteBufferPool(factor, -1, null, -1, maxMemory);
|
||||||
ConcurrentMap<Integer, Bucket> buckets = bufferPool.bucketsFor(true);
|
ConcurrentMap<Integer, Bucket> buckets = bufferPool.bucketsFor(true);
|
||||||
|
|
||||||
|
@ -149,26 +147,39 @@ public class MappedByteBufferPoolTest
|
||||||
{
|
{
|
||||||
int capacity = factor * i;
|
int capacity = factor * i;
|
||||||
ByteBuffer buffer = bufferPool.acquire(capacity, true);
|
ByteBuffer buffer = bufferPool.acquire(capacity, true);
|
||||||
|
assertThat(buffer.capacity(), equalTo(capacity));
|
||||||
bufferPool.release(buffer);
|
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.
|
// 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);
|
bufferPool.release(buffer);
|
||||||
|
|
||||||
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
|
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
|
||||||
long memory = bufferPool.getMemory(true);
|
assertThat(bufferPool.getMemory(true), equalTo(8L * factor));
|
||||||
assertThat(memory, lessThan((long)maxMemory));
|
assertThat(buckets.get(1).size(), equalTo(1));
|
||||||
assertNull(buckets.get(4));
|
assertThat(buckets.get(2).size(), equalTo(2));
|
||||||
|
assertThat(buckets.get(3).size(), equalTo(1));
|
||||||
|
|
||||||
// Create and release a large buffer.
|
// Create and release a large buffer.
|
||||||
// Max memory is exceeded and buckets 3 and 1 are cleared.
|
// Max memory is exceeded and buckets 3 and 1 are cleared.
|
||||||
// We will have 2x2+7=11.
|
// 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);
|
bufferPool.release(buffer);
|
||||||
memory = bufferPool.getMemory(true);
|
|
||||||
assertThat(memory, lessThanOrEqualTo((long)maxMemory));
|
assertThat(bufferPool.getMemory(true), equalTo(11L * factor));
|
||||||
assertNull(buckets.get(1));
|
assertThat(buckets.get(2).size(), equalTo(2));
|
||||||
assertNull(buckets.get(3));
|
assertThat(buckets.get(7).size(), equalTo(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue