Skip to content

Commit 184df88

Browse files
authored
Avoid actually allocating a giant buffer (#2347)
Motivation: Our CI system is beginning to struggle with allocating a giant buffer in testSliceOfMassiveBufferWithAdvancedReaderIndexIsOk. We can work around this by faking out the allocation. Modifications: - Provide a fake allocator that doesn't actually allocate memory. - Rewrite the test to use it. Result: Test still validates the behaviour but doesn't touch memory anymore.
1 parent 1ce136b commit 184df88

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Tests/NIOCoreTests/ByteBufferTest.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,14 +1754,24 @@ class ByteBufferTest: XCTestCase {
17541754
throw XCTSkip("This test is only supported on 64-bit systems.")
17551755
}
17561756

1757+
// This allocator assumes that we'll never call realloc.
1758+
let fakeAllocator = ByteBufferAllocator(
1759+
hookedMalloc: { _ in .init(bitPattern: 0xdeadbeef) },
1760+
hookedRealloc: { _, _ in fatalError() },
1761+
hookedFree: { precondition($0 == .init(bitPattern: 0xdeadbeef)!) },
1762+
hookedMemcpy: {_, _, _ in }
1763+
)
1764+
17571765
let targetSize = Int(UInt32.max)
1758-
var buffer = self.allocator.buffer(capacity: targetSize)
1766+
var buffer = fakeAllocator.buffer(capacity: targetSize)
17591767

17601768
// Move the reader index forward such that we hit the slow path.
17611769
let offset = Int(_UInt24.max) + 1
17621770
buffer.moveWriterIndex(to: offset)
17631771
buffer.moveReaderIndex(to: offset)
1764-
buffer.writeInteger(UInt32(0))
1772+
1773+
// Pretend we wrote a UInt32.
1774+
buffer.moveWriterIndex(forwardBy: 4)
17651775

17661776
// We're going to move the readerIndex forward by 1, and then slice.
17671777
buffer.moveReaderIndex(forwardBy: 1)

0 commit comments

Comments
 (0)