mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
### Motivation
The new `NoUseUnstructuredThrowingTask` diagnostic on nightly-main flags
every `Task { try await ... }`
whose result is dropped. We have a few of those.
### Changes
- Use `async let _` instead of a fire-and-forget `Task` in
`NIOAsyncWriterTests` and
`NIOThrowingAsyncSequenceTests`.
- `NIOPerformanceTester`: rethrow the error from the `Task` in
`measureAndPrint` instead of
swallowing it.
- Name the `swift-nio` dependency explicitly in
`Benchmarks/Package.swift`; SwiftPM otherwise infers
it from the directory name, which breaks in git worktrees.
### Result
No warnings. Suspended yields in the tests are cancelled when the test
returns instead of leaking
past it, and async benchmarks fail loudly.
67 lines
1.7 KiB
Swift
67 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2019 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Dispatch
|
|
import NIOConcurrencyHelpers
|
|
|
|
protocol Benchmark: AnyObject {
|
|
func setUp() throws
|
|
func tearDown()
|
|
func run() throws -> Int
|
|
}
|
|
|
|
func measureAndPrint<B: Benchmark>(desc: String, benchmark bench: B) throws {
|
|
try bench.setUp()
|
|
defer {
|
|
bench.tearDown()
|
|
}
|
|
try measureAndPrint(desc: desc) {
|
|
try bench.run()
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
protocol AsyncBenchmark: AnyObject, Sendable {
|
|
func setUp() async throws
|
|
func tearDown()
|
|
func run() async throws -> Int
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
func measureAndPrint<B: AsyncBenchmark>(desc: String, benchmark bench: B) throws {
|
|
let group = DispatchGroup()
|
|
group.enter()
|
|
let caughtError = NIOLockedValueBox<Error?>(nil)
|
|
Task {
|
|
do {
|
|
try await bench.setUp()
|
|
defer {
|
|
bench.tearDown()
|
|
}
|
|
try await measureAndPrint(desc: desc) {
|
|
try await bench.run()
|
|
}
|
|
} catch {
|
|
caughtError.withLockedValue { $0 = error }
|
|
}
|
|
group.leave()
|
|
}
|
|
|
|
group.wait()
|
|
|
|
if let error = caughtError.withLockedValue({ $0 }) {
|
|
throw error
|
|
}
|
|
}
|