Files
swift-nio/Tests/NIOCoreTests/NIOAnyDebugTest.swift
Johannes Weiss 0ee1657e8a Make NIOFileDescriptor/FileRegion/IOData Sendable & soft-deprecated (#2598)
Motivation:

`IOData` is a legacy but alas also core type that needs to be
`Sendable`. Before this PR however it can't be `Sendable` because it
holds a `FileRegion` which holds a `NIOFileDescriptor`. So let's make
all of these `Sendable` but let's also start the deprecation journey for
the following types:

- `IOData`, now soft-deprecated (no warnings) because on its reliance on
`FileRegion`
- `FileRegion`, now soft-deprecated (no warnings) because on its
reliance on `NIOFileHandle`
- `NIOFileHandle`, now soft-deprecated (warnings on the
`NIOFileHandle(descriptor:)` constructor but with a
`NIOFileHandle(_deprecatedTakingOwnershipOfDescriptor:)` alternative
- `NonBlockingFileIO`, now soft-deprecated (warnings on the `openFile`
functions (but with `_deprecated` alternatives) because of their
reliance on `NIOFileHandle)

Modification:

- Make `NIOFileDescriptor`, `FileRegion` and `IOData` `Sendable` by
tracking the fd number and the usage state in an atomic
- Enforce singular access by making the `withFileDescriptor { fd ... }`
function atomically exchange the fd number for a "I'm busy" sentinel
value
- Start deprecating `IOData`, `NIOFileHandle`, `NonBlockingFileIO`,
`FileRegion`

Result:
- `NIOFileDescriptor`, `FileRegion` and `IOData` can be `Sendable`
2024-11-25 16:05:30 +00:00

73 lines
2.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 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 NIOCore
import XCTest
class NIOAnyDebugTest: XCTestCase {
func testCustomStringConvertible() throws {
XCTAssertEqual(wrappedInNIOAnyBlock("string").description, "String: string")
XCTAssertEqual(wrappedInNIOAnyBlock(123).description, "Int: 123")
let bb = ByteBuffer(string: "byte buffer string")
XCTAssertEqual(
wrappedInNIOAnyBlock(bb).description,
"ByteBuffer: [627974652062756666657220737472696e67](18 bytes)"
)
let fileHandle = NIOFileHandle(_deprecatedTakingOwnershipOfDescriptor: 1)
defer {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 1, endIndex: 5)
XCTAssertEqual(
wrappedInNIOAnyBlock(fileRegion).description,
"""
FileRegion: \
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).description,
"""
AddressedEnvelope<ByteBuffer>: \
AddressedEnvelope { \
remoteAddress: \(socketAddress), \
data: \(envelopeByteBuffer) }
"""
)
}
func testCustomDebugStringConvertible() {
XCTAssertEqual(wrappedInNIOAnyBlock("string").debugDescription, "(String: string)")
let any = wrappedInNIOAnyBlock("test")
XCTAssertEqual(any.debugDescription, "(\(any.description))")
}
private func wrappedInNIOAnyBlock(_ item: Any) -> NIOAny {
NIOAny(item)
}
}