Files
swift-mirror/test/Concurrency/async_cancellation.swift
Allan Shortlidge c02fc4724d Tests: Remove -disable-availability-checking from many Concurrency tests.
Instead, use the `%target-swift-5.1-abi-triple` substitution to compile the tests
for deployment to the minimum OS versions required for use of _Concurrency APIs.
2024-10-18 16:21:51 -07:00

59 lines
1.4 KiB
Swift

// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %s -emit-sil -o /dev/null -verify
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
// REQUIRES: concurrency
// REQUIRES: asserts
enum PictureData {
case value(String)
case failedToLoadImagePlaceholder
}
@available(SwiftStdlib 5.1, *)
func test_cancellation_checkCancellation() async throws {
try Task.checkCancellation()
}
@available(SwiftStdlib 5.1, *)
func test_cancellation_guard_isCancelled(_ any: Any) async -> PictureData {
guard !Task.isCancelled else {
return PictureData.failedToLoadImagePlaceholder
}
return PictureData.value("...")
}
@available(SwiftStdlib 5.1, *)
struct SomeFile: Sendable {
func close() {}
}
@available(SwiftStdlib 5.1, *)
func test_cancellation_withTaskCancellationHandler(_ anything: Any) async -> PictureData? {
let handle: Task<PictureData, Error> = .init {
let file = SomeFile()
return await withTaskCancellationHandler {
await test_cancellation_guard_isCancelled(file)
} onCancel: {
file.close()
}
}
handle.cancel()
return nil
}
@available(SwiftStdlib 5.1, *)
func test_cancellation_loop() async -> Int {
struct SampleTask { func process() async {} }
let tasks = [SampleTask(), SampleTask()]
var processed = 0
for t in tasks where !Task.isCancelled {
await t.process()
processed += 1
}
return processed
}