forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_task_sleep_cancel.swift
114 lines (89 loc) · 3.13 KB
/
async_task_sleep_cancel.swift
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( -target %target-swift-5.1-abi-triple %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,*)
@mainstructMain{
staticletpause=500_000_000 // 500ms
staticfunc main()async{
// CHECK: Starting!
print("Starting!")
awaittestSleepFinished()
awaittestSleepMomentary()
awaittestSleepCancelledBeforeStarted()
awaittestSleepCancelled()
}
staticfunc testSleepFinished()async{
// CHECK-NEXT: Testing sleep that completes
print("Testing sleep that completes")
letstart=DispatchTime.now()
// try! will fail if the task got cancelled (which shouldn't happen).
try!awaitTask.sleep(nanoseconds:UInt64(pause))
letstop=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!")
}
staticfunc 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!awaitTask.sleep(nanoseconds:0)
// CHECK-NEXT: Wakey wakey!
print("Wakey wakey!")
}
staticfunc testSleepCancelledBeforeStarted()async{
// CHECK-NEXT: Testing sleep that gets cancelled before it starts
print("Testing sleep that gets cancelled before it starts")
letsleepyTask=Task{
tryawaitTask.sleep(nanoseconds:UInt64(pause))
}
do{
sleepyTask.cancel()
tryawait 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!")
}
staticfunc testSleepCancelled()async{
// CHECK-NEXT: Testing sleep that gets cancelled before it completes
print("Testing sleep that gets cancelled before it completes")
letstart=DispatchTime.now()
letsleepyTask=Task{
tryawaitTask.sleep(nanoseconds:UInt64(pause))
}
do{
letwaiterTask=Task{
tryawait sleepyTask.value
}
letcancellerTask=Task{
awaitTask.sleep(UInt64(pause /2))
sleepyTask.cancel()
}
tryawait waiterTask.value
fatalError("sleep(nanoseconds:) should have thrown CancellationError")
}catch is CancellationError{
// CHECK-NEXT: Caught the cancellation error
print("Caught the cancellation error")
letstop=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!")
}
}