Skip to content

Commit 28ce3d9

Browse files
committed
Use async sleep in concurrency tests
1 parent af4b6bc commit 28ce3d9

11 files changed

+14
-15
lines changed

test/Concurrency/Runtime/actor_counters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func runTest(numCounters: Int, numWorkers: Int, numIterations: Int) async {
5353
for i in 0..<numWorkers {
5454
workers.append(
5555
Task.runDetached { [counters] in
56-
usleep(UInt32.random(in: 0..<100) * 1000)
56+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
5757
await worker(identity: i, counters: counters, numIterations: numIterations)
5858
}
5959
)

test/Concurrency/Runtime/async_let_fibonacci.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ func asyncFib(_ n: Int) async -> Int {
3030
async let second = await asyncFib(n-1)
3131

3232
// Sleep a random amount of time waiting on the result producing a result.
33-
usleep(UInt32.random(in: 0..<100) * 1000)
33+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
3434

3535
let result = await first + second
3636

3737
// Sleep a random amount of time before producing a result.
38-
usleep(UInt32.random(in: 0..<100) * 1000)
38+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
3939

4040
return result
4141
}

test/Concurrency/Runtime/async_task_priority_current.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func test_multiple_lo_indirectly_escalated() async {
3232
@concurrent
3333
func loopUntil(priority: Task.Priority) async {
3434
while (Task.currentPriority != priority) {
35-
sleep(1)
35+
await Task.sleep(1_000_000_000)
3636
}
3737
}
3838

test/Concurrency/Runtime/async_taskgroup_is_empty.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func test_taskGroup_isEmpty() async {
2525
print("before add: isEmpty=\(group.isEmpty)")
2626

2727
await group.add {
28-
sleep(2)
28+
await Task.sleep(2_000_000_000)
2929
return await asyncEcho(1)
3030
}
3131

test/Concurrency/Runtime/async_taskgroup_next_not_invoked_cancelAll.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func test_skipCallingNext_butInvokeCancelAll() async {
1919
for n in numbers {
2020
print("group.add { \(n) }")
2121
await group.add { [group] () async -> Int in
22-
sleep(2)
22+
await Task.sleep(1_000_000_000)
2323
print(" inside group.add { \(n) }")
2424
print(" inside group.add { \(n) } (group cancelled: \(group.isCancelled))")
2525
print(" inside group.add { \(n) } (group child task cancelled: \(Task.isCancelled))")

test/Concurrency/Runtime/async_taskgroup_next_not_invoked_without_cancelAll.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import Darwin
1111
#elseif canImport(Glibc)
1212
import Glibc
1313
#endif
14-
1514
func test_skipCallingNext() async {
1615
let numbers = [1, 1]
1716

1817
let result = try! await Task.withGroup(resultType: Int.self) { (group) async -> Int in
1918
for n in numbers {
2019
print("group.add { \(n) }")
2120
await group.add { () async -> Int in
22-
sleep(1)
21+
await Task.sleep(1_000_000_000)
2322
let c = Task.isCancelled
2423
print(" inside group.add { \(n) } (canceled: \(c))")
2524
return n

test/Concurrency/Runtime/async_taskgroup_next_on_completed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func test_sum_nextOnCompleted() async {
2828

2929
// We specifically want to await on completed child tasks in this test,
3030
// so give them some time to complete before we hit group.next()
31-
sleep(2)
31+
await Task.sleep(2_000_000_000)
3232

3333
var sum = 0
3434
do {

test/Concurrency/Runtime/async_taskgroup_next_on_pending.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Glibc
1313
#endif
1414

1515
func completeSlowly(n: Int) async -> Int {
16-
sleep(UInt32(n + 1))
16+
await Task.sleep(UInt64((n * 1_000_000_000) + 1_000_000_000))
1717
print(" complete group.add { \(n) }")
1818
return n
1919
}

test/Concurrency/Runtime/basic_future.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func testSimple(
3434
// can complete.
3535
if doSuspend {
3636
print("- Future sleeping")
37-
sleep(1) // TODO: await Task.sleep would be preferable here
37+
await Task.sleep(1_000_000_000)
3838
}
3939

4040
if (shouldThrow) {
@@ -50,7 +50,7 @@ func testSimple(
5050
// can complete.
5151
if !doSuspend {
5252
print("+ Reader sleeping")
53-
sleep(1)
53+
await Task.sleep(1_000_000_000)
5454
}
5555

5656
do {

test/Concurrency/Runtime/future_fibonacci.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func asyncFib(_ n: Int) async -> Int {
3737
}
3838

3939
// Sleep a random amount of time waiting on the result producing a result.
40-
usleep(UInt32.random(in: 0..<100) * 1000)
40+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
4141

4242
let result = try! await first.get() + second.get()
4343

4444
// Sleep a random amount of time before producing a result.
45-
usleep(UInt32.random(in: 0..<100) * 1000)
45+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
4646

4747
return result
4848
}

0 commit comments

Comments
 (0)