-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathConcurrentTestHelper.swift
52 lines (50 loc) · 1.76 KB
/
ConcurrentTestHelper.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
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
/// Runs a test concurrently for the number of times specified
///
/// The test is run repeatedly using a Swift task group; each test run is performed as a "child task".
/// The function returns when all test runs have completed, and rethrows if a test run throws.
/// - Parameters:
/// - count: The number of test runs
/// - test: The function pointer for the test to run
/// - Throws: Any error thrown by one of the test runs.
public func repeatConcurrently(
count: Int,
test: @escaping @Sendable () async throws -> Void
) async throws {
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
for _ in 0..<count {
taskGroup.addTask {
try await test()
}
}
try await taskGroup.waitForAll()
}
}
/// Runs a test concurrently for the number of times specified with args
///
/// The test is run repeatedly using a Swift task group; each test run is performed as a "child task".
/// The function returns when all test runs have completed, and rethrows if a test run throws.
/// - Parameters:
/// - count: The number of test runs
/// - test: The function pointer for the test to run
/// - args: Any values to pass along to test function
/// - Throws: Any error thrown by one of the test runs.
public func repeatConcurrentlyWithArgs<Arg: Sendable>(
count: Int,
test: @escaping @Sendable (Arg) async throws -> Void,
arg: Arg
) async throws {
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
for _ in 0..<count {
taskGroup.addTask {
try await test(arg)
}
}
try await taskGroup.waitForAll()
}
}