Skip to content

Commit 473c518

Browse files
Trevörweissi
Trevör
andauthored
Remove unneeded assertion (#169)
* Remove activity assertion in prepareForClose motivation: assertion on activity sometimes fails but it turns out it is overly restrictive changes: - remove assertion and accept any given activity in prepareForClose * test closing connections whilst syncShutdown Co-authored-by: Johannes Weiss <[email protected]>
1 parent 7768d23 commit 473c518

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Diff for: Sources/AsyncHTTPClient/ConnectionPool.swift

-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ final class ConnectionPool {
414414
assert(MultiThreadedEventLoopGroup.currentEventLoop == nil,
415415
"HTTPClient shutdown on EventLoop unsupported") // calls .wait() so it would crash later anyway
416416
let (waitersFutures, closeFutures) = self.stateLock.withLock { () -> ([EventLoopFuture<Connection>], [EventLoopFuture<Void>]) in
417-
assert(self.state.activity == .opened, "Invalid activity: \(self.state.activity)")
418417
// Fail waiters
419418
let waitersCopy = self.state.waiters
420419
self.state.waiters.removeAll()

Diff for: Tests/AsyncHTTPClientTests/HTTPClientInternalTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extension HTTPClientInternalTests {
3535
("testChannelAndDelegateOnDifferentEventLoops", testChannelAndDelegateOnDifferentEventLoops),
3636
("testResponseConnectionCloseGet", testResponseConnectionCloseGet),
3737
("testWeNoticeRemoteClosuresEvenWhenConnectionIsIdleInPool", testWeNoticeRemoteClosuresEvenWhenConnectionIsIdleInPool),
38+
("testWeTolerateConnectionsGoingAwayWhilstPoolIsShuttingDown", testWeTolerateConnectionsGoingAwayWhilstPoolIsShuttingDown),
3839
]
3940
}
4041
}

Diff for: Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

+42
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,46 @@ class HTTPClientInternalTests: XCTestCase {
581581
XCTAssertEqual(2, sharedStateServerHandler.connectionNumber.load())
582582
XCTAssertEqual(2, sharedStateServerHandler.requestNumber.load())
583583
}
584+
585+
func testWeTolerateConnectionsGoingAwayWhilstPoolIsShuttingDown() {
586+
struct NoChannelError: Error {}
587+
588+
let client = HTTPClient(eventLoopGroupProvider: .createNew)
589+
var maybeServersAndChannels: [(HTTPBin, Channel)]?
590+
XCTAssertNoThrow(maybeServersAndChannels = try (0..<10).map { _ in
591+
let web = HTTPBin()
592+
defer {
593+
XCTAssertNoThrow(try web.shutdown())
594+
}
595+
596+
let req = try! HTTPClient.Request(url: "http://localhost:\(web.serverChannel.localAddress!.port!)/get",
597+
method: .GET,
598+
body: nil)
599+
var maybeConnection: ConnectionPool.Connection?
600+
XCTAssertNoThrow(try maybeConnection = client.pool.getConnection(for: req,
601+
preference: .indifferent,
602+
on: client.eventLoopGroup.next(),
603+
deadline: nil).wait())
604+
guard let connection = maybeConnection else {
605+
XCTFail("couldn't make connection")
606+
throw NoChannelError()
607+
}
608+
609+
let channel = connection.channel
610+
client.pool.release(connection)
611+
return (web, channel)
612+
})
613+
614+
guard let serversAndChannels = maybeServersAndChannels else {
615+
XCTFail("couldn't open servers")
616+
return
617+
}
618+
619+
DispatchQueue.global().async {
620+
serversAndChannels.forEach { serverAndChannel in
621+
serverAndChannel.1.close(promise: nil)
622+
}
623+
}
624+
XCTAssertNoThrow(try client.syncShutdown())
625+
}
584626
}

0 commit comments

Comments
 (0)