Skip to content

Make mega yield configurable by environment variable #2064

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

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Sources/ComposableArchitecture/TestStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2365,17 +2365,24 @@ class TestReducer<State, Action>: ReducerProtocol {
}
}

extension Task where Success == Never, Failure == Never {
// NB: We would love if this was not necessary, but due to a lack of async testing tools in Swift
// we're not sure if there is an alternative. See this forum post for more information:
extension Task where Success == Failure, Failure == Never {
// NB: We would love if this was not necessary. See this forum post for more information:
// https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304
@_spi(Internals) public static func megaYield(count: Int = 10) async {
for _ in 1...count {
@_spi(Internals) public static func megaYield(count: Int = defaultMegaYieldCount) async {
for _ in 0..<count {
await Task<Void, Never>.detached(priority: .background) { await Task.yield() }.value
}
}
}

@_spi(Internals) public let defaultMegaYieldCount = max(
0,
min(
ProcessInfo.processInfo.environment["TASK_MEGA_YIELD_COUNT"].flatMap(Int.init) ?? 20,
10_000
)
)

// NB: Only needed until Xcode ships a macOS SDK that uses the 5.7 standard library.
// See: https://forums.swift.org/t/xcode-14-rc-cannot-specialize-protocol-type/60171/15
#if swift(>=5.7) && !os(macOS) && !targetEnvironment(macCatalyst)
Expand Down