Skip to content

Commit 08f4d2f

Browse files
committed
[Concurrency][Tests] Add a test of waiting until a time before the epoch.
Add a test to check that attempting to `Task.sleep(until:)` passing a time before the start of the Dispatch clocks works and doesn't wait forever. rdar://148899609
1 parent 69c965e commit 08f4d2f

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

test/Concurrency/Runtime/async_task_sleep.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ import Dispatch
1414
@available(SwiftStdlib 5.1, *)
1515
@main struct Main {
1616
static let pause = 500_000_000 // 500ms
17-
17+
1818
static func main() async {
1919
await testSleepDuration()
2020
await testSleepDoesNotBlock()
2121
await testSleepHuge()
22+
if #available(SwiftStdlib 5.7, *) {
23+
await testSleepNegative()
24+
}
2225
}
2326

2427
static func testSleepDuration() async {
@@ -34,7 +37,7 @@ import Dispatch
3437

3538
static func testSleepDoesNotBlock() async {
3639
// FIXME: Should run on main executor
37-
let task = detach {
40+
let task = Task.detached {
3841
print("Run first")
3942
}
4043

@@ -50,10 +53,10 @@ import Dispatch
5053
static func testSleepHuge() async {
5154
// Make sure nanoseconds values about Int64.max don't get interpreted as
5255
// negative and fail to sleep.
53-
let task1 = detach {
56+
let task1 = Task.detached {
5457
try await Task.sleep(nanoseconds: UInt64(Int64.max) + 1)
5558
}
56-
let task2 = detach {
59+
let task2 = Task.detached {
5760
try await Task.sleep(nanoseconds: UInt64.max)
5861
}
5962

@@ -73,4 +76,28 @@ import Dispatch
7376
fatalError("Sleep 2 completed early.")
7477
} catch {}
7578
}
79+
80+
@available(SwiftStdlib 5.7, *)
81+
static func testSleepNegative() async {
82+
// Make sure that "negative" times don't cause us to sleep forever
83+
let negativeDuration = Duration(secondsComponent: -60,
84+
attosecondsComponent: 0)
85+
let negativeTime = unsafe unsafeBitCast(negativeDuration,
86+
to: ContinuousClock.Instant.self)
87+
88+
let task = Task.detached {
89+
try await Task.sleep(until: negativeTime)
90+
}
91+
92+
try! await Task.sleep(nanoseconds: UInt64(pause))
93+
94+
task.cancel()
95+
96+
// The sleep should complete; if they throw, this is a failure.
97+
do {
98+
_ = try await task.value
99+
} catch {
100+
fatalError("Sleep tried to wait for a negative time")
101+
}
102+
}
76103
}

0 commit comments

Comments
 (0)