-
Notifications
You must be signed in to change notification settings - Fork 126
Add an upper bound on the number of test cases we run in parallel. #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4da4061
85822e3
23224a2
5f9bbc5
2b551e6
5e47794
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2024–2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| private import _TestingInternals | ||
|
|
||
| /// The number of CPU cores on the current system, or `nil` if that | ||
| /// information is not available. | ||
| var cpuCoreCount: Int? { | ||
| #if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) | ||
| return Int(sysconf(Int32(_SC_NPROCESSORS_CONF))) | ||
| #elseif os(Windows) | ||
| var siInfo = SYSTEM_INFO() | ||
| GetSystemInfo(&siInfo) | ||
| return Int(siInfo.dwNumberOfProcessors) | ||
| #elseif os(WASI) | ||
| return 1 | ||
| #else | ||
| #warning("Platform-specific implementation missing: CPU core count unavailable") | ||
| return nil | ||
| #endif | ||
| } | ||
|
|
||
| /// A type whose instances can run a series of work items in strict order. | ||
| /// | ||
| /// When a work item is scheduled on an instance of this type, it runs after any | ||
| /// previously-scheduled work items. If it suspends, subsequently-scheduled work | ||
| /// items do not start running; they must wait until the suspended work item | ||
| /// either returns or throws an error. | ||
| /// | ||
| /// This type is not part of the public interface of the testing library. | ||
| final actor Serializer { | ||
grynspan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// The maximum number of work items that may run concurrently. | ||
| nonisolated let maximumWidth: Int | ||
|
|
||
| /// The number of scheduled work items, including any currently running. | ||
| private var _currentWidth = 0 | ||
|
|
||
| /// Continuations for any scheduled work items that haven't started yet. | ||
| private var _continuations = [CheckedContinuation<Void, Never>]() | ||
|
|
||
| init(maximumWidth: Int = 1) { | ||
| self.maximumWidth = maximumWidth | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we precondition on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eeeeh okay. |
||
| } | ||
|
|
||
| /// Run a work item serially after any previously-scheduled work items. | ||
| /// | ||
| /// - Parameters: | ||
| /// - workItem: A closure to run. | ||
| /// | ||
| /// - Returns: Whatever is returned from `workItem`. | ||
| /// | ||
| /// - Throws: Whatever is thrown by `workItem`. | ||
| func run<R>(_ workItem: @isolated(any) @Sendable () async throws -> R) async rethrows -> R where R: Sendable { | ||
| _currentWidth += 1 | ||
| defer { | ||
| // Resume the next scheduled closure. | ||
| if !_continuations.isEmpty { | ||
| let continuation = _continuations.removeFirst() | ||
grynspan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continuation.resume() | ||
| } | ||
|
|
||
| _currentWidth -= 1 | ||
| } | ||
|
|
||
| await withCheckedContinuation { continuation in | ||
| if _currentWidth <= maximumWidth { | ||
| // Nothing else was scheduled, so we can resume immediately. | ||
| continuation.resume() | ||
| } else { | ||
| // Something was scheduled, so add the continuation to the | ||
| // list. When it resumes, we can run. | ||
| _continuations.append(continuation) | ||
| } | ||
| } | ||
|
|
||
| return try await workItem() | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.