Skip to content

Commit 6e6ec40

Browse files
feat: added tests IS#923
1 parent bfef79f commit 6e6ec40

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
@testable import SocketForwarder
20+
21+
struct ConnectHandlerRaceTest {
22+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
23+
24+
@Test
25+
func testRapidConnectDisconnect() async throws {
26+
let requestCount = 500
27+
28+
let serverAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
29+
let server = TCPEchoServer(serverAddress: serverAddress, eventLoopGroup: eventLoopGroup)
30+
let serverChannel = try await server.run().get()
31+
let actualServerAddress = try #require(serverChannel.localAddress)
32+
33+
let proxyAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
34+
let forwarder = try TCPForwarder(
35+
proxyAddress: proxyAddress,
36+
serverAddress: actualServerAddress,
37+
eventLoopGroup: eventLoopGroup
38+
)
39+
let forwarderResult = try await forwarder.run().get()
40+
let actualProxyAddress = try #require(forwarderResult.proxyAddress)
41+
42+
try await withThrowingTaskGroup(of: Void.self) { group in
43+
for _ in 0..<requestCount {
44+
group.addTask {
45+
do {
46+
let channel = try await ClientBootstrap(group: self.eventLoopGroup)
47+
.connect(to: actualProxyAddress)
48+
.get()
49+
50+
try await channel.close()
51+
} catch {
52+
// Going to ignore connection errors as we are intentionally stressing it
53+
}
54+
}
55+
}
56+
try await group.waitForAll()
57+
}
58+
59+
serverChannel.eventLoop.execute { _ = serverChannel.close() }
60+
try await serverChannel.closeFuture.get()
61+
62+
forwarderResult.close()
63+
try await forwarderResult.wait()
64+
}
65+
}
66+

0 commit comments

Comments
 (0)