Skip to content
Merged
Show file tree
Hide file tree
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
83 changes: 26 additions & 57 deletions Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String>)
case byte(NIOAsyncChannel<UInt8, UInt8>)
Expand All @@ -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)
Expand Down Expand Up @@ -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<EventLoopFuture<NegotiationResult>, Never> = try await ServerBootstrap(
group: eventLoopGroup
Expand Down Expand Up @@ -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<EventLoopFuture<EventLoopFuture<NegotiationResult>>, Never> =
try await ServerBootstrap(group: eventLoopGroup)
Expand Down Expand Up @@ -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<EventLoopFuture<NegotiationResult>, Never> = try await ServerBootstrap(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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<ByteBuffer, ByteBuffer>
let toChannel: NIOAsyncChannel<Never, ByteBuffer>
Expand Down Expand Up @@ -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<ByteBuffer, ByteBuffer>
let fromChannel: NIOAsyncChannel<ByteBuffer, Never>
Expand Down Expand Up @@ -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<ByteBuffer, ByteBuffer>
let toChannel: NIOAsyncChannel<Never, ByteBuffer>
Expand Down Expand Up @@ -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<NegotiationResult>
let toChannel: NIOAsyncChannel<Never, ByteBuffer>
Expand Down Expand Up @@ -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)
Expand All @@ -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<NegotiationResult>.self) { group in
group.addTask {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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, *)
Expand Down
36 changes: 24 additions & 12 deletions Tests/NIOPosixTests/SerialExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,39 +47,42 @@ 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)
await testActor.assertNotInLoop(loop2)
}

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 {
#if compiler(<6.0)
throw XCTSkip("Custom executors are only supported in 5.9")
#else

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was actually a bug here which would always leak the group...

let el = group.next()
let el = self.group.next()

let testActor = EventLoopBoundActor(loop: el)
let result = try await el.submit {
Expand All @@ -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
}
}
Loading