@@ -71,7 +71,7 @@ public func confirmPassesEventually(
71
71
comment: comment,
72
72
sourceLocation: sourceLocation
73
73
)
74
- await poller. evaluate ( isolation: isolation) {
74
+ await poller. evaluateBool ( isolation: isolation) {
75
75
do {
76
76
return try await body ( )
77
77
} catch {
@@ -136,7 +136,7 @@ public func requirePassesEventually(
136
136
comment: comment,
137
137
sourceLocation: sourceLocation
138
138
)
139
- let passed = await poller. evaluate ( raiseIssue: false , isolation: isolation) {
139
+ let passed = await poller. evaluateBool ( raiseIssue: false , isolation: isolation) {
140
140
do {
141
141
return try await body ( )
142
142
} catch {
@@ -272,7 +272,7 @@ public func confirmAlwaysPasses(
272
272
comment: comment,
273
273
sourceLocation: sourceLocation
274
274
)
275
- await poller. evaluate ( isolation: isolation) {
275
+ await poller. evaluateBool ( isolation: isolation) {
276
276
do {
277
277
return try await body ( )
278
278
} catch {
@@ -335,7 +335,7 @@ public func requireAlwaysPasses(
335
335
comment: comment,
336
336
sourceLocation: sourceLocation
337
337
)
338
- let passed = await poller. evaluate ( raiseIssue: false , isolation: isolation) {
338
+ let passed = await poller. evaluateBool ( raiseIssue: false , isolation: isolation) {
339
339
do {
340
340
return try await body ( )
341
341
} catch {
@@ -490,61 +490,19 @@ private struct Poller {
490
490
///
491
491
/// - Side effects: If polling fails (see `PollingBehavior`), then this will
492
492
/// record an issue.
493
- @discardableResult func evaluate (
493
+ @discardableResult func evaluateBool (
494
494
raiseIssue: Bool = true ,
495
495
isolation: isolated ( any Actor ) ? ,
496
496
_ body: @escaping ( ) async -> Bool
497
497
) 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
545
504
}
546
- }
547
- return . ranToCompletion
505
+ } != nil
548
506
}
549
507
550
508
/// Evaluate polling, and process the result, raising an issue if necessary.
@@ -603,8 +561,9 @@ private struct Poller {
603
561
isolation: isolated ( any Actor ) ? = #isolation,
604
562
expression: @escaping ( ) async -> sending R?
605
563
) async -> ( PollResult , R ? ) {
564
+ var lastResult : R ?
606
565
for iteration in 0 ..< pollingIterations {
607
- let lastResult = await expression ( )
566
+ lastResult = await expression ( )
608
567
if let result = pollingBehavior. processFinishedExpression (
609
568
expressionResult: lastResult != nil
610
569
) {
@@ -622,6 +581,6 @@ private struct Poller {
622
581
return ( . cancelled, nil )
623
582
}
624
583
}
625
- return ( . ranToCompletion, nil )
584
+ return ( . ranToCompletion, lastResult )
626
585
}
627
586
}
0 commit comments