Files
swift-mirror/test/Concurrency/async_cancellation.swift
Michael Gottesman b3e837c16c [region-isolation] Enable region isolation by default with strict-concurrency.
I added a disable flag -disable-region-based-isolation-with-strict-concurrency
so that we do not need to update the current tests. It is only available when
asserts are enabled to ensure users cannot use it.

rdar://125918028
2024-04-04 13:07:32 -07:00

61 lines
1.7 KiB
Swift

// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify -strict-concurrency=targeted
// RUN: %target-swift-frontend -disable-availability-checking %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -disable-region-based-isolation-with-strict-concurrency
// RUN: %target-swift-frontend -disable-availability-checking %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
}