Skip to content

Commit 742e8ec

Browse files
committed
ConnectionPool Waiter should store its timeout task
Motivation: Connection pool waiters have a scheduled tasks for to timeout waiting. This is cancelled and set to `nil` when the waiter is succeeded or failed. Hoever, when scheduling a task the task was never stored! This would lead to the potentially firing when the deadline expired although the effect of this would be a no-op. Modifications: - Capture the waiters scheduled task - Add a test Result: Cancelled tasks are actually cancelled.
1 parent 593fe0f commit 742e8ec

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Sources/GRPC/ConnectionPool/ConnectionPool+Waiter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension ConnectionPool {
7171
execute body: @escaping () -> Void
7272
) {
7373
assert(self._scheduledTimeout == nil)
74-
eventLoop.scheduleTask(deadline: self._deadline, body)
74+
self._scheduledTimeout = eventLoop.scheduleTask(deadline: self._deadline, body)
7575
}
7676

7777
/// Returns a boolean value indicating whether the deadline for this waiter occurs after the

Tests/GRPCTests/ConnectionPool/ConnectionPoolTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,25 @@ final class ConnectionPoolTests: GRPCTestCase {
846846
}
847847
}
848848
}
849+
850+
func testWaiterStoresItsScheduledTask() throws {
851+
let deadline = NIODeadline.uptimeNanoseconds(42)
852+
let promise = self.eventLoop.makePromise(of: Channel.self)
853+
let waiter = ConnectionPool.Waiter(deadline: deadline, promise: promise) {
854+
return $0.eventLoop.makeSucceededVoidFuture()
855+
}
856+
857+
XCTAssertNil(waiter._scheduledTimeout)
858+
859+
waiter.scheduleTimeout(on: self.eventLoop) {
860+
waiter.fail(ConnectionPoolError.deadlineExceeded(connectionError: nil))
861+
}
862+
863+
XCTAssertNotNil(waiter._scheduledTimeout)
864+
self.eventLoop.advanceTime(to: deadline)
865+
XCTAssertThrowsError(try promise.futureResult.wait())
866+
XCTAssertNil(waiter._scheduledTimeout)
867+
}
849868
}
850869

851870
extension ConnectionPool {

0 commit comments

Comments
 (0)