Skip to content

Commit 196ceab

Browse files
authored
Fix race in TCPThroughputBenchmark (#2724)
1 parent 609469f commit 196ceab

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Sources/NIOPerformanceTester/TCPThroughputBenchmark.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ final class TCPThroughputBenchmark: Benchmark {
3838
public typealias InboundIn = ByteBuffer
3939
public typealias OutboundOut = ByteBuffer
4040

41+
private let connectionEstablishedPromise: EventLoopPromise<EventLoop>
4142
private var context: ChannelHandlerContext!
4243

44+
init(_ connectionEstablishedPromise: EventLoopPromise<EventLoop>) {
45+
self.connectionEstablishedPromise = connectionEstablishedPromise
46+
}
47+
4348
public func channelActive(context: ChannelHandlerContext) {
4449
self.context = context
50+
connectionEstablishedPromise.succeed(context.eventLoop)
4551
}
4652

4753
public func send(_ message: ByteBuffer, times count: Int) {
@@ -106,12 +112,11 @@ final class TCPThroughputBenchmark: Benchmark {
106112
func setUp() throws {
107113
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 4)
108114

109-
let connectionEstablished: EventLoopPromise<EventLoop> = self.group.next().makePromise()
115+
let connectionEstablishedPromise: EventLoopPromise<EventLoop> = self.group.next().makePromise()
110116

111117
self.serverChannel = try ServerBootstrap(group: self.group)
112118
.childChannelInitializer { channel in
113-
self.serverHandler = ServerHandler()
114-
connectionEstablished.succeed(channel.eventLoop)
119+
self.serverHandler = ServerHandler(connectionEstablishedPromise)
115120
return channel.pipeline.addHandler(self.serverHandler)
116121
}
117122
.bind(host: "127.0.0.1", port: 0)
@@ -134,7 +139,7 @@ final class TCPThroughputBenchmark: Benchmark {
134139
}
135140
self.message = message
136141

137-
self.serverEventLoop = try connectionEstablished.futureResult.wait()
142+
self.serverEventLoop = try connectionEstablishedPromise.futureResult.wait()
138143
}
139144

140145
func tearDown() {

0 commit comments

Comments
 (0)