Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@younata
Copy link
Member

@younata younata commented Sep 24, 2025

This trades wall-clock time accuracy for reliability. It's nowhere near as important for a polling expectation to finish within the specified duration as it is to actually poll as expected.

Basically, under heavy system loads, the actual gap between polling attempts increases. Under sufficiently heavy loads, with a sufficiently low timeout, we might never have polled in the first place.

Instead, this calculate how many times we should poll by dividing the timeout by the interval. We then poll exactly that many times, sleeping for however long the polling interval is.

If polling interval is ever 0, then we set it to 1 nanosecond.

WaitUntil is also re-implemented in both async and sync, but that's because the older infrastructure for doing polling was replaced. Starting in Swift 6.3, we will emit a warning when WaitUntil is used in Swift Testing.

This change also removes the ability to detect a blocked runloop, so Nimble will no longer detect for that. In my opinion, this is a worthwhile tradeoff, as blocked runloop was a source of test flakiness.

This isn't changing any public-facing code, aside from adding deprecation warnings. It still counts as a major release.

  • Does this have tests?

Fixes #1095, though that's a happy consequence of this.

@younata younata force-pushed the make-polling-expectations-more-reliable branch from f7f6ff1 to 0aa5b7f Compare October 1, 2025 16:23
…vy system loads

This trades wall-clock time accuracy for reliability. It's nowhere near as important for a polling expectation to finish within the specified duration as it is to actually poll as expected.

Basically, under heavy system loads, the actual gap between polling attempts increases. Under sufficiently heavy loads, with a sufficiently low timeout, we might never have polled in the first place.

Instead, this calculate how many times we should poll by dividing the timeout by the interval. We then poll exactly that many times, sleeping for however long the polling interval is.

If polling interval is ever 0, then we set it to 1 nanosecond.

WaitUntil is also re-implemented in both async and sync, but that's because the older infrastructure for doing polling was replaced. Starting in Swift 6.3, we will emit a warning when WaitUntil is used in Swift Testing.

This change also removes the ability to detect a blocked runloop, so Nimble will no longer detect for that. In my opinion, this is a worthwhile tradeoff, as blocked runloop was a source of test flakiness.
@younata younata force-pushed the make-polling-expectations-more-reliable branch from 0aa5b7f to a265638 Compare October 3, 2025 20:37
@younata younata force-pushed the make-polling-expectations-more-reliable branch from b1f97b6 to afa847d Compare October 4, 2025 04:43
@younata younata merged commit 5a0cc37 into main Oct 4, 2025
12 of 13 checks passed
@younata younata deleted the make-polling-expectations-more-reliable branch October 4, 2025 05:36
@tahirmt
Copy link

tahirmt commented Oct 10, 2025

Any chance we can get a release published since this change was merged?

@sewerynplazuk
Copy link

sewerynplazuk commented Oct 15, 2025

👋 I tried to use the fix in my project and noticed a few test cases failing on sync waitUntil. Probably regression, it should be reproducible with:

it("nested main queue async calls") {
    waitUntil(timeout: .seconds(5)) { done in
        DispatchQueue.main.async {
            DispatchQueue.main.async {
                DispatchQueue.main.async {
                    print("Calling done()")
                    done()
                }
            }
        }
    }
}

it("operation queue with main underlyingQueue and barrier") {
    waitUntil(timeout: .seconds(5)) { done in
        let operationQueue = OperationQueue()
        operationQueue.underlyingQueue = .main

        let operation = BlockOperation {
            print("Operation executing")
        }

        operationQueue.addOperation(operation)
        operationQueue.addBarrierBlock {
            print("Calling done()")
            done()
        }
    }
}

it("nested main async with operation queue") {
    waitUntil(timeout: .seconds(5)) { done in
        let indexingQueue = DispatchQueue.main
        let operationQueue = OperationQueue()
        operationQueue.underlyingQueue = indexingQueue

        indexingQueue.async {
            indexingQueue.async {
                indexingQueue.async {
                    let operation = BlockOperation {
                        print("Operation executing")
                    }

                    operationQueue.addOperation(operation)
                    operationQueue.addBarrierBlock {
                        print("Calling done()")
                        done()
                    }
                }
            }
        }
    }
}

it("nested background async with operation queue") {
    waitUntil(timeout: .seconds(5)) { done in
        let indexingQueue = DispatchQueue(label: "test.queue")
        let operationQueue = OperationQueue()
        operationQueue.underlyingQueue = indexingQueue

        indexingQueue.async {
            indexingQueue.async {
                indexingQueue.async {
                    let operation = BlockOperation {
                        print("Operation executing")
                    }

                    operationQueue.addOperation(operation)
                    operationQueue.addBarrierBlock {
                        print("Calling done()")
                        done()
                    }
                }
            }
        }
    }
}

In all cases done() is reached almost immediately, however Waited more than X seconds error is raised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

toEventually in Xcode 15 somtimes results in "main run loop was unresponsive"

3 participants