Skip to content

Commit a61b31c

Browse files
authored
fix test and a crash when closed (#254)
1 parent 5e3b77b commit a61b31c

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

Sources/AsyncHTTPClient/ConnectionsState.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ extension HTTP1ConnectionProvider {
229229
self.openedConnectionsCount -= 1
230230
return self.processNextWaiter()
231231
case .closed:
232-
assertionFailure("should not happen")
232+
// This can happen in the following scenario: user initiates a connection that will fail to connect,
233+
// user calls `syncShutdown` before we received an error from the bootstrap. In this scenario,
234+
// pool will be `.closed` but connection will be still in the process of being established/failed,
235+
// so then this process finishes, it will get to this point.
233236
return .none
234237
}
235238
}

Tests/AsyncHTTPClientTests/ConnectionPoolTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extension ConnectionPoolTests {
3434
("testAcquireReplace", testAcquireReplace),
3535
("testAcquireWhenUnavailableSpecificEL", testAcquireWhenUnavailableSpecificEL),
3636
("testAcquireWhenClosed", testAcquireWhenClosed),
37+
("testConnectFailedWhenClosed", testConnectFailedWhenClosed),
3738
("testReleaseAliveConnectionEmptyQueue", testReleaseAliveConnectionEmptyQueue),
3839
("testReleaseAliveButClosingConnectionEmptyQueue", testReleaseAliveButClosingConnectionEmptyQueue),
3940
("testReleaseInactiveConnectionEmptyQueue", testReleaseInactiveConnectionEmptyQueue),

Tests/AsyncHTTPClientTests/ConnectionPoolTests.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ class ConnectionPoolTests: XCTestCase {
304304

305305
func testAcquireWhenClosed() {
306306
var state = HTTP1ConnectionProvider.ConnectionsState(eventLoop: self.eventLoop)
307-
var snapshot = state.testsOnly_getInternalState()
308-
snapshot.state = .closed
309-
state.testsOnly_setInternalState(snapshot)
307+
_ = state.close()
310308

311309
XCTAssertFalse(state.enqueue())
312310

@@ -320,6 +318,19 @@ class ConnectionPoolTests: XCTestCase {
320318
}
321319
}
322320

321+
func testConnectFailedWhenClosed() {
322+
var state = HTTP1ConnectionProvider.ConnectionsState(eventLoop: self.eventLoop)
323+
_ = state.close()
324+
325+
let action = state.connectFailed()
326+
switch action {
327+
case .none:
328+
break
329+
default:
330+
XCTFail("Unexpected action: \(action)")
331+
}
332+
}
333+
323334
// MARK: - Release Tests
324335

325336
func testReleaseAliveConnectionEmptyQueue() throws {

Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,18 @@ class HTTPClientInternalTests: XCTestCase {
797797
}
798798

799799
func testUncleanCloseThrows() {
800-
let httpBin = HTTPBin()
800+
let server = NIOHTTP1TestServer(group: self.clientGroup)
801801
defer {
802-
XCTAssertNoThrow(try httpBin.shutdown())
802+
XCTAssertNoThrow(try server.stop())
803803
}
804+
804805
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
805806

806-
_ = httpClient.get(url: "http://localhost:\(httpBin.port)/wait")
807+
_ = httpClient.get(url: "http://localhost:\(server.serverPort)/wait")
808+
809+
XCTAssertNoThrow(try server.readInbound()) // .head
810+
XCTAssertNoThrow(try server.readInbound()) // .end
811+
807812
do {
808813
try httpClient.syncShutdown(requiresCleanClose: true)
809814
XCTFail("There should be an error on shutdown")

0 commit comments

Comments
 (0)