-
Notifications
You must be signed in to change notification settings - Fork 722
Fix NioAsyncWriter test on concurrency thread pool with single thread #3135
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
glbrntt
merged 15 commits into
apple:main
from
orobio:fix-NIOAsyncWriter-test-on-concurrency-thread-pool-with-single-thread
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ee05a53
Make sure NIOAsyncWriter test doesn't hang indefinitely
orobio 04e2e44
Add NIOThreadPoolTaskExecutor to NIOTestUtils
orobio b83d90d
Use NIOThreadPoolTaskExecutor for NIOAsyncWriter test that hangs on A…
orobio 7af7145
Rename test to fit with the section it's in
orobio c778a2f
Fix year in copyright notice
orobio e993300
Fix year in copyright notice
orobio 4634b06
Make NIOThreadPoolTaskExecutor member functions internal and @usableF…
orobio b0ba0b8
Merge branch 'main' into fix-NIOAsyncWriter-test-on-concurrency-threa…
glbrntt d885e38
Merge branch 'main' into fix-NIOAsyncWriter-test-on-concurrency-threa…
glbrntt 8afbc3f
Use ManualTaskExecutor instead of NIOThreadPoolTaskExecutor
orobio 5766412
Change ManualTaskExecutor related functionality from public to package
orobio 42a1c1c
Merge branch 'main' into fix-NIOAsyncWriter-test-on-concurrency-threa…
Lukasa 21487cb
Fix newline at end of file
orobio 9c579b5
Include availability declaration with visionOS in #if compiler(>=6)
orobio 3a5833a
Merge branch 'main' into fix-NIOAsyncWriter-test-on-concurrency-threa…
glbrntt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftNIO open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if compiler(>=6) | ||
|
|
||
| import DequeModule | ||
| import Synchronization | ||
|
|
||
| /// Provide a `ManualTaskExecutor` for the duration of the given `body`. | ||
| /// | ||
| /// The executor can be used for setting the executor preference of tasks and fully control | ||
| /// when execution of the tasks is performed. | ||
| /// | ||
| /// Example usage: | ||
| /// ```swift | ||
| /// await withDiscardingTaskGroup { group in | ||
| /// await withManualTaskExecutor { taskExecutor in | ||
| /// group.addTask(executorPreference: taskExecutor) { | ||
| /// print("Running") | ||
| /// } | ||
| /// taskExecutor.runUntilQueueIsEmpty() // Run the task synchronously | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// - warning: Do not escape the task executor from the closure for later use and make sure that | ||
| /// all tasks running on the executor are completely finished before `body` returns. | ||
| /// It is highly recommended to use structured concurrency with this task executor. | ||
| /// | ||
| /// - Parameters: | ||
| /// - body: The closure that will accept the task executor. | ||
| /// | ||
| /// - Throws: When `body` throws. | ||
| /// | ||
| /// - Returns: The value returned by `body`. | ||
| @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) | ||
| @inlinable | ||
| package func withManualTaskExecutor<T, Failure>( | ||
| body: (ManualTaskExecutor) async throws(Failure) -> T | ||
| ) async throws(Failure) -> T { | ||
| let taskExecutor = ManualTaskExecutor() | ||
| defer { taskExecutor.shutdown() } | ||
| return try await body(taskExecutor) | ||
| } | ||
|
|
||
| /// Provide two `ManualTaskExecutor`s for the duration of the given `body`. | ||
| /// | ||
| /// The executors can be used for setting the executor preference of tasks and fully control | ||
| /// when execution of the tasks is performed. | ||
| /// | ||
| /// Example usage: | ||
| /// ```swift | ||
| /// await withDiscardingTaskGroup { group in | ||
| /// await withManualTaskExecutor { taskExecutor1, taskExecutor2 in | ||
| /// group.addTask(executorPreference: taskExecutor1) { | ||
| /// print("Running 1") | ||
| /// } | ||
| /// group.addTask(executorPreference: taskExecutor2) { | ||
| /// print("Running 2") | ||
| /// } | ||
| /// taskExecutor2.runUntilQueueIsEmpty() // Run second task synchronously | ||
| /// taskExecutor1.runUntilQueueIsEmpty() // Run first task synchronously | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// - warning: Do not escape the task executors from the closure for later use and make sure that | ||
| /// all tasks running on the executors are completely finished before `body` returns. | ||
| /// It is highly recommended to use structured concurrency with these task executors. | ||
| /// | ||
| /// - Parameters: | ||
| /// - body: The closure that will accept the task executors. | ||
| /// | ||
| /// - Throws: When `body` throws. | ||
| /// | ||
| /// - Returns: The value returned by `body`. | ||
| @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) | ||
| @inlinable | ||
| package func withManualTaskExecutor<T, Failure>( | ||
| body: (ManualTaskExecutor, ManualTaskExecutor) async throws(Failure) -> T | ||
| ) async throws(Failure) -> T { | ||
| let taskExecutor1 = ManualTaskExecutor() | ||
| defer { taskExecutor1.shutdown() } | ||
|
|
||
| let taskExecutor2 = ManualTaskExecutor() | ||
| defer { taskExecutor2.shutdown() } | ||
|
|
||
| return try await body(taskExecutor1, taskExecutor2) | ||
| } | ||
|
|
||
| /// Manual task executor. | ||
| /// | ||
| /// A `TaskExecutor` that does not use any threadpool or similar mechanism to run the jobs. | ||
| /// Jobs are manually run by calling the `runUntilQueueIsEmpty` method. | ||
| /// | ||
| @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) | ||
| @usableFromInline | ||
| package final class ManualTaskExecutor: TaskExecutor { | ||
| struct Storage { | ||
| var isShutdown = false | ||
| var jobs = Deque<UnownedJob>() | ||
| } | ||
|
|
||
| private let storage = Mutex<Storage>(.init()) | ||
|
|
||
| @usableFromInline | ||
| init() {} | ||
|
|
||
| /// Run jobs until queue is empty. | ||
| /// | ||
| /// Synchronously runs all enqueued jobs, including any jobs that are enqueued while running. | ||
| /// When this function returns, it means that each task running on this executor is either: | ||
| /// - suspended | ||
| /// - moved (temporarily) to a different executor | ||
| /// - finished | ||
| /// | ||
| /// If not all tasks are finished, this function must be called again. | ||
| package func runUntilQueueIsEmpty() { | ||
| while let job = self.storage.withLock({ $0.jobs.popFirst() }) { | ||
| job.runSynchronously(on: self.asUnownedTaskExecutor()) | ||
| } | ||
| } | ||
|
|
||
| /// Enqueue a job. | ||
| /// | ||
| /// Called by the concurrency runtime. | ||
| /// | ||
| /// - Parameter job: The job to enqueue. | ||
| @usableFromInline | ||
| package func enqueue(_ job: UnownedJob) { | ||
| self.storage.withLock { storage in | ||
| if storage.isShutdown { | ||
| fatalError("A job is enqueued after manual executor shutdown") | ||
| } | ||
| storage.jobs.append(job) | ||
| } | ||
| } | ||
|
|
||
| /// Shutdown. | ||
| /// | ||
| /// Since the manual task executor is not running anything in the background, this is purely to catch | ||
| /// any issues due to incorrect usage of the executor. The shutdown verifies that the queue is empty | ||
| /// and makes sure that no new jobs can be enqueued. | ||
| @usableFromInline | ||
| func shutdown() { | ||
| self.storage.withLock { storage in | ||
| if !storage.jobs.isEmpty { | ||
| fatalError("Shutdown of manual executor with jobs in queue") | ||
| } | ||
| storage.isShutdown = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif // compiler(>=6) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftNIO open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if compiler(>=6) | ||
|
|
||
| import NIOTestUtils | ||
| import Synchronization | ||
| import XCTest | ||
|
|
||
| class ManualTaskExecutorTest: XCTestCase { | ||
| @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) | ||
| func testManualTaskExecutor() async { | ||
| await withDiscardingTaskGroup { group in | ||
| await withManualTaskExecutor { taskExecutor in | ||
| let taskDidRun = Mutex(false) | ||
|
|
||
| group.addTask(executorPreference: taskExecutor) { | ||
| taskDidRun.withLock { $0 = true } | ||
| } | ||
|
|
||
| // Run task | ||
| XCTAssertFalse(taskDidRun.withLock { $0 }) | ||
| taskExecutor.runUntilQueueIsEmpty() | ||
| XCTAssertTrue(taskDidRun.withLock { $0 }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) | ||
| func testTwoManualTaskExecutors() async { | ||
| await withDiscardingTaskGroup { group in | ||
| await withManualTaskExecutor { taskExecutor1, taskExecutor2 in | ||
| let task1DidRun = Mutex(false) | ||
| let task2DidRun = Mutex(false) | ||
|
|
||
| group.addTask(executorPreference: taskExecutor1) { | ||
| task1DidRun.withLock { $0 = true } | ||
| } | ||
|
|
||
| group.addTask(executorPreference: taskExecutor2) { | ||
| task2DidRun.withLock { $0 = true } | ||
| } | ||
|
|
||
| // Run task 1 | ||
| XCTAssertFalse(task1DidRun.withLock { $0 }) | ||
| taskExecutor1.runUntilQueueIsEmpty() | ||
| XCTAssertTrue(task1DidRun.withLock { $0 }) | ||
|
|
||
| // Run task 2 | ||
| XCTAssertFalse(task2DidRun.withLock { $0 }) | ||
| taskExecutor2.runUntilQueueIsEmpty() | ||
| XCTAssertTrue(task2DidRun.withLock { $0 }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif // compiler(>=6) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.