Skip to content

Commit 255acf8

Browse files
authored
Run a subtask of the runner task group for each individual test even when running serially. (#578)
Right now, when tests are run serially, they're simply run in a for-loop. This has the side effect of potentially building up a lot of autorelease pool cruft (on platforms with such a concept) if those tests never introduce any suspension points. As well, this produces a subtle difference in task cancellation propagation because calling `UnsafeCurrentTask.cancel()` in a serialized test will cancel the entire task group, not just the test's own subtask. Resolves rdar://130034310. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 5ccb3b7 commit 255acf8

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Sources/Testing/Running/Runner.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,17 @@ extension Runner {
121121
_ body: @Sendable @escaping (E) async throws -> Void
122122
) async throws where E: Sendable {
123123
let isParallelizationEnabled = step?.action.isParallelizationEnabled ?? configuration.isParallelizationEnabled
124-
if isParallelizationEnabled {
125-
// Each element gets its own subtask to run in.
126-
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
127-
for element in sequence {
128-
_ = taskGroup.addTaskUnlessCancelled {
129-
try await body(element)
130-
}
131-
}
132-
try await taskGroup.waitForAll()
133-
}
134-
} else {
135-
// No task group or subtasks required as parallelization is not enabled.
124+
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
136125
for element in sequence {
137-
try Task.checkCancellation()
138-
try await body(element)
126+
// Each element gets its own subtask to run in.
127+
_ = taskGroup.addTaskUnlessCancelled {
128+
try await body(element)
129+
}
130+
131+
// If not parallelizing, wait after each task.
132+
if !isParallelizationEnabled {
133+
try await taskGroup.waitForAll()
134+
}
139135
}
140136
}
141137
}

0 commit comments

Comments
 (0)