|
13 | 13 | //===----------------------------------------------------------------------===// |
14 | 14 |
|
15 | 15 | import NIOCore // NOTE: Not @testable import here -- testing public API surface. |
| 16 | +import NIOEmbedded |
16 | 17 | import NIOPosix // NOTE: Not @testable import here -- testing public API surface. |
17 | 18 | import Testing |
18 | 19 |
|
@@ -63,21 +64,50 @@ import Testing |
63 | 64 | @Test func testUnderlyingTransportForUnsupportedChannels() throws { |
64 | 65 | let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
65 | 66 | defer { #expect(throws: Never.self) { try group.syncShutdownGracefully() } } |
66 | | - |
67 | | - // Right now pipe channels do not expose their underlying transport, so we'll use this to test API behaviour. |
68 | | - let channel = try NIOPipeBootstrap(group: group).takingOwnershipOfDescriptor(output: STDOUT_FILENO).wait() |
| 67 | + let channel = EmbeddedChannel() |
69 | 68 | defer { #expect(throws: Never.self) { try channel.close().wait() } } |
70 | 69 |
|
71 | 70 | #expect(channel is any NIOTransportAccessibleChannelCore == false) |
72 | 71 |
|
73 | | - try channel.eventLoop.submit { |
74 | | - let syncOps = channel.pipeline.syncOperations |
| 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 | + } |
75 | 79 |
|
76 | | - // Calling the public API will never run the closure -- we cannot specify a type to pass the runtime check. |
77 | | - try #expect(syncOps.withUnsafeTransportIfAvailable { 42 } == nil) |
78 | | - try #expect(syncOps.withUnsafeTransportIfAvailable(of: Any.self) { _ in 42 } == nil) |
79 | | - try #expect(syncOps.withUnsafeTransportIfAvailable(of: CInt.self) { _ in 42 } == nil) |
80 | | - try #expect(syncOps.withUnsafeTransportIfAvailable(of: type(of: STDOUT_FILENO).self) { _ in 42 } == nil) |
81 | | - }.wait() |
| 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) |
82 | 112 | } |
83 | 113 | } |
0 commit comments