Files
swift-mirror/test/Concurrency/Runtime/async_task_cancellation_while_running.swift

42 lines
1.2 KiB
Swift

// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library)
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch
// rdar://76038845
// UNSUPPORTED: use_os_stdlib
import Dispatch
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
func test_detach_cancel_while_child_running() async {
let h: Task.Handle<Bool, Error> = detach {
async let childCancelled: Bool = { () -> Bool in
await Task.sleep(3_000_000_000)
return Task.isCancelled
}()
let childWasCancelled = await childCancelled
print("child, cancelled: \(childWasCancelled)") // CHECK: child, cancelled: true
let selfWasCancelled = Task.isCancelled
print("self, cancelled: \(selfWasCancelled )") // CHECK: self, cancelled: true
return selfWasCancelled
}
// sleep here, i.e. give the task a moment to start running
await Task.sleep(2_000_000_000)
h.cancel()
print("handle cancel")
let got = try! await h.get()
print("was cancelled: \(got)") // CHECK: was cancelled: true
}
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@main struct Main {
static func main() async {
await test_detach_cancel_while_child_running()
}
}