-
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
7d4f41f
commit 7bfe8d6
Showing
3 changed files
with
109 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 currentFetchTask: Task<(), Never>? { | ||
didSet { | ||
if let oldTask = oldValue { | ||
oldTask.cancel() | ||
} | ||
} | ||
} | ||
public func awaitReconciliation() async { | ||
while let task = self.currentFetchTask { | ||
// 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.currentFetchTask == 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,67 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import Confidence | ||
|
||
class TaskManagerTests: XCTestCase { | ||
func testAwaitReconciliationCancelTask() async throws { | ||
var signal1 = false | ||
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) | ||
signal1 = true | ||
} catch { | ||
cancelTaskExpectation.fulfill() | ||
} | ||
} | ||
taskManager.currentFetchTask = 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) | ||
XCTAssertEqual(signal1, false) | ||
} | ||
|
||
func testOverrideTask() async throws { | ||
var signal1 = false | ||
var signal2 = false | ||
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) | ||
signal1 = true | ||
} catch { | ||
cancelTaskExpectation.fulfill() | ||
} | ||
} | ||
taskManager.currentFetchTask = tenSeconds1 | ||
// Ensures the currentTask is set and has started | ||
try await Task.sleep(nanoseconds: 100_000_000) | ||
let tenSeconds2 = Task { | ||
signal2 = true | ||
secondTaskExpectation.fulfill() | ||
} | ||
taskManager.currentFetchTask = 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) | ||
XCTAssertEqual(signal1, false) | ||
XCTAssertEqual(signal2, true) | ||
} | ||
} |