Skip to content

Add test for main dispatch queue processing in CFRunLoop #2806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Tests/Foundation/Tests/TestRunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,41 @@ class TestRunLoop : XCTestCase {
XCTAssertTrue(didDeallocate)
}

func test_mainDispatchQueueCallout() {
let runLoop = RunLoop.current

var asyncExecuted = false
DispatchQueue.main.async {
asyncExecuted = true
}

// RunLoop should service main queue
_ = runLoop.run(mode: .default, before: Date(timeIntervalSinceNow: 2))
XCTAssertTrue(asyncExecuted, "Main queue async code should be executed")

asyncExecuted = false
DispatchQueue.main.async {
asyncExecuted = true
}

// Second run to be sure RunLoop will not stuck
_ = runLoop.run(mode: .default, before: Date(timeIntervalSinceNow: 2))
XCTAssertTrue(asyncExecuted, "Main queue async code should be executed")

var timerFired = false
let dummyTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
timerFired = true
}
runLoop.add(dummyTimer, forMode: .default)

// At this moment RunLoop has no work to do except waiting for timer.
// But RunLoop will exit prematurely if event from previous async calls
// got stuck in wrong state.
_ = runLoop.run(mode: .default, before: Date(timeIntervalSinceNow: 2))

XCTAssertTrue(timerFired, "Time should fire already")
}

static var allTests : [(String, (TestRunLoop) -> () throws -> Void)] {
return [
("test_constants", test_constants),
Expand All @@ -134,6 +169,7 @@ class TestRunLoop : XCTestCase {
("test_runLoopLimitDate", test_runLoopLimitDate),
("test_runLoopPoll", test_runLoopPoll),
("test_addingRemovingPorts", test_addingRemovingPorts),
("test_mainDispatchQueueCallout", test_mainDispatchQueueCallout)
]
}
}
Expand Down