Skip to content

Commit 81c2c9c

Browse files
authored
ci: disable OperationQueue tests on non-macos platforms (#5)
* ci: fix test failure * ci: fix rounding off errors for tests * ci: cancel underlying task on deinit of operation * ci: disable `OperationQueue` tests on non-macos platform
1 parent 9a852d9 commit 81c2c9c

7 files changed

Lines changed: 19 additions & 15 deletions

File tree

Sources/AsyncObjects/CancellationSource.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public actor CancellationSource {
131131
/// and propagate cancellation to linked cancellation sources.
132132
///
133133
/// - Parameter nanoseconds: The delay after which cancellation event triggered.
134+
/// - Throws: `CancellationError` if cancelled.
134135
@Sendable
135136
public func cancel(afterNanoseconds nanoseconds: UInt64) async throws {
136137
try await Task.sleep(nanoseconds: nanoseconds)

Sources/AsyncObjects/Future.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ extension Future where Failure == Error {
337337
/// Immediately returns if `Future` is fulfilled otherwise waits asynchronously
338338
/// for `Future` to be fulfilled. If the Future terminates with an error,
339339
/// the awaiting caller receives the error instead.
340+
///
341+
/// - Throws: If future rejected with error or `CancellationError` if cancelled.
340342
public var value: Output {
341343
get async throws {
342344
if let result = result { return try result.get() }

Sources/AsyncObjects/Locker.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public final class Locker: Equatable, Hashable, Sendable {
9898
///
9999
/// - Parameter critical: The critical task to perform.
100100
/// - Returns: The result from the critical task.
101+
/// - Throws: Error occurred running critical task.
101102
@discardableResult
102103
public func perform<R>(_ critical: () throws -> R) rethrows -> R {
103104
lock()

Sources/AsyncObjects/TaskOperation.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
114114
/// - operation: The asynchronous operation to execute.
115115
///
116116
/// - Returns: The newly created asynchronous operation.
117-
///
118-
/// - Important: When specifying to track child tasks to true,
119-
/// be sure that you aren't keeping strong reference
120-
/// to ``TaskTracker/current`` inside your asynchronous operation.
121-
/// Otherwise, operation won't complete as soon asynchronous operation
122-
/// and their child tasks complete.
123117
public init(
124118
trackChildTasks shouldTrackChildTasks: Bool = false,
125119
synchronizedWith locker: Locker = .init(),
@@ -134,6 +128,7 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
134128
}
135129

136130
deinit {
131+
execTask?.cancel()
137132
locker.perform { self.continuations.forEach { $0.value.cancel() } }
138133
}
139134

Sources/AsyncObjects/TaskTracker.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// Do not keep strong reference of this object, as then object won't
1010
/// deallocate as soon asynchronous operations and their child tasks
1111
/// complete.
12-
public final class TaskTracker: Sendable {
12+
final class TaskTracker: Sendable {
1313
/// The tracker associated with current task.
1414
///
1515
/// Use the `withValue` method to assign a tracker to asynchronous operation.
@@ -21,7 +21,7 @@ public final class TaskTracker: Sendable {
2121
/// deallocate as soon asynchronous operations and their child tasks
2222
/// complete.
2323
@TaskLocal
24-
public static var current: TaskTracker?
24+
static var current: TaskTracker?
2525

2626
/// The action to complete when task and all its child tasks complete.
2727
private let fire: @Sendable () -> Void
@@ -39,7 +39,7 @@ public final class TaskTracker: Sendable {
3939
/// Do not keep strong reference of this object, as then object won't
4040
/// deallocate as soon asynchronous operations and their child tasks
4141
/// complete.
42-
public init(onComplete fire: @Sendable @escaping () -> Void) {
42+
init(onComplete fire: @Sendable @escaping () -> Void) {
4343
self.fire = fire
4444
}
4545

Tests/AsyncObjectsTests/TaskOperationTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Dispatch
33
@testable import AsyncObjects
44

55
class TaskOperationTests: XCTestCase {
6-
6+
#if canImport(Darwin)
77
func testTaskOperation() async throws {
88
let queue = OperationQueue()
99
let operation = TaskOperation {
@@ -120,6 +120,7 @@ class TaskOperationTests: XCTestCase {
120120
XCTAssertFalse(operation.isExecuting)
121121
XCTAssertTrue(operation.isCancelled)
122122
}
123+
#endif
123124

124125
func testTaskOperationAsyncWait() async throws {
125126
let operation = TaskOperation {

Tests/AsyncObjectsTests/XCTestCase.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ extension XCTestCase {
1212
XCTAssertEqual(
1313
seconds,
1414
Int(
15-
DispatchTime.now().uptimeNanoseconds - time.uptimeNanoseconds
16-
) / Int(1E9)
15+
(Double(
16+
DispatchTime.now().uptimeNanoseconds
17+
- time.uptimeNanoseconds
18+
) / 1E9).rounded(.toNearestOrAwayFromZero)
19+
)
1720
)
1821
}
1922

@@ -41,10 +44,11 @@ extension XCTestCase {
4144
) async rethrows where R.Bound == Int {
4245
let time = DispatchTime.now()
4346
try await task()
44-
let duration =
45-
Int(
47+
let duration = Int(
48+
(Double(
4649
DispatchTime.now().uptimeNanoseconds - time.uptimeNanoseconds
47-
) / Int(1E9)
50+
) / 1E9).rounded(.toNearestOrAwayFromZero)
51+
)
4852
XCTAssertTrue(
4953
range.contains(duration),
5054
"\(duration) not present in \(range)"

0 commit comments

Comments
 (0)