From dd7d4b0fb1d39345b09ba59c2ec49b9742224ca2 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Mon, 25 Nov 2024 09:58:25 +0000 Subject: [PATCH] fix warnings: syncShutdownGracefully not available in async contexts (#2995) ### Motivation: Warnings are annoying. Companion of #2994 ### Modifications: Fix all the `syncShutdownGracefully` not being available in `async` contexts warnings. ### Result: Everybody happier. --- .../AsyncChannelBootstrapTests.swift | 83 ++++++------------- Tests/NIOPosixTests/SerialExecutorTests.swift | 36 +++++--- 2 files changed, 50 insertions(+), 69 deletions(-) diff --git a/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift b/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift index e1dc3d215b..847a5ac782 100644 --- a/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift +++ b/Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2023-2024 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -199,6 +199,8 @@ private final class AddressedEnvelopingHandler: ChannelDuplexHandler { @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) final class AsyncChannelBootstrapTests: XCTestCase { + var group: MultiThreadedEventLoopGroup! + enum NegotiationResult { case string(NIOAsyncChannel) case byte(NIOAsyncChannel) @@ -214,10 +216,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { // MARK: Server/Client Bootstrap func testServerClientBootstrap_withAsyncChannel_andHostPort() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let channel = try await ServerBootstrap(group: eventLoopGroup) .serverChannelOption(.socketOption(.so_reuseaddr), value: 1) @@ -273,10 +272,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testAsyncChannelProtocolNegotiation() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let channel: NIOAsyncChannel, Never> = try await ServerBootstrap( group: eventLoopGroup @@ -360,10 +356,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testAsyncChannelNestedProtocolNegotiation() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let channel: NIOAsyncChannel>, Never> = try await ServerBootstrap(group: eventLoopGroup) @@ -497,10 +490,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } } - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let channels = NIOLockedValueBox<[Channel]>([Channel]()) let channel: NIOAsyncChannel, Never> = try await ServerBootstrap( @@ -610,10 +600,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testServerClientBootstrap_withAsyncChannel_clientConnectedSocket() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let channel = try await ServerBootstrap(group: eventLoopGroup) .serverChannelOption(.socketOption(.so_reuseaddr), value: 1) @@ -675,10 +662,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { // MARK: Datagram Bootstrap func testDatagramBootstrap_withAsyncChannel_andHostPort() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let serverChannel = try await self.makeUDPServerChannel(eventLoopGroup: eventLoopGroup) let clientChannel = try await self.makeUDPClientChannel( @@ -700,10 +684,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testDatagramBootstrap_withProtocolNegotiation_andHostPort() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! // We are creating a channel here to get a random port from the system let channel = try await DatagramBootstrap(group: eventLoopGroup) @@ -785,10 +766,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { // MARK: - Pipe Bootstrap func testPipeBootstrap() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let (pipe1ReadFD, pipe1WriteFD, pipe2ReadFD, pipe2WriteFD) = self.makePipeFileDescriptors() let channel: NIOAsyncChannel let toChannel: NIOAsyncChannel @@ -861,10 +839,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testPipeBootstrap_whenInputNil() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let (pipe1ReadFD, pipe1WriteFD) = self.makePipeFileDescriptors() let channel: NIOAsyncChannel let fromChannel: NIOAsyncChannel @@ -916,10 +891,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testPipeBootstrap_whenOutputNil() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let (pipe1ReadFD, pipe1WriteFD) = self.makePipeFileDescriptors() let channel: NIOAsyncChannel let toChannel: NIOAsyncChannel @@ -973,10 +945,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { } func testPipeBootstrap_withProtocolNegotiation() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let (pipe1ReadFD, pipe1WriteFD, pipe2ReadFD, pipe2WriteFD) = self.makePipeFileDescriptors() let negotiationResult: EventLoopFuture let toChannel: NIOAsyncChannel @@ -1067,10 +1036,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { func testRawSocketBootstrap() async throws { try XCTSkipIfUserHasNotEnoughRightsForRawSocketAPI() - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let serverChannel = try await self.makeRawSocketServerChannel(eventLoopGroup: eventLoopGroup) let clientChannel = try await self.makeRawSocketClientChannel(eventLoopGroup: eventLoopGroup) @@ -1091,10 +1057,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { func testRawSocketBootstrap_withProtocolNegotiation() async throws { try XCTSkipIfUserHasNotEnoughRightsForRawSocketAPI() - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! try await withThrowingTaskGroup(of: EventLoopFuture.self) { group in group.addTask { @@ -1142,10 +1105,7 @@ final class AsyncChannelBootstrapTests: XCTestCase { func testVSock() async throws { try XCTSkipUnless(System.supportsVsockLoopback, "No vsock loopback transport available") - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3) - defer { - try! eventLoopGroup.syncShutdownGracefully() - } + let eventLoopGroup = self.group! let port = VsockAddress.Port(1234) @@ -1560,6 +1520,15 @@ final class AsyncChannelBootstrapTests: XCTestCase { try channel.pipeline.syncOperations.addHandler(negotiationHandler) return negotiationHandler.protocolNegotiationResult } + + override func setUp() { + self.group = MultiThreadedEventLoopGroup(numberOfThreads: 3) + } + + override func tearDown() { + XCTAssertNoThrow(try self.group.syncShutdownGracefully()) + self.group = nil + } } @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) diff --git a/Tests/NIOPosixTests/SerialExecutorTests.swift b/Tests/NIOPosixTests/SerialExecutorTests.swift index 0ec3994f9c..3c69698d0f 100644 --- a/Tests/NIOPosixTests/SerialExecutorTests.swift +++ b/Tests/NIOPosixTests/SerialExecutorTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2023-2024 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -47,6 +47,8 @@ actor EventLoopBoundActor { @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) final class SerialExecutorTests: XCTestCase { + var group: MultiThreadedEventLoopGroup! + private func _testBasicExecutorFitsOnEventLoop(loop1: EventLoop, loop2: EventLoop) async throws { let testActor = EventLoopBoundActor(loop: loop1) await testActor.assertInLoop(loop1) @@ -54,23 +56,25 @@ final class SerialExecutorTests: XCTestCase { } func testBasicExecutorFitsOnEventLoop_MTELG() async throws { - let group = MultiThreadedEventLoopGroup(numberOfThreads: 2) - defer { - try! group.syncShutdownGracefully() - } - let loops = Array(group.makeIterator()) + let loops = Array(self.group.makeIterator()) try await self._testBasicExecutorFitsOnEventLoop(loop1: loops[0], loop2: loops[1]) } func testBasicExecutorFitsOnEventLoop_AsyncTestingEventLoop() async throws { let loop1 = NIOAsyncTestingEventLoop() let loop2 = NIOAsyncTestingEventLoop() - defer { - try? loop1.syncShutdownGracefully() - try? loop2.syncShutdownGracefully() + func shutdown() async { + await loop1.shutdownGracefully() + await loop2.shutdownGracefully() } - try await self._testBasicExecutorFitsOnEventLoop(loop1: loop1, loop2: loop2) + do { + try await self._testBasicExecutorFitsOnEventLoop(loop1: loop1, loop2: loop2) + await shutdown() + } catch { + await shutdown() + throw error + } } func testAssumeIsolation() async throws { @@ -78,8 +82,7 @@ final class SerialExecutorTests: XCTestCase { throw XCTSkip("Custom executors are only supported in 5.9") #else - let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - let el = group.next() + let el = self.group.next() let testActor = EventLoopBoundActor(loop: el) let result = try await el.submit { @@ -88,4 +91,13 @@ final class SerialExecutorTests: XCTestCase { XCTAssertEqual(result, 0) #endif } + + override func setUp() { + self.group = MultiThreadedEventLoopGroup(numberOfThreads: 3) + } + + override func tearDown() { + XCTAssertNoThrow(try self.group.syncShutdownGracefully()) + self.group = nil + } }