Hi
I'm currently writing a library that wraps another API and I'm using the NIOHTTP1TestServer
to assert my library's requests/responses. But I'm seeing some failing tests that do not occur on my Mac, but do occur on Linux, these are around the capacity allocated for byte buffers. In essence the code I'm using is the following.
Which is taken from the NIOHTTP1TestServer
docs swift-nio/NIOHTTP1TestServer.swift at 2d913d65af1a4f3f72cee2c71f565d7a07f50105 · apple/swift-nio · GitHub
XCTAssertNoThrow(requestComplete = try! Self.utilities.client.caseFields.add(caseField: Self.utilities.newCaseFieldRequestObject))
var requestBuffer = Self.utilities.allocator.buffer(capacity: 0)
try! requestBuffer.writeJSONEncodable(Self.utilities.newCaseFieldRequestObject, encoder: Self.utilities.encoder)
let contentLength = requestBuffer.readableBytes
XCTAssertNoThrow(XCTAssertEqual(.head(.init(...test headers, uri, method etc...)))) // passes
XCTAssertNoThrow(XCTAssertEqual(.body(requestBuffer), try Self.utilities.testServer.readInbound())) // fails on Linux
This will fail at the .body()
assertion. With the following error:
"body(ByteBuffer { readerIndex: 0, writerIndex: 226, readableBytes: 226, capacity: 256, slice: _ByteBufferSlice { 0..<256 }, storage: 0x00005613cfc5a8d0 (256 bytes) })") is not equal to (
"body(ByteBuffer { readerIndex: 0, writerIndex: 226, readableBytes: 226, capacity: 226, slice: _ByteBufferSlice { 201..<427 }, storage: 0x00007f1d000439a0 (1024 bytes) })") -
This reads to me that the expected object is received as both byte length matches. But it seems that on the Mac the capacity is larger than on Linux?
Originally I manually set the capacity to a number larger than the the object, which is where I first noticed the error on Linux, I've since moved to setting it to 0
, because it seems from the source that the buffer will expand as needed.
I also see in the source
/// The current capacity of the storage of this `ByteBuffer`, this is not constant and does _not_ signify the number
/// of bytes that have been written to this `ByteBuffer`.
public var capacity: Int {
return self._slice.count
}
These always pass on my machine running 5.3, but fail on swift:5.3-focal
. So is this an expected behaviour between the two OS?
I should add, that I have another 35 tests and they don't exhibit this behaviour and pass fine in Docker and my machine. It seems to be just these two for some reason?