Skip to content

Commit

Permalink
Changes from comments in PR
Browse files Browse the repository at this point in the history
Remove no longer supported code
Add tests for errors being thrown, and the thread pool not being active
  • Loading branch information
adam-fowler committed Oct 23, 2023
1 parent 23e48bf commit 3bdc6c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
12 changes: 0 additions & 12 deletions Sources/NIOPosix/NIOThreadPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ extension NIOThreadPool {

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThreadPool {
#if swift(>=5.7)
/// Runs the submitted closure if the thread pool is still active, otherwise throw an error.
/// The closure will be run on the thread pool so can do blocking work.
///
Expand All @@ -305,17 +304,6 @@ extension NIOThreadPool {
public func runIfActive<T>(_ body: @escaping @Sendable () throws -> T) async throws -> T {
try await self._runIfActive(body)
}
#else
/// Runs the submitted closure if the thread pool is still active, otherwise throw an error.
/// The closure will be run on the thread pool so can do blocking work.
///
/// - parameters:
/// - body: The closure which performs some blocking work to be done on the thread pool.
/// - returns: result of the passed closure.
public func runIfActive<T>(_ body: @escaping () throws -> T) async throws -> T {
try await self._runIfActive(body)
}
#endif

private func _runIfActive<T>(_ body: @escaping () throws -> T) async throws -> T {
try await withCheckedThrowingContinuation { (cont: CheckedContinuation<T, Error>) in
Expand Down
33 changes: 33 additions & 0 deletions Tests/NIOPosixTests/NIOThreadPoolTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ class NIOThreadPoolTest: XCTestCase {
try await pool.shutdownGracefully()
}

func testAsyncThreadPoolErrorPropagation() async throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
struct ThreadPoolError: Error {}
let numberOfThreads = 1
let pool = NIOThreadPool(numberOfThreads: numberOfThreads)
pool.start()
do {
try await pool.runIfActive {
throw ThreadPoolError()
}
XCTFail("Should not get here as closure sent to runIfActive threw an error")
} catch {
XCTAssertNotNil(error as? ThreadPoolError, "Error thrown should be of type ThreadPoolError")
}
try await pool.shutdownGracefully()
}

func testAsyncThreadPoolNotActiveError() async throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
struct ThreadPoolError: Error {}
let numberOfThreads = 1
let pool = NIOThreadPool(numberOfThreads: numberOfThreads)
do {
try await pool.runIfActive {
throw ThreadPoolError()
}
XCTFail("Should not get here as thread pool isn't active")
} catch {
XCTAssertNotNil(error as? NIOThreadPoolError.ThreadPoolInactive, "Error thrown should be of type ThreadPoolError")
}
try await pool.shutdownGracefully()
}

func testAsyncShutdownWorks() async throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
let threadPool = NIOThreadPool(numberOfThreads: 17)
Expand Down

0 comments on commit 3bdc6c4

Please sign in to comment.