Skip to content

Instantly share code, notes, and snippets.

@aufflick
Created October 10, 2018 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aufflick/b468fa132a506ec2d5f66c37b4e52971 to your computer and use it in GitHub Desktop.
Save aufflick/b468fa132a506ec2d5f66c37b4e52971 to your computer and use it in GitHub Desktop.
testGCDDeterminism.swift
import XCTest
class GCDTestTests: XCTestCase {
func testGCDDeterminism() {
// Just for sanity, confirming *everything* is happening on the serial main thread.
assert(Thread.isMainThread)
for _ in 0..<1_000 {
let expectations = (0..<1_000).map { self.expectation(description: "Expect \($0)") }
for i in 0..<1_000 {
DispatchQueue.main.async {
expectations[i].fulfill()
}
}
wait(for: expectations, timeout: 1, enforceOrder: true)
}
}
}
@aufflick
Copy link
Author

I would expect this to be deterministic, but it fairly quickly throws out of order expectation failures. Am I misunderstanding how GCD scheduling works or is this a bug?

Test Suite 'Selected tests' started at 2018-10-10 11:00:54.974
Test Suite 'GCDTestTests.xctest' started at 2018-10-10 11:00:54.975
Test Suite 'GCDTestTests' started at 2018-10-10 11:00:54.975
Test Case '-[GCDTestTests.GCDTestTests testGCDDeterminism]' started.
/Users/maufflick/Desktop/GCDTest/GCDTestTests/GCDTestTests.swift:19: error: -[GCDTestTests.GCDTestTests testGCDDeterminism] : Failed due to expectation fulfilled in incorrect order.
/Users/maufflick/Desktop/GCDTest/GCDTestTests/GCDTestTests.swift:19: error: -[GCDTestTests.GCDTestTests testGCDDeterminism] : Failed due to expectation fulfilled in incorrect order.
/Users/maufflick/Desktop/GCDTest/GCDTestTests/GCDTestTests.swift:19: error: -[GCDTestTests.GCDTestTests testGCDDeterminism] : Failed due to expectation fulfilled in incorrect order.
/Users/maufflick/Desktop/GCDTest/GCDTestTests/GCDTestTests.swift:19: error: -[GCDTestTests.GCDTestTests testGCDDeterminism] : Failed due to expectation fulfilled in incorrect order.
/Users/maufflick/Desktop/GCDTest/GCDTestTests/GCDTestTests.swift:19: error: -[GCDTestTests.GCDTestTests testGCDDeterminism] : Failed due to expectation fulfilled in incorrect order.
/Users/maufflick/Desktop/GCDTest/GCDTestTests/GCDTestTests.swift:19: error: -[GCDTestTests.GCDTestTests testGCDDeterminism] : Failed due to expectation fulfilled in incorrect order.

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