1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library) | %FileCheck %s --dump-input always
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
import _Concurrency
// FIXME: should not depend on Dispatch
import Dispatch
@available(SwiftStdlib 5.1, *)
@main struct Main {
static let pause = 500_000_000 // 500ms
static func main() async {
// CHECK: Starting!
print("Starting!")
await testSleepFinished()
await testSleepMomentary()
await testSleepCancelledBeforeStarted()
await testSleepCancelled()
}
static func testSleepFinished() async {
// CHECK-NEXT: Testing sleep that completes
print("Testing sleep that completes")
let start = DispatchTime.now()
// try! will fail if the task got cancelled (which shouldn't happen).
try! await Task.sleep(nanoseconds: UInt64(pause))
let stop = DispatchTime.now()
// assert that at least the specified time passed since calling `sleep`
assert(stop >= (start + .nanoseconds(pause)))
// CHECK-NEXT: Wakey wakey!
print("Wakey wakey!")
}
static func testSleepMomentary() async {
// CHECK-NEXT: Testing sleep that completes instantly
print("Testing sleep that completes instantly")
// try! will fail if the task got cancelled (which shouldn't happen).
try! await Task.sleep(nanoseconds: 0)
// CHECK-NEXT: Wakey wakey!
print("Wakey wakey!")
}
static func testSleepCancelledBeforeStarted() async {
// CHECK-NEXT: Testing sleep that gets cancelled before it starts
print("Testing sleep that gets cancelled before it starts")
let sleepyTask = Task {
try await Task.sleep(nanoseconds: UInt64(pause))
}
do {
sleepyTask.cancel()
try await sleepyTask.value
print("Bah, weird scheduling")
} catch is CancellationError {
print("Caught the cancellation error")
} catch {
fatalError("sleep(nanoseconds:) threw some other error: \(error)")
}
// CHECK: Cancelled!
print("Cancelled!")
}
static func testSleepCancelled() async {
// CHECK-NEXT: Testing sleep that gets cancelled before it completes
print("Testing sleep that gets cancelled before it completes")
let start = DispatchTime.now()
let sleepyTask = Task {
try await Task.sleep(nanoseconds: UInt64(pause))
}
do {
let waiterTask = Task {
try await sleepyTask.value
}
let cancellerTask = Task {
await Task.sleep(UInt64(pause / 2))
sleepyTask.cancel()
}
try await waiterTask.value
fatalError("sleep(nanoseconds:) should have thrown CancellationError")
} catch is CancellationError {
// CHECK-NEXT: Caught the cancellation error
print("Caught the cancellation error")
let stop = DispatchTime.now()
// assert that we stopped early.
assert(stop < (start + .nanoseconds(pause)))
} catch {
fatalError("sleep(nanoseconds:) threw some other error: \(error)")
}
// CHECK-NEXT: Cancelled!
print("Cancelled!")
}
}
|