Skip to content

Parity: RunLoop.add(Port…)/.remove(Port…) #2469

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
Aug 15, 2019
Merged
Show file tree
Hide file tree
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
57 changes: 54 additions & 3 deletions Foundation/RunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,66 @@ open class RunLoop: NSObject {
}

open func add(_ timer: Timer, forMode mode: RunLoop.Mode) {
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer._cfObject, mode._cfStringUniquingKnown)
CFRunLoopAddTimer(_cfRunLoop, timer._cfObject, mode._cfStringUniquingKnown)
}

private let monitoredPortsWithModesLock = NSLock() // guards:
private var monitoredPortsWithModes: [Port: Set<RunLoop.Mode>] = [:]
private var monitoredPortObservers: [Port: NSObjectProtocol] = [:]

open func add(_ aPort: Port, forMode mode: RunLoop.Mode) {
NSUnimplemented()
var shouldSchedule = false
monitoredPortsWithModesLock.synchronized {
if monitoredPortsWithModes[aPort]?.contains(mode) != true {
monitoredPortsWithModes[aPort, default: []].insert(mode)

let shouldStartMonitoring = monitoredPortObservers[aPort] == nil
if shouldStartMonitoring {
monitoredPortObservers[aPort] = NotificationCenter.default.addObserver(forName: Port.didBecomeInvalidNotification, object: aPort, queue: nil, using: { [weak self] (notification) in
self?.portDidInvalidate(aPort)
})
}

shouldSchedule = true
}
}

if shouldSchedule {
aPort.schedule(in: self, forMode: mode)
}
}

private func portDidInvalidate(_ aPort: Port) {
monitoredPortsWithModesLock.synchronized {
if let observer = monitoredPortObservers.removeValue(forKey: aPort) {
NotificationCenter.default.removeObserver(observer)
}
monitoredPortsWithModes.removeValue(forKey: aPort)
}
}

open func remove(_ aPort: Port, forMode mode: RunLoop.Mode) {
NSUnimplemented()
var shouldRemove = false
monitoredPortsWithModesLock.synchronized {
guard let modes = monitoredPortsWithModes[aPort], modes.contains(mode) else {
return
}

shouldRemove = true

if modes.count == 1 {
if let observer = monitoredPortObservers.removeValue(forKey: aPort) {
NotificationCenter.default.removeObserver(observer)
}
monitoredPortsWithModes.removeValue(forKey: aPort)
} else {
monitoredPortsWithModes[aPort]?.remove(mode)
}
}

if shouldRemove {
aPort.remove(from: self, forMode: mode)
}
}

open func limitDate(forMode mode: RunLoop.Mode) -> Date? {
Expand Down
89 changes: 78 additions & 11 deletions TestFoundation/TestRunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
//

class TestRunLoop : XCTestCase {
static var allTests : [(String, (TestRunLoop) -> () throws -> Void)] {
return [
("test_constants", test_constants),
("test_runLoopInit", test_runLoopInit),
("test_commonModes", test_commonModes),
// these tests do not work the same as Darwin https://bugs.swift.org/browse/SR-399
// ("test_runLoopRunMode", test_runLoopRunMode),
// ("test_runLoopLimitDate", test_runLoopLimitDate),
]
}

func test_constants() {
XCTAssertEqual(RunLoop.Mode.common.rawValue, "kCFRunLoopCommonModes",
"\(RunLoop.Mode.common.rawValue) is not equal to kCFRunLoopCommonModes")
Expand Down Expand Up @@ -92,4 +81,82 @@ class TestRunLoop : XCTestCase {

waitForExpectations(timeout: 10)
}

func test_addingRemovingPorts() {
let runLoop = RunLoop.current
var didDeallocate = false

do {
let port = TestPort {
didDeallocate = true
}
let customMode = RunLoop.Mode(rawValue: "Custom")

XCTAssertEqual(port.scheduledModes, [])

runLoop.add(port, forMode: .default)
XCTAssertEqual(port.scheduledModes, [.default])

runLoop.add(port, forMode: .default)
XCTAssertEqual(port.scheduledModes, [.default])

runLoop.add(port, forMode: customMode)
XCTAssertEqual(port.scheduledModes, [.default, customMode])

runLoop.remove(port, forMode: customMode)
XCTAssertEqual(port.scheduledModes, [.default])

runLoop.add(port, forMode: customMode)
XCTAssertEqual(port.scheduledModes, [.default, customMode])

port.invalidate()
}

XCTAssertTrue(didDeallocate)
}

static var allTests : [(String, (TestRunLoop) -> () throws -> Void)] {
return [
("test_constants", test_constants),
("test_runLoopInit", test_runLoopInit),
("test_commonModes", test_commonModes),
// these tests do not work the same as Darwin https://bugs.swift.org/browse/SR-399
// ("test_runLoopRunMode", test_runLoopRunMode),
// ("test_runLoopLimitDate", test_runLoopLimitDate),
("test_addingRemovingPorts", test_addingRemovingPorts),
]
}
}

class TestPort: Port {
let sentinel: () -> Void
init(sentinel: @escaping () -> Void) {
self.sentinel = sentinel
super.init()
}

deinit {
invalidate()
sentinel()
}

private var _isValid = true
open override var isValid: Bool { return _isValid }

open override func invalidate() {
guard isValid else { return }

_isValid = false
NotificationCenter.default.post(name: Port.didBecomeInvalidNotification, object: self)
}

var scheduledModes: [RunLoop.Mode] = []

open override func schedule(in runLoop: RunLoop, forMode mode: RunLoop.Mode) {
scheduledModes.append(mode)
}

open override func remove(from runLoop: RunLoop, forMode mode: RunLoop.Mode) {
scheduledModes = scheduledModes.filter { $0 != mode }
}
}