Skip to content

Commit

Permalink
wip: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyamahunt committed Mar 4, 2023
1 parent 4beae68 commit b5824c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 7 additions & 4 deletions Sources/AsyncObjects/TaskOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject, Loggable,
) async throws {
let key = UUID()
log("Waiting", id: key, file: file, function: function, line: line)
for await _ in event { break }
var iter = event.makeAsyncIterator()
await iter.next()

do {
try Task.checkCancellation()
} catch {
Expand All @@ -283,10 +285,11 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject, Loggable,
)
throw error
}
do {
let _ = try await execTask?.value

switch await self.result {
case .success:
log("Finished", id: key, file: file, function: function, line: line)
} catch {
case .failure(let error):
log(
"Finished with error: \(error)", id: key,
file: file, function: function, line: line
Expand Down
17 changes: 14 additions & 3 deletions Tests/AsyncObjectsTests/TaskOperationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ class TaskOperationTests: XCTestCase {
try await operation.wait(forSeconds: 3)
}

func testFinisheAsyncdWait() async throws {
let operation = TaskOperation { /* Do nothing */ }
func testAsyncWaitError() async throws {
let operation = TaskOperation { throw CancellationError() }
operation.signal()
try await operation.wait(forSeconds: 3)
do {
try await operation.wait(forSeconds: 3)
XCTFail("Unexpected task progression")
} catch is CancellationError {}
}

func testDeinit() async throws {
Expand Down Expand Up @@ -265,6 +268,10 @@ class TaskOperationTaskManagementTests: XCTestCase {
XCTAssertFalse(error.localizedDescription.isEmpty)
default: XCTFail("Unexpected operation result")
}
do {
try await operation.wait(forSeconds: 3)
XCTFail("Unexpected task progression")
} catch is DurationTimeoutError {}
}

func testNotStartedCancellationError() async throws {
Expand All @@ -278,6 +285,10 @@ class TaskOperationTaskManagementTests: XCTestCase {
XCTAssertFalse(error.localizedDescription.isEmpty)
default: XCTFail("Unexpected operation result")
}
do {
try await operation.wait(forSeconds: 3)
XCTFail("Unexpected task progression")
} catch is CancellationError {}
}
}

Expand Down

0 comments on commit b5824c4

Please sign in to comment.