Files
swift-nio/Tests/NIOTests/NIOAnyDebugTest.swift
T
f9a7eaf6e0 Disambiguate the capacity of the ByteBuffer (no. of addressable bytes) and capacity of underlying storage (#1485). (#1629)
Motivation:

The exisiting ByteBuffer`s `capacity` property exposes the number of
addressable bytes in the buffer (that is, the difference between its
upper and lower bound). This value can be significantly different than
the number of bytes allocated to the buffer's underlying storage.
For example, when constructing of COW slice from the original buffer the
underlying storage maybe tens of thousands of bytes while the slice's
capacity is 1.

To avoid any ambiguitity, a new property - `storageCapacity` - will be
added.

Modifications:

Added new `storageCapacity` property to expose the capacity of the buffer (or
derived COW slice) underlying storage (that is allocated to the buffer).
This property is part of public interface ans is typed as an Int.

Updated unit tests to verify `storedCapacity` remains constant when
slicing the ByteBuffer and also to account for the addition of the new
property in the CustomStringConvertible desciption.

Result:

The `storageCapacity` of a buffer and a COW slice taken from that buffer
with remain the same data is written to the slice.

Co-authored-by: Graeme Jenkinson <gcjenkinson@Graemes-MacBook-Pro.local>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-09-25 11:10:20 +01:00

58 lines
2.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) YEARS Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
class NIOAnyDebugTest: XCTestCase {
func testCustomStringConvertible() throws {
XCTAssertEqual(wrappedInNIOAnyBlock("string"), wrappedInNIOAnyBlock("string"))
XCTAssertEqual(wrappedInNIOAnyBlock(123), wrappedInNIOAnyBlock("123"))
let bb = ByteBuffer(string: "byte buffer string")
XCTAssertTrue(wrappedInNIOAnyBlock(bb).contains("NIOAny { ByteBuffer { readerIndex: 0, writerIndex: 18, readableBytes: 18, capacity: 32, storageCapacity: 32, slice: _ByteBufferSlice { 0..<32 }, storage: "))
XCTAssertTrue(wrappedInNIOAnyBlock(bb).hasSuffix(" }"))
let fileHandle = NIOFileHandle(descriptor: 1)
defer {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 1, endIndex: 5)
XCTAssertEqual(wrappedInNIOAnyBlock(fileRegion), wrappedInNIOAnyBlock("""
FileRegion { \
handle: \
FileHandle \
{ descriptor: 1 \
}, \
readerIndex: \(fileRegion.readerIndex), \
endIndex: \(fileRegion.endIndex) }
"""))
let socketAddress = try SocketAddress(unixDomainSocketPath: "socketAdress")
let envelopeByteBuffer = ByteBuffer(string: "envelope buffer")
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: socketAddress, data: envelopeByteBuffer)
XCTAssertEqual(wrappedInNIOAnyBlock("\(envelope)"), wrappedInNIOAnyBlock("""
AddressedEnvelope { \
remoteAddress: \(socketAddress), \
data: \(envelopeByteBuffer) }
"""))
}
private func wrappedInNIOAnyBlock(_ item: Any) -> String {
return "NIOAny { \(item) }"
}
}