Skip to content

Commit 045950e

Browse files
committed
Polling confirmations: Handle extremely large polling iterations
1 parent 5e76bf1 commit 045950e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Sources/Testing/Polling/Polling.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ private struct Poller {
384384
precondition(interval > Duration.zero)
385385
precondition(duration > interval)
386386

387-
let iterations = max(Int(duration.seconds() / interval.seconds()), 1)
387+
let iterations = Int(exactly:
388+
max(duration.seconds() / interval.seconds(), 1).rounded()
389+
) ?? Int.max
390+
// if Int(exactly:) returns nil, then that generally means the value is too
391+
// large. In which case, we should fall back to Int.max.
388392

389393
let failureReason: PollingFailureReason
390394
switch await poll(iterations: iterations, expression: body) {

Tests/TestingTests/PollingTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ struct PollingConfirmationTests {
210210
) { true }
211211
}
212212
}
213+
214+
@available(_clockAPI, *)
215+
@Test("Handles extremely large polling iterations")
216+
func handlesLargePollingIterations() async throws {
217+
await #expect(processExitsWith: .success) {
218+
try await confirmation(
219+
until: .firstPass,
220+
within: .seconds(Int.max),
221+
pollingEvery: .nanoseconds(1)
222+
) { true }
223+
}
224+
}
213225
#endif
214226
}
215227
}

0 commit comments

Comments
 (0)