|
| 1 | +import XCTest |
| 2 | +@testable import AsyncObjects |
| 3 | + |
| 4 | +class CancellationSourceTests: XCTestCase { |
| 5 | + |
| 6 | + func testTaskCancellation() async throws { |
| 7 | + let source = await CancellationSource() |
| 8 | + let task = Task { |
| 9 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 10 | + } |
| 11 | + await source.register(task: task) |
| 12 | + await source.cancel() |
| 13 | + XCTAssertTrue(task.isCancelled) |
| 14 | + } |
| 15 | + |
| 16 | + func testTaskCancellationWithTimeout() async throws { |
| 17 | + let source = CancellationSource(cancelAfterNanoseconds: UInt64(5E9)) |
| 18 | + let task = Task { |
| 19 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 20 | + } |
| 21 | + await source.register(task: task) |
| 22 | + try await Task.sleep(nanoseconds: UInt64(6E9)) |
| 23 | + XCTAssertTrue(task.isCancelled) |
| 24 | + } |
| 25 | + |
| 26 | + func testTaskCancellationWithLinkedSource() async throws { |
| 27 | + let parentSource = await CancellationSource() |
| 28 | + let source = await CancellationSource(linkedWith: parentSource) |
| 29 | + let task = Task { |
| 30 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 31 | + } |
| 32 | + await source.register(task: task) |
| 33 | + await parentSource.cancel() |
| 34 | + XCTAssertTrue(task.isCancelled) |
| 35 | + } |
| 36 | + |
| 37 | + func testTaskCancellationWithMultipleLinkedSources() async throws { |
| 38 | + let parentSource1 = await CancellationSource() |
| 39 | + let parentSource2 = await CancellationSource() |
| 40 | + let source = await CancellationSource( |
| 41 | + linkedWith: parentSource1, parentSource2 |
| 42 | + ) |
| 43 | + let task = Task { |
| 44 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 45 | + } |
| 46 | + await source.register(task: task) |
| 47 | + await parentSource1.cancel() |
| 48 | + XCTAssertTrue(task.isCancelled) |
| 49 | + } |
| 50 | + |
| 51 | + func testTaskCancellationWithSourcePassedOnInitialization() async throws { |
| 52 | + let source = await CancellationSource() |
| 53 | + let task = await Task(cancellationSource: source) { |
| 54 | + do { |
| 55 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 56 | + XCTFail() |
| 57 | + } catch {} |
| 58 | + } |
| 59 | + await source.cancel() |
| 60 | + XCTAssertTrue(task.isCancelled) |
| 61 | + } |
| 62 | + |
| 63 | + func testDetachedTaskCancellationWithSourcePassedOnInitialization() |
| 64 | + async throws |
| 65 | + { |
| 66 | + let source = await CancellationSource() |
| 67 | + let task = await Task.detached(cancellationSource: source) { |
| 68 | + do { |
| 69 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 70 | + XCTFail() |
| 71 | + } catch {} |
| 72 | + } |
| 73 | + await source.cancel() |
| 74 | + XCTAssertTrue(task.isCancelled) |
| 75 | + } |
| 76 | + |
| 77 | + func testThrowingTaskCancellationWithSourcePassedOnInitialization() |
| 78 | + async throws |
| 79 | + { |
| 80 | + let source = await CancellationSource() |
| 81 | + let task = try await Task(cancellationSource: source) { |
| 82 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 83 | + } |
| 84 | + await source.cancel() |
| 85 | + XCTAssertTrue(task.isCancelled) |
| 86 | + } |
| 87 | + |
| 88 | + func testThrowingDetachedTaskCancellationWithSourcePassedOnInitialization() |
| 89 | + async throws |
| 90 | + { |
| 91 | + let source = await CancellationSource() |
| 92 | + let task = try await Task.detached(cancellationSource: source) { |
| 93 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 94 | + } |
| 95 | + await source.cancel() |
| 96 | + XCTAssertTrue(task.isCancelled) |
| 97 | + } |
| 98 | + |
| 99 | + func createTaskWithCancellationSource( |
| 100 | + _ source: CancellationSource |
| 101 | + ) -> Task<Void, Never> { |
| 102 | + return Task(cancellationSource: source) { |
| 103 | + do { |
| 104 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 105 | + XCTAssertTrue(Task.isCancelled) |
| 106 | + } catch {} |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + func testTaskCancellationWithSourcePassedOnSyncInitialization() async throws |
| 111 | + { |
| 112 | + let source = await CancellationSource() |
| 113 | + let task = createTaskWithCancellationSource(source) |
| 114 | + Task { |
| 115 | + try await Task.sleep(nanoseconds: UInt64(2E9)) |
| 116 | + await source.cancel() |
| 117 | + } |
| 118 | + await task.value |
| 119 | + } |
| 120 | + |
| 121 | + func createDetachedTaskWithCancellationSource( |
| 122 | + _ source: CancellationSource |
| 123 | + ) -> Task<Void, Never> { |
| 124 | + return Task.detached(cancellationSource: source) { |
| 125 | + do { |
| 126 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 127 | + XCTFail() |
| 128 | + } catch {} |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + func testDetachedTaskCancellationWithSourcePassedOnSyncInitialization() |
| 133 | + async throws |
| 134 | + { |
| 135 | + let source = await CancellationSource() |
| 136 | + let task = createDetachedTaskWithCancellationSource(source) |
| 137 | + Task { |
| 138 | + try await Task.sleep(nanoseconds: UInt64(2E9)) |
| 139 | + await source.cancel() |
| 140 | + } |
| 141 | + await task.value |
| 142 | + } |
| 143 | + |
| 144 | + func createThrowingTaskWithCancellationSource( |
| 145 | + _ source: CancellationSource |
| 146 | + ) throws -> Task<Void, Error> { |
| 147 | + return try Task(cancellationSource: source) { |
| 148 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 149 | + XCTFail() |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + func testThrowingTaskCancellationWithSourcePassedOnSyncInitialization() |
| 154 | + async throws |
| 155 | + { |
| 156 | + let source = await CancellationSource() |
| 157 | + let task = try createThrowingTaskWithCancellationSource(source) |
| 158 | + Task { |
| 159 | + try await Task.sleep(nanoseconds: UInt64(2E9)) |
| 160 | + await source.cancel() |
| 161 | + } |
| 162 | + let value: Void? = try? await task.value |
| 163 | + XCTAssertNil(value) |
| 164 | + } |
| 165 | + |
| 166 | + func createThrowingDetachedTaskWithCancellationSource( |
| 167 | + _ source: CancellationSource |
| 168 | + ) throws -> Task<Void, Error> { |
| 169 | + return try Task.detached(cancellationSource: source) { |
| 170 | + try await Task.sleep(nanoseconds: UInt64(10E9)) |
| 171 | + XCTFail() |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + func |
| 176 | + testThrowingDetachedTaskCancellationWithSourcePassedOnSyncInitialization() |
| 177 | + async throws |
| 178 | + { |
| 179 | + let source = await CancellationSource() |
| 180 | + let task = try createThrowingDetachedTaskWithCancellationSource(source) |
| 181 | + Task { |
| 182 | + try await Task.sleep(nanoseconds: UInt64(2E9)) |
| 183 | + await source.cancel() |
| 184 | + } |
| 185 | + let value: Void? = try? await task.value |
| 186 | + XCTAssertNil(value) |
| 187 | + } |
| 188 | +} |
0 commit comments