@@ -71,7 +71,7 @@ public func confirmPassesEventually(
7171 comment: comment,
7272 sourceLocation: sourceLocation
7373 )
74- await poller. evaluate ( isolation: isolation) {
74+ await poller. evaluateBool ( isolation: isolation) {
7575 do {
7676 return try await body ( )
7777 } catch {
@@ -136,7 +136,7 @@ public func requirePassesEventually(
136136 comment: comment,
137137 sourceLocation: sourceLocation
138138 )
139- let passed = await poller. evaluate ( raiseIssue: false , isolation: isolation) {
139+ let passed = await poller. evaluateBool ( raiseIssue: false , isolation: isolation) {
140140 do {
141141 return try await body ( )
142142 } catch {
@@ -272,7 +272,7 @@ public func confirmAlwaysPasses(
272272 comment: comment,
273273 sourceLocation: sourceLocation
274274 )
275- await poller. evaluate ( isolation: isolation) {
275+ await poller. evaluateBool ( isolation: isolation) {
276276 do {
277277 return try await body ( )
278278 } catch {
@@ -335,7 +335,7 @@ public func requireAlwaysPasses(
335335 comment: comment,
336336 sourceLocation: sourceLocation
337337 )
338- let passed = await poller. evaluate ( raiseIssue: false , isolation: isolation) {
338+ let passed = await poller. evaluateBool ( raiseIssue: false , isolation: isolation) {
339339 do {
340340 return try await body ( )
341341 } catch {
@@ -490,61 +490,19 @@ private struct Poller {
490490 ///
491491 /// - Side effects: If polling fails (see `PollingBehavior`), then this will
492492 /// record an issue.
493- @discardableResult func evaluate (
493+ @discardableResult func evaluateBool (
494494 raiseIssue: Bool = true ,
495495 isolation: isolated ( any Actor ) ? ,
496496 _ body: @escaping ( ) async -> Bool
497497 ) async -> Bool {
498- precondition ( pollingIterations > 0 )
499- precondition ( pollingInterval > Duration . zero)
500- let result = await poll (
501- expression: body
502- )
503- if let issue = result. issue (
504- comment: comment,
505- sourceContext: . init( backtrace: . current( ) , sourceLocation: sourceLocation) ,
506- pollingBehavior: pollingBehavior
507- ) {
508- if raiseIssue {
509- issue. record ( )
510- }
511- return false
512- } else {
513- return true
514- }
515- }
516-
517- /// This function contains the logic for continuously polling an expression,
518- /// as well as processing the results of that expression
519- ///
520- /// - Parameters:
521- /// - expression: An expression to continuously evaluate
522- /// - behavior: The polling behavior to use
523- /// - timeout: How long to poll for unitl the timeout triggers.
524- /// - Returns: The result of this polling.
525- private func poll(
526- isolation: isolated ( any Actor ) ? = #isolation,
527- expression: @escaping ( ) async -> Bool
528- ) async -> PollResult {
529- for iteration in 0 ..< pollingIterations {
530- if let result = await pollingBehavior. processFinishedExpression (
531- expressionResult: expression ( )
532- ) {
533- return result
534- }
535- if iteration == ( pollingIterations - 1 ) {
536- // don't bother sleeping if it's the last iteration.
537- break
538- }
539- do {
540- try await Task . sleep ( for: pollingInterval)
541- } catch {
542- // `Task.sleep` should only throw an error if it's cancelled
543- // during the sleep period.
544- return . cancelled
498+ await evaluate ( raiseIssue: raiseIssue, isolation: isolation) {
499+ if await body ( ) {
500+ // return any non-nil value.
501+ return true
502+ } else {
503+ return nil
545504 }
546- }
547- return . ranToCompletion
505+ } != nil
548506 }
549507
550508 /// Evaluate polling, and process the result, raising an issue if necessary.
@@ -603,8 +561,9 @@ private struct Poller {
603561 isolation: isolated ( any Actor ) ? = #isolation,
604562 expression: @escaping ( ) async -> sending R?
605563 ) async -> ( PollResult , R ? ) {
564+ var lastResult : R ?
606565 for iteration in 0 ..< pollingIterations {
607- let lastResult = await expression ( )
566+ lastResult = await expression ( )
608567 if let result = pollingBehavior. processFinishedExpression (
609568 expressionResult: lastResult != nil
610569 ) {
@@ -622,6 +581,6 @@ private struct Poller {
622581 return ( . cancelled, nil )
623582 }
624583 }
625- return ( . ranToCompletion, nil )
584+ return ( . ranToCompletion, lastResult )
626585 }
627586}
0 commit comments