Skip to content

Commit

Permalink
Add unit tests for NWWaitingHandler, closes #589 (#702)
Browse files Browse the repository at this point in the history
* Add unit tests for NWWaitingHandler

Motivation:

Closes #589. Since we already have a public initializer for
NIOTSNetworkEvents.WaitingForConnectivity, we should add unit tests for
the handler now that it's straightforward.

Modifications:

The tests are in their own file `Tests/NWWaitingHandlerTests.swift`.

* Bump swift-nio-transport-services to 1.13.0

* [email protected]

* Apply suggestions from code review

Co-authored-by: David Nadoba <[email protected]>

---------

Co-authored-by: David Nadoba <[email protected]>
  • Loading branch information
natikgadzhi and dnadoba authored Aug 14, 2023
1 parent 8c90405 commit 16f7e62
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Tests/AsyncHTTPClientTests/NWWaitingHandlerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//===----------------------------------------------------------------------===//
//
// 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 NIOEmbedded
import NIOSSL
import NIOTransportServices
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?
var transientError: NWError?

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 .waitingForConnectivity 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()) {
XCTAssertEqualTypeAndValue($0, NIOSSLError.handshakeFailed(BoringSSLError.wantConnect))
}
}
}

#endif

0 comments on commit 16f7e62

Please sign in to comment.