lidavidm commented on code in PR #34231:
URL: https://github.com/apache/arrow/pull/34231#discussion_r1111970829
##########
java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java:
##########
@@ -54,6 +57,80 @@ public void wrapForeignAllocation() {
assertEquals(0, allocator.getAllocatedMemory());
}
+ @Test
+ public void wrapForeignAllocationWithAllocationListener() {
+ final long bufferSize = 16;
+
+ final CountingAllocationListener listener = new
CountingAllocationListener();
+ try (BufferAllocator listenedAllocator =
+ allocator.newChildAllocator("child", listener, 0L,
allocator.getLimit())) {
+ UnsafeForeignAllocation allocation = new
UnsafeForeignAllocation(bufferSize);
+ try {
+ assertEquals(0, listenedAllocator.getAllocatedMemory());
+ ArrowBuf buf = listenedAllocator.wrapForeignAllocation(allocation);
+ assertEquals(bufferSize, buf.capacity());
+ assertEquals(16, listener.getCurrentMem());
+ buf.close();
+ assertEquals(0, listener.getCurrentMem());
+ assertTrue(allocation.released);
+ } finally {
+ allocation.release0();
+ }
+ assertEquals(0, listenedAllocator.getAllocatedMemory());
+ }
+ assertEquals(1, listener.getNumPreCalls());
+ assertEquals(1, listener.getNumCalls());
+ assertEquals(1, listener.getNumReleaseCalls());
+ assertEquals(16, listener.getTotalMem());
+ }
+
+ @Test(expected = OutOfMemoryException.class)
+ public void wrapForeignAllocationFailedWithAllocationListener() {
+ final long bufferSize = 16;
+ final long limit = bufferSize - 1;
+
+ final CountingAllocationListener listener = new
CountingAllocationListener();
+ try (BufferAllocator listenedAllocator =
+ allocator.newChildAllocator("child", listener, 0L, limit)) {
+ UnsafeForeignAllocation allocation = new
UnsafeForeignAllocation(bufferSize);
+ try {
+ assertEquals(0, listenedAllocator.getAllocatedMemory());
+ ArrowBuf buf = listenedAllocator.wrapForeignAllocation(allocation);
+ assertEquals(bufferSize, buf.capacity());
+ buf.close();
+ assertTrue(allocation.released);
+ } finally {
+ allocation.release0();
+ }
+ }
+ }
+
+ @Test
+ public void wrapForeignAllocationWithAllocationListenerReclaimingSpace() {
+ final long bufferSize = 16;
+ final long limit = 2 * bufferSize - 1;
+
+ final List<ArrowBuf> bufferedToBeFreed = new ArrayList<>();
Review Comment:
nit: typo
```suggestion
final List<ArrowBuf> buffersToBeFreed = new ArrayList<>();
```
--
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]