|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the container project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import NIO |
| 18 | +import Testing |
| 19 | + |
| 20 | +@testable import SocketForwarder |
| 21 | + |
| 22 | +struct ConnectHandlerRaceTest { |
| 23 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) |
| 24 | + |
| 25 | + @Test |
| 26 | + func testRapidConnectDisconnect() async throws { |
| 27 | + let requestCount = 500 |
| 28 | + |
| 29 | + let serverAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0) |
| 30 | + let server = TCPEchoServer(serverAddress: serverAddress, eventLoopGroup: eventLoopGroup) |
| 31 | + let serverChannel = try await server.run().get() |
| 32 | + let actualServerAddress = try #require(serverChannel.localAddress) |
| 33 | + |
| 34 | + let proxyAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0) |
| 35 | + let forwarder = try TCPForwarder( |
| 36 | + proxyAddress: proxyAddress, |
| 37 | + serverAddress: actualServerAddress, |
| 38 | + eventLoopGroup: eventLoopGroup |
| 39 | + ) |
| 40 | + let forwarderResult = try await forwarder.run().get() |
| 41 | + let actualProxyAddress = try #require(forwarderResult.proxyAddress) |
| 42 | + |
| 43 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 44 | + for _ in 0..<requestCount { |
| 45 | + group.addTask { |
| 46 | + do { |
| 47 | + let channel = try await ClientBootstrap(group: self.eventLoopGroup) |
| 48 | + .connect(to: actualProxyAddress) |
| 49 | + .get() |
| 50 | + |
| 51 | + try await channel.close() |
| 52 | + } catch { |
| 53 | + // Going to ignore connection errors as we are intentionally stressing it |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + try await group.waitForAll() |
| 58 | + } |
| 59 | + |
| 60 | + serverChannel.eventLoop.execute { _ = serverChannel.close() } |
| 61 | + try await serverChannel.closeFuture.get() |
| 62 | + |
| 63 | + forwarderResult.close() |
| 64 | + try await forwarderResult.wait() |
| 65 | + } |
| 66 | +} |
0 commit comments