-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Utilize the RunLoop as a way to avoid database deadlocks. * Fix formatting in DateExtensionTests.
- Loading branch information
Showing
5 changed files
with
243 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// RunLoopExtension.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2021 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
extension RunLoop { | ||
|
||
final class ResumeCondition { | ||
|
||
private let lock = NSLock() | ||
private var receivePorts = [Port]() | ||
|
||
private var _isResolved = false | ||
var isResolved: Bool { | ||
lock.lock() | ||
defer { lock.unlock() } | ||
return _isResolved | ||
} | ||
|
||
init() { | ||
} | ||
|
||
convenience init(dispatchGroup: DispatchGroup) { | ||
self.init() | ||
dispatchGroup.notify(queue: .main) { | ||
self.resolve() | ||
} | ||
} | ||
|
||
func addPort(to runLoop: RunLoop, forMode mode: RunLoop.Mode) -> Port? { | ||
lock.lock() | ||
defer { lock.unlock() } | ||
guard !_isResolved else { return nil } | ||
|
||
let port = Port() | ||
receivePorts.append(port) | ||
runLoop.add(port, forMode: mode) | ||
|
||
return port | ||
} | ||
|
||
func resolve(mode: RunLoop.Mode = .default) { | ||
lock.lock() | ||
|
||
assert(!_isResolved) | ||
_isResolved = true | ||
|
||
let ports = receivePorts | ||
|
||
lock.unlock() | ||
|
||
let sendPort = Port() | ||
RunLoop.current.add(sendPort, forMode: mode) | ||
|
||
// Send Wake message from current RunLoop port to each running RunLoop | ||
for receivePort in ports { | ||
receivePort.send(before: Date(), components: nil, from: sendPort, reserved: 0) | ||
} | ||
|
||
RunLoop.current.remove(sendPort, forMode: mode) | ||
} | ||
|
||
} | ||
|
||
func run(mode: RunLoop.Mode = .default, until condition: ResumeCondition) { | ||
// Add port to current RunLoop to receive Wake message | ||
guard let port = condition.addPort(to: self, forMode: mode) else { | ||
// already resolved | ||
return | ||
} | ||
|
||
while !condition.isResolved { | ||
self.run(mode: mode, before: .distantFuture) | ||
} | ||
self.remove(port, forMode: mode) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// RunLoopExtensionTests.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2021 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import Core | ||
|
||
class RunLoopExtensionTests: XCTestCase { | ||
|
||
func testWhenConditionResolvedThenNoWaitIsPerformed() { | ||
let condition = RunLoop.ResumeCondition() | ||
condition.resolve() | ||
|
||
let e = expectation(description: "should execute after wait") | ||
var isExecuted = false | ||
RunLoop.current.perform { | ||
isExecuted = true | ||
e.fulfill() | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
XCTAssertFalse(isExecuted) | ||
|
||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testWhenConditionIsResolvedThenWaitIsFinished() { | ||
let condition = RunLoop.ResumeCondition() | ||
|
||
let e = expectation(description: "should execute") | ||
RunLoop.current.perform { | ||
condition.resolve() | ||
e.fulfill() | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
waitForExpectations(timeout: 0) | ||
} | ||
|
||
func testWhenDispatchGroupIsEmptyThenNoWaitIsPerformed() { | ||
let dispatchGroup = DispatchGroup() | ||
let condition = RunLoop.ResumeCondition(dispatchGroup: dispatchGroup) | ||
|
||
let e = expectation(description: "should execute") | ||
RunLoop.current.perform { | ||
e.fulfill() | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
waitForExpectations(timeout: 0) | ||
} | ||
|
||
func testWhenDispatchGroupIsCompleteThenWaitIsFinished() { | ||
let dispatchGroup = DispatchGroup() | ||
let condition = RunLoop.ResumeCondition(dispatchGroup: dispatchGroup) | ||
|
||
let e = expectation(description: "should execute") | ||
RunLoop.current.perform { | ||
e.fulfill() | ||
} | ||
|
||
for _ in 0..<3 { | ||
dispatchGroup.enter() | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 0.05) { | ||
dispatchGroup.leave() | ||
} | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
waitForExpectations(timeout: 0) | ||
} | ||
|
||
func testWhenNestedWaitIsCalledThenWaitIsPerformed() { | ||
let condition = RunLoop.ResumeCondition() | ||
|
||
let e = expectation(description: "should execute") | ||
RunLoop.current.perform { | ||
RunLoop.current.perform { | ||
condition.resolve() | ||
e.fulfill() | ||
} | ||
RunLoop.current.run(until: condition) | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
waitForExpectations(timeout: 0) | ||
} | ||
|
||
func testWhenResolveFromBackgroundThreadThenWaitIsFinished() { | ||
let condition = RunLoop.ResumeCondition() | ||
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 0.05) { | ||
condition.resolve() | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
} | ||
|
||
func testWhenWaitingInBackgroundThreadThenWaitIsFinishedWhenResolved() { | ||
let condition = RunLoop.ResumeCondition() | ||
|
||
let e = expectation(description: "should execute") | ||
DispatchQueue.global().async { | ||
RunLoop.current.run(until: condition) | ||
e.fulfill() | ||
} | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 0.05) { | ||
condition.resolve() | ||
} | ||
|
||
RunLoop.current.run(until: condition) | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
} |