Skip to content

fix test and a crash when closed #254

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/AsyncHTTPClient/ConnectionsState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ extension HTTP1ConnectionProvider {
self.openedConnectionsCount -= 1
return self.processNextWaiter()
case .closed:
assertionFailure("should not happen")
// This can happen in the following scenario: user initiates a connection that will fail to connect,
// user calls `syncShutdown` before we received an error from the bootstrap. In this scenario,
// pool will be `.closed` but connection will be still in the process of being established/failed,
// so then this process finishes, it will get to this point.
return .none
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension ConnectionPoolTests {
("testAcquireReplace", testAcquireReplace),
("testAcquireWhenUnavailableSpecificEL", testAcquireWhenUnavailableSpecificEL),
("testAcquireWhenClosed", testAcquireWhenClosed),
("testConnectFailedWhenClosed", testConnectFailedWhenClosed),
("testReleaseAliveConnectionEmptyQueue", testReleaseAliveConnectionEmptyQueue),
("testReleaseAliveButClosingConnectionEmptyQueue", testReleaseAliveButClosingConnectionEmptyQueue),
("testReleaseInactiveConnectionEmptyQueue", testReleaseInactiveConnectionEmptyQueue),
Expand Down
17 changes: 14 additions & 3 deletions Tests/AsyncHTTPClientTests/ConnectionPoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,7 @@ class ConnectionPoolTests: XCTestCase {

func testAcquireWhenClosed() {
var state = HTTP1ConnectionProvider.ConnectionsState(eventLoop: self.eventLoop)
var snapshot = state.testsOnly_getInternalState()
snapshot.state = .closed
state.testsOnly_setInternalState(snapshot)
_ = state.close()

XCTAssertFalse(state.enqueue())

Expand All @@ -320,6 +318,19 @@ class ConnectionPoolTests: XCTestCase {
}
}

func testConnectFailedWhenClosed() {
var state = HTTP1ConnectionProvider.ConnectionsState(eventLoop: self.eventLoop)
_ = state.close()

let action = state.connectFailed()
switch action {
case .none:
break
default:
XCTFail("Unexpected action: \(action)")
}
}

// MARK: - Release Tests

func testReleaseAliveConnectionEmptyQueue() throws {
Expand Down
11 changes: 8 additions & 3 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -797,13 +797,18 @@ class HTTPClientInternalTests: XCTestCase {
}

func testUncleanCloseThrows() {
let httpBin = HTTPBin()
let server = NIOHTTP1TestServer(group: self.clientGroup)
defer {
XCTAssertNoThrow(try httpBin.shutdown())
XCTAssertNoThrow(try server.stop())
}

let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))

_ = httpClient.get(url: "http://localhost:\(httpBin.port)/wait")
_ = httpClient.get(url: "http://localhost:\(server.serverPort)/wait")

XCTAssertNoThrow(try server.readInbound()) // .head
XCTAssertNoThrow(try server.readInbound()) // .end

do {
try httpClient.syncShutdown(requiresCleanClose: true)
XCTFail("There should be an error on shutdown")
Expand Down