Skip to content

Commit 16f7e62

Browse files
natikgadzhidnadoba
andauthored
Add unit tests for NWWaitingHandler, closes #589 (#702)
* 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]>
1 parent 8c90405 commit 16f7e62

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the AsyncHTTPClient 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 AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#if canImport(Network)
16+
@testable import AsyncHTTPClient
17+
import Network
18+
import NIOCore
19+
import NIOEmbedded
20+
import NIOSSL
21+
import NIOTransportServices
22+
import XCTest
23+
24+
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
25+
class NWWaitingHandlerTests: XCTestCase {
26+
class MockRequester: HTTPConnectionRequester {
27+
var waitingForConnectivityCalled = false
28+
var connectionID: AsyncHTTPClient.HTTPConnectionPool.Connection.ID?
29+
var transientError: NWError?
30+
31+
func http1ConnectionCreated(_: AsyncHTTPClient.HTTP1Connection) {}
32+
33+
func http2ConnectionCreated(_: AsyncHTTPClient.HTTP2Connection, maximumStreams: Int) {}
34+
35+
func failedToCreateHTTPConnection(_: AsyncHTTPClient.HTTPConnectionPool.Connection.ID, error: Error) {}
36+
37+
func waitingForConnectivity(_ connectionID: AsyncHTTPClient.HTTPConnectionPool.Connection.ID, error: Error) {
38+
self.waitingForConnectivityCalled = true
39+
self.connectionID = connectionID
40+
self.transientError = error as? NWError
41+
}
42+
}
43+
44+
func testWaitingHandlerInvokesWaitingForConnectivity() {
45+
let requester = MockRequester()
46+
let connectionID: AsyncHTTPClient.HTTPConnectionPool.Connection.ID = 1
47+
let waitingEventHandler = NWWaitingHandler(requester: requester, connectionID: connectionID)
48+
let embedded = EmbeddedChannel(handlers: [waitingEventHandler])
49+
50+
embedded.pipeline.fireUserInboundEventTriggered(NIOTSNetworkEvents.WaitingForConnectivity(transientError: .dns(1)))
51+
52+
XCTAssertTrue(requester.waitingForConnectivityCalled, "Expected the handler to invoke .waitingForConnectivity on the requester")
53+
XCTAssertEqual(requester.connectionID, connectionID, "Expected the handler to pass connectionID to requester")
54+
XCTAssertEqual(requester.transientError, NWError.dns(1))
55+
}
56+
57+
func testWaitingHandlerDoesNotInvokeWaitingForConnectionOnUnrelatedErrors() {
58+
let requester = MockRequester()
59+
let waitingEventHandler = NWWaitingHandler(requester: requester, connectionID: 1)
60+
let embedded = EmbeddedChannel(handlers: [waitingEventHandler])
61+
embedded.pipeline.fireUserInboundEventTriggered(NIOTSNetworkEvents.BetterPathAvailable())
62+
63+
XCTAssertFalse(requester.waitingForConnectivityCalled, "Should not call .waitingForConnectivity on unrelated events")
64+
}
65+
66+
func testWaitingHandlerPassesTheEventDownTheContext() {
67+
let requester = MockRequester()
68+
let waitingEventHandler = NWWaitingHandler(requester: requester, connectionID: 1)
69+
let tlsEventsHandler = TLSEventsHandler(deadline: nil)
70+
let embedded = EmbeddedChannel(handlers: [waitingEventHandler, tlsEventsHandler])
71+
72+
embedded.pipeline.fireErrorCaught(NIOSSLError.handshakeFailed(BoringSSLError.wantConnect))
73+
XCTAssertThrowsError(try XCTUnwrap(tlsEventsHandler.tlsEstablishedFuture).wait()) {
74+
XCTAssertEqualTypeAndValue($0, NIOSSLError.handshakeFailed(BoringSSLError.wantConnect))
75+
}
76+
}
77+
}
78+
79+
#endif

0 commit comments

Comments
 (0)