|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore // NOTE: Not @testable import here -- testing public API surface. |
| 16 | +import NIOEmbedded |
| 17 | +import NIOPosix // NOTE: Not @testable import here -- testing public API surface. |
| 18 | +import Testing |
| 19 | + |
| 20 | +@Suite struct NIOTransportAccessibleChannelCoreTests { |
| 21 | + @Test func testUnderlyingSocketAccessForSocketBasedChannel() throws { |
| 22 | + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 23 | + defer { #expect(throws: Never.self) { try group.syncShutdownGracefully() } } |
| 24 | + let channel = try DatagramBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait() |
| 25 | + defer { #expect(throws: Never.self) { try channel.close().wait() } } |
| 26 | + |
| 27 | + // We don't expect users to do this runtime check, but test the channel we got back from bootstrap conforms. |
| 28 | + #expect(channel is any NIOTransportAccessibleChannelCore) |
| 29 | + #expect(channel is any NIOTransportAccessibleChannelCore<NIOBSDSocket.Handle>) |
| 30 | + #expect(channel is any NIOTransportAccessibleChannelCore<Any> == false) |
| 31 | + |
| 32 | + // Here we try the public API use, in various flavours. |
| 33 | + try channel.eventLoop.submit { |
| 34 | + let syncOps = channel.pipeline.syncOperations |
| 35 | + |
| 36 | + // Calling without explicit transport type runs closure if body inefers correct transport type. |
| 37 | + try #expect(syncOps.withUnsafeTransportIfAvailable { fd in fd != NIOBSDSocket.invalidHandle } == true) |
| 38 | + try #expect(syncOps.withUnsafeTransportIfAvailable { $0 != NIOBSDSocket.invalidHandle } == true) |
| 39 | + |
| 40 | + // Calling with explicit correct transport type runs closure. |
| 41 | + try #expect(syncOps.withUnsafeTransportIfAvailable(of: NIOBSDSocket.Handle.self) { _ in 42 } == 42) |
| 42 | + try #expect(syncOps.withUnsafeTransportIfAvailable { (_: NIOBSDSocket.Handle) in 42 } == 42) |
| 43 | + |
| 44 | + // Calling with explicit incorrect transport type does not run closure. |
| 45 | + try #expect(syncOps.withUnsafeTransportIfAvailable(of: String.self) { _ in 42 } == nil) |
| 46 | + try #expect(syncOps.withUnsafeTransportIfAvailable { (_: String) in 42 } == nil) |
| 47 | + |
| 48 | + // Calling with explicit Any transport type does not run closure. |
| 49 | + try #expect(syncOps.withUnsafeTransportIfAvailable(of: Any.self) { _ in 42 } == nil) |
| 50 | + try #expect(syncOps.withUnsafeTransportIfAvailable { (_: Any) in 42 } == nil) |
| 51 | + |
| 52 | + // Calling without explicit transport type does not run closure, even if body doesn't use transport. |
| 53 | + try #expect(syncOps.withUnsafeTransportIfAvailable { 42 } == nil) |
| 54 | + |
| 55 | + // Fun aside: What is the resolved type of the above function and why does it allow ignoring closure param? |
| 56 | + try #expect(syncOps.withUnsafeTransportIfAvailable { $0.self } == nil) |
| 57 | + // Answer: `$0: any (~Copyable & ~Escapable).Type` |
| 58 | + |
| 59 | + // Calling without explicit transport type does not run closure, even if body uses compatible literal value. |
| 60 | + try #expect(syncOps.withUnsafeTransportIfAvailable { transport in transport != -1 } == nil) |
| 61 | + }.wait() |
| 62 | + } |
| 63 | + |
| 64 | + @Test func testUnderlyingTransportForUnsupportedChannels() throws { |
| 65 | + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 66 | + defer { #expect(throws: Never.self) { try group.syncShutdownGracefully() } } |
| 67 | + let channel = EmbeddedChannel() |
| 68 | + defer { #expect(throws: Never.self) { try channel.close().wait() } } |
| 69 | + |
| 70 | + #expect(channel is any NIOTransportAccessibleChannelCore == false) |
| 71 | + |
| 72 | + // Calling the public API will never run the closure -- we cannot specify a type to pass the runtime check. |
| 73 | + let syncOps = channel.pipeline.syncOperations |
| 74 | + try #expect(syncOps.withUnsafeTransportIfAvailable { 42 } == nil) |
| 75 | + try #expect(syncOps.withUnsafeTransportIfAvailable(of: Any.self) { _ in 42 } == nil) |
| 76 | + try #expect(syncOps.withUnsafeTransportIfAvailable(of: CInt.self) { _ in 42 } == nil) |
| 77 | + try #expect(syncOps.withUnsafeTransportIfAvailable(of: type(of: STDOUT_FILENO).self) { _ in 42 } == nil) |
| 78 | + } |
| 79 | + |
| 80 | + @Test func testUnderlyingTransportConformanceForExpectedChannels() throws { |
| 81 | + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 82 | + defer { #expect(throws: Never.self) { try group.syncShutdownGracefully() } } |
| 83 | + |
| 84 | + // SeverSocketChannel -- yep. |
| 85 | + let serverChannel = try ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait() |
| 86 | + defer { #expect(throws: Never.self) { try serverChannel.close().wait() } } |
| 87 | + #expect(serverChannel is any NIOTransportAccessibleChannelCore) |
| 88 | + #expect(serverChannel is any NIOTransportAccessibleChannelCore<NIOBSDSocket.Handle>) |
| 89 | + |
| 90 | + // SocketChannel -- yep. |
| 91 | + let clientChannel = try ClientBootstrap(group: group).connect(to: serverChannel.localAddress!).wait() |
| 92 | + defer { #expect(throws: Never.self) { try clientChannel.close().wait() } } |
| 93 | + #expect(clientChannel is any NIOTransportAccessibleChannelCore) |
| 94 | + #expect(clientChannel is any NIOTransportAccessibleChannelCore<NIOBSDSocket.Handle>) |
| 95 | + |
| 96 | + // DatagramChannel -- yep. |
| 97 | + let datagramChannel = try DatagramBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait() |
| 98 | + defer { #expect(throws: Never.self) { try datagramChannel.close().wait() } } |
| 99 | + #expect(datagramChannel is any NIOTransportAccessibleChannelCore) |
| 100 | + #expect(datagramChannel is any NIOTransportAccessibleChannelCore<NIOBSDSocket.Handle>) |
| 101 | + |
| 102 | + // PipeChannel -- yep. |
| 103 | + let pipeChannel = try NIOPipeBootstrap(group: group).takingOwnershipOfDescriptor(output: STDOUT_FILENO).wait() |
| 104 | + defer { #expect(throws: Never.self) { try pipeChannel.close().wait() } } |
| 105 | + #expect(pipeChannel is any NIOTransportAccessibleChannelCore) |
| 106 | + #expect(pipeChannel is any NIOTransportAccessibleChannelCore<NIOBSDSocket.PipeHandle>) |
| 107 | + |
| 108 | + // EmbeddedChannel -- nope. |
| 109 | + let embeddedChannel = EmbeddedChannel() |
| 110 | + defer { #expect(throws: Never.self) { try embeddedChannel.close().wait() } } |
| 111 | + #expect(embeddedChannel is any NIOTransportAccessibleChannelCore == false) |
| 112 | + } |
| 113 | +} |
0 commit comments