-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: TaskManager refactor and tests
- Loading branch information
1 parent
d6b4c9d
commit a7ab0f3
Showing
3 changed files
with
138 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Foundation | ||
|
||
class TaskManager { | ||
public var currentTask: Task<(), Never>? { | ||
didSet { | ||
if let oldTask = oldValue { | ||
oldTask.cancel() | ||
} | ||
} | ||
} | ||
public func awaitReconciliation() async { | ||
while let task = self.currentTask { | ||
// If current task is cancelled, return | ||
if task.isCancelled { | ||
return | ||
} | ||
// Wait for result of current task | ||
await task.value | ||
// If current task gets cancelled, check again if a new task was set | ||
if task.isCancelled { | ||
continue | ||
} | ||
// If current task finished successfully | ||
// and the set task has not changed, we are done waiting | ||
if self.currentTask == task { | ||
return | ||
} | ||
} | ||
} | ||
} |
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,96 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import Confidence | ||
|
||
class TaskManagerTests: XCTestCase { | ||
func testAwaitReconciliationCancelTask() async throws { | ||
let signalManager = SignalManager() | ||
let reconciliationExpectation = XCTestExpectation(description: "reconciliationExpectation") | ||
let cancelTaskExpectation = XCTestExpectation(description: "cancelTaskExpectation") | ||
let taskManager = TaskManager() | ||
|
||
let tenSeconds = Task { | ||
do { | ||
try await Task.sleep(nanoseconds: 10_000_000_000) | ||
await signalManager.setSignal1(true) | ||
} catch { | ||
cancelTaskExpectation.fulfill() | ||
} | ||
} | ||
taskManager.currentTask = tenSeconds | ||
Task { | ||
await taskManager.awaitReconciliation() | ||
reconciliationExpectation.fulfill() | ||
} | ||
// Ensures the currentTask is set and has started | ||
try await Task.sleep(nanoseconds: 100_000_000) | ||
tenSeconds.cancel() | ||
await fulfillment(of: [cancelTaskExpectation, reconciliationExpectation], timeout: 1) | ||
|
||
let finalSignal1 = await signalManager.getSignal1() | ||
|
||
XCTAssertEqual(finalSignal1, false) | ||
} | ||
|
||
func testOverrideTask() async throws { | ||
let signalManager = SignalManager() | ||
let reconciliationExpectation = XCTestExpectation(description: "reconciliationExpectation") | ||
let cancelTaskExpectation = XCTestExpectation(description: "cancelTaskExpectation") | ||
let secondTaskExpectation = XCTestExpectation(description: "secondTaskExpectation") | ||
let taskManager = TaskManager() | ||
|
||
let tenSeconds1 = Task { | ||
do { | ||
try await Task.sleep(nanoseconds: 10_000_000_000) | ||
await signalManager.setSignal1(true) | ||
} catch { | ||
cancelTaskExpectation.fulfill() | ||
} | ||
} | ||
taskManager.currentTask = tenSeconds1 | ||
// Ensures the currentTask is set and has started | ||
try await Task.sleep(nanoseconds: 100_000_000) | ||
|
||
let tenSeconds2 = Task { | ||
await signalManager.setSignal2(true) | ||
secondTaskExpectation.fulfill() | ||
} | ||
taskManager.currentTask = tenSeconds2 | ||
|
||
// Ensures the currentTask is set and has started | ||
try await Task.sleep(nanoseconds: 100_000_000) | ||
Task { | ||
await taskManager.awaitReconciliation() | ||
reconciliationExpectation.fulfill() | ||
} | ||
await fulfillment(of: [cancelTaskExpectation, reconciliationExpectation, secondTaskExpectation], timeout: 1) | ||
|
||
let finalSignal1 = await signalManager.getSignal1() | ||
let finalSignal2 = await signalManager.getSignal2() | ||
|
||
XCTAssertEqual(finalSignal1, false) | ||
XCTAssertEqual(finalSignal2, true) | ||
} | ||
|
||
private actor SignalManager { | ||
private var _signal1 = false | ||
private var _signal2 = false | ||
|
||
// Functions to access and mutate `signal1` and `signal2` | ||
func setSignal1(_ value: Bool) { | ||
_signal1 = value | ||
} | ||
|
||
func setSignal2(_ value: Bool) { | ||
_signal2 = value | ||
} | ||
|
||
func getSignal1() -> Bool { | ||
return _signal1 | ||
} | ||
|
||
func getSignal2() -> Bool { | ||
return _signal2 | ||
} | ||
} | ||
} |