-
Notifications
You must be signed in to change notification settings - Fork 125
Add unit tests for NWWaitingHandler, closes #589 #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dnadoba
merged 6 commits into
swift-server:main
from
natikgadzhi:internal/waiting-handler-unit-tests
Aug 14, 2023
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2daed11
Add unit tests for NWWaitingHandler
natikgadzhi f3a3ef0
Bump swift-nio-transport-services to 1.13.0
natikgadzhi 7270e47
Merge branch 'main' into internal/waiting-handler-unit-tests
natikgadzhi 4efea4c
SwiftFormat@0.48.8
natikgadzhi 9fcf553
Apply suggestions from code review
natikgadzhi e02093c
Merge branch 'main' into internal/waiting-handler-unit-tests
natikgadzhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(Network) | ||
@testable import AsyncHTTPClient | ||
import Network | ||
import NIOCore | ||
import NIOTransportServices | ||
import NIOEmbedded | ||
import NIOSSL | ||
import XCTest | ||
|
||
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) | ||
class NWWaitingHandlerTests: XCTestCase { | ||
|
||
class MockRequester: HTTPConnectionRequester { | ||
var waitingForConnectivityCalled = false | ||
var connectionID: AsyncHTTPClient.HTTPConnectionPool.Connection.ID? = nil | ||
var transientError: NWError? = nil | ||
|
||
func http1ConnectionCreated(_: AsyncHTTPClient.HTTP1Connection) { | ||
} | ||
|
||
func http2ConnectionCreated(_: AsyncHTTPClient.HTTP2Connection, maximumStreams: Int) { | ||
} | ||
|
||
func failedToCreateHTTPConnection(_: AsyncHTTPClient.HTTPConnectionPool.Connection.ID, error: Error) { | ||
} | ||
|
||
func waitingForConnectivity(_ connectionID: AsyncHTTPClient.HTTPConnectionPool.Connection.ID, error: Error) { | ||
self.waitingForConnectivityCalled = true | ||
self.connectionID = connectionID | ||
self.transientError = error as? NWError | ||
} | ||
} | ||
|
||
func testWaitingHandlerInvokesWaitingForConnectivity() { | ||
let requester = MockRequester() | ||
let connectionID: AsyncHTTPClient.HTTPConnectionPool.Connection.ID = 1 | ||
let waitingEventHandler = NWWaitingHandler(requester: requester, connectionID: connectionID) | ||
let embedded = EmbeddedChannel(handlers: [waitingEventHandler]) | ||
|
||
embedded.pipeline.fireUserInboundEventTriggered(NIOTSNetworkEvents.WaitingForConnectivity(transientError: .dns(1))) | ||
|
||
XCTAssertTrue(requester.waitingForConnectivityCalled, "Expected the handler to invoke .waitingForConnectivity on the requester") | ||
XCTAssertEqual(requester.connectionID, connectionID, "Expected the handler to pass connectionID to requester") | ||
XCTAssertEqual(requester.transientError, NWError.dns(1)) | ||
} | ||
|
||
func testWaitingHandlerDoesNotInvokeWaitingForConnectionOnUnrelatedErrors() { | ||
let requester = MockRequester() | ||
let waitingEventHandler = NWWaitingHandler(requester: requester, connectionID: 1) | ||
let embedded = EmbeddedChannel(handlers: [waitingEventHandler]) | ||
embedded.pipeline.fireUserInboundEventTriggered(NIOTSNetworkEvents.BetterPathAvailable()) | ||
|
||
XCTAssertFalse(requester.waitingForConnectivityCalled, "Should not call .waitingForConnectitivy on unrelated events") | ||
} | ||
|
||
func testWaitingHandlerPassesTheEventDownTheContext() { | ||
let requester = MockRequester() | ||
let waitingEventHandler = NWWaitingHandler(requester: requester, connectionID: 1) | ||
let tlsEventsHandler = TLSEventsHandler(deadline: nil) | ||
let embedded = EmbeddedChannel(handlers: [waitingEventHandler, tlsEventsHandler]) | ||
|
||
embedded.pipeline.fireErrorCaught(NIOSSLError.handshakeFailed(BoringSSLError.wantConnect)) | ||
XCTAssertThrowsError(try XCTUnwrap(tlsEventsHandler.tlsEstablishedFuture).wait()) { | ||
XCTAssertEqual($0 as? NIOSSLError, .handshakeFailed(BoringSSLError.wantConnect)) | ||
natikgadzhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
#endif | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.