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.
This commit is contained in:
Cory Benfield 2023-01-12 10:33:54 +00:00 committed by GitHub
parent 1ce136b4c3
commit 184df88ec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -1754,14 +1754,24 @@ class ByteBufferTest: XCTestCase {
throw XCTSkip("This test is only supported on 64-bit systems.")
}
// This allocator assumes that we'll never call realloc.
let fakeAllocator = ByteBufferAllocator(
hookedMalloc: { _ in .init(bitPattern: 0xdeadbeef) },
hookedRealloc: { _, _ in fatalError() },
hookedFree: { precondition($0 == .init(bitPattern: 0xdeadbeef)!) },
hookedMemcpy: {_, _, _ in }
)
let targetSize = Int(UInt32.max)
var buffer = self.allocator.buffer(capacity: targetSize)
var buffer = fakeAllocator.buffer(capacity: targetSize)
// Move the reader index forward such that we hit the slow path.
let offset = Int(_UInt24.max) + 1
buffer.moveWriterIndex(to: offset)
buffer.moveReaderIndex(to: offset)
buffer.writeInteger(UInt32(0))
// Pretend we wrote a UInt32.
buffer.moveWriterIndex(forwardBy: 4)
// We're going to move the readerIndex forward by 1, and then slice.
buffer.moveReaderIndex(forwardBy: 1)