diff --git a/Sources/NIOCore/ChannelHandler.swift b/Sources/NIOCore/ChannelHandler.swift index aaa13f3b8..f51ef5abd 100644 --- a/Sources/NIOCore/ChannelHandler.swift +++ b/Sources/NIOCore/ChannelHandler.swift @@ -354,26 +354,49 @@ public struct NIOProtocolNegotiationResult { case deferredResult(EventLoopFuture>) } - private let result: Result + private let _result: Result - public init(result: NegotiationResult) { - self.result = .finished(result) - } - - public init(deferredResult: EventLoopFuture>) { - self.result = .deferredResult(deferredResult) - } - - /// Waits for the final protocol negotiation result and returns the value. + /// The final result of protocol negotiation. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) - public func waitForFinalResult() async throws -> NegotiationResult { - switch self.result { - case .finished(let negotiationResult): - return negotiationResult - case .deferredResult(let eventLoopFuture): - return try await eventLoopFuture.flatMap { $0.result.resolve(on: eventLoopFuture.eventLoop) }.get() + var result: NegotiationResult { + get async throws { + switch self._result { + case .finished(let negotiationResult): + return negotiationResult + case .deferredResult(let eventLoopFuture): + return try await eventLoopFuture.flatMap { $0._result.resolve(on: eventLoopFuture.eventLoop) }.get() + } } } + + /// Intializes a new ``NIOProtocolNegotiationResult`` with a final result. + /// + /// - Parameter result: The final result of protocol negotiation. + public init(result: NegotiationResult) { + self._result = .finished(result) + } + + /// Intializes a new ``NIOProtocolNegotiationResult`` with a deferred result. + /// + /// - Parameter deferredResult: The deferred result. + public init(deferredResult: EventLoopFuture>) { + self._result = .deferredResult(deferredResult) + } + + @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) + @available(*, deprecated, renamed: "getResult") + public func waitForFinalResult() async throws -> NegotiationResult { + try await self.result + } +} + +extension EventLoopFuture { + /// Get the result/error from the protocol negotiation. + @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) + @_spi(AsyncChannel) + public func getResult() async throws -> NegotiationResult where Value == NIOProtocolNegotiationResult { + try await self.get().result + } } @_spi(AsyncChannel) @@ -389,7 +412,7 @@ extension NIOProtocolNegotiationResult.Result { case .deferredResult(let future): return future.flatMap { result in - return resolve(on: eventLoop, result: result.result) + return resolve(on: eventLoop, result: result._result) } } } diff --git a/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift b/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift index e014601ac..44843b558 100644 --- a/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift +++ b/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift @@ -282,7 +282,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { try await withThrowingTaskGroup(of: Void.self) { group in for try await negotiationResult in channel.inboundStream { group.addTask { - switch try await negotiationResult.get().waitForFinalResult() { + switch try await negotiationResult.getResult() { case .string(let channel): for try await value in channel.inboundStream { continuation.yield(.string(value)) @@ -302,7 +302,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { port: channel.channel.localAddress!.port!, proposedALPN: .string ) - let stringNegotiationResult = try await stringNegotiationResultFuture.get().waitForFinalResult() + let stringNegotiationResult = try await stringNegotiationResultFuture.getResult() switch stringNegotiationResult { case .string(let stringChannel): // This is the actual content @@ -317,7 +317,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { port: channel.channel.localAddress!.port!, proposedALPN: .byte ) - let byteNegotiationResult = try await byteNegotiationResultFuture.get().waitForFinalResult() + let byteNegotiationResult = try await byteNegotiationResultFuture.getResult() switch byteNegotiationResult { case .string: preconditionFailure() @@ -356,7 +356,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { try await withThrowingTaskGroup(of: Void.self) { group in for try await negotiationResult in channel.inboundStream { group.addTask { - switch try await negotiationResult.get().waitForFinalResult() { + switch try await negotiationResult.getResult() { case .string(let channel): for try await value in channel.inboundStream { continuation.yield(.string(value)) @@ -377,7 +377,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { proposedOuterALPN: .string, proposedInnerALPN: .string ) - switch try await stringStringNegotiationResult.get().waitForFinalResult() { + switch try await stringStringNegotiationResult.getResult() { case .string(let stringChannel): // This is the actual content try await stringChannel.outboundWriter.write("hello") @@ -392,7 +392,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { proposedOuterALPN: .byte, proposedInnerALPN: .string ) - switch try await byteStringNegotiationResult.get().waitForFinalResult() { + switch try await byteStringNegotiationResult.getResult() { case .string(let stringChannel): // This is the actual content try await stringChannel.outboundWriter.write("hello") @@ -407,7 +407,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { proposedOuterALPN: .byte, proposedInnerALPN: .byte ) - switch try await byteByteNegotiationResult.get().waitForFinalResult() { + switch try await byteByteNegotiationResult.getResult() { case .string: preconditionFailure() case .byte(let byteChannel): @@ -422,7 +422,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { proposedOuterALPN: .string, proposedInnerALPN: .byte ) - switch try await stringByteNegotiationResult.get().waitForFinalResult() { + switch try await stringByteNegotiationResult.getResult() { case .string: preconditionFailure() case .byte(let byteChannel): @@ -485,7 +485,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { try await withThrowingTaskGroup(of: Void.self) { group in for try await negotiationResult in channel.inboundStream { group.addTask { - switch try await negotiationResult.get().waitForFinalResult() { + switch try await negotiationResult.getResult() { case .string(let channel): for try await value in channel.inboundStream { continuation.yield(.string(value)) @@ -506,7 +506,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { proposedALPN: .unknown ) await XCTAssertThrowsError( - try await failedProtocolNegotiation.get().waitForFinalResult() + try await failedProtocolNegotiation.getResult() ) // Let's check that we can still open a new connection @@ -515,7 +515,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { port: channel.channel.localAddress!.port!, proposedALPN: .string ) - switch try await stringNegotiationResult.get().waitForFinalResult() { + switch try await stringNegotiationResult.getResult() { case .string(let stringChannel): // This is the actual content try await stringChannel.outboundWriter.write("hello") @@ -599,7 +599,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { let firstNegotiationResult = try await group.next() let secondNegotiationResult = try await group.next() - switch (try await firstNegotiationResult?.get().waitForFinalResult(), try await secondNegotiationResult?.get().waitForFinalResult()) { + switch (try await firstNegotiationResult?.getResult(), try await secondNegotiationResult?.getResult()) { case (.string(let firstChannel), .string(let secondChannel)): var firstInboundIterator = firstChannel.inboundStream.makeAsyncIterator() var secondInboundIterator = secondChannel.inboundStream.makeAsyncIterator() @@ -682,7 +682,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { try toChannel.writeBytes(.init(string: "alpn:string\nHello\n")) let negotiationResult = try await group.next() - switch try await negotiationResult?.get().waitForFinalResult() { + switch try await negotiationResult?.getResult() { case .string(let channel): var inboundIterator = channel.inboundStream.makeAsyncIterator() do { @@ -753,7 +753,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { let firstNegotiationResult = try await group.next() let secondNegotiationResult = try await group.next() - switch (try await firstNegotiationResult?.get().waitForFinalResult(), try await secondNegotiationResult?.get().waitForFinalResult()) { + switch (try await firstNegotiationResult?.getResult(), try await secondNegotiationResult?.getResult()) { case (.string(let firstChannel), .string(let secondChannel)): var firstInboundIterator = firstChannel.inboundStream.makeAsyncIterator() var secondInboundIterator = secondChannel.inboundStream.makeAsyncIterator()