Skip to content

Commit 56bb5f4

Browse files
authored
testConnect() declared a custom Socket but never used it. (#331)
Motivation: testConnect() should use its custom Socket implementation. Modifications: Make the test use the custom Socket Result: Correct test
1 parent 1360fc6 commit 56bb5f4

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Tests/NIOTests/SocketChannelTest.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,36 +195,41 @@ public class SocketChannelTest : XCTestCase {
195195
}
196196
}
197197

198+
let group = MultiThreadedEventLoopGroup(numThreads: 1)
199+
defer {
200+
XCTAssertNoThrow(try group.syncShutdownGracefully())
201+
}
202+
198203
class ConnectSocket: Socket {
199-
init() throws {
204+
private let promise: EventLoopPromise<Void>
205+
init(promise: EventLoopPromise<Void>) throws {
206+
self.promise = promise
200207
try super.init(protocolFamily: PF_INET, type: Posix.SOCK_STREAM)
201208
}
202209

203210
override func connect(to address: SocketAddress) throws -> Bool {
211+
self.promise.succeed(result: ())
204212
return true
205213
}
206214
}
207215

208-
let group = MultiThreadedEventLoopGroup(numThreads: 1)
209-
defer {
210-
XCTAssertNoThrow(try group.syncShutdownGracefully())
211-
}
212-
let serverChannel = try ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait()
213-
defer {
214-
XCTAssertNoThrow(try serverChannel.close().wait())
215-
}
216-
let channel = try SocketChannel(eventLoop: group.next() as! SelectableEventLoop, protocolFamily: PF_INET)
216+
let eventLoop = group.next()
217+
let connectPromise: EventLoopPromise<Void> = eventLoop.newPromise()
218+
219+
let channel = try SocketChannel(socket: ConnectSocket(promise: connectPromise), parent: nil, eventLoop: eventLoop as! SelectableEventLoop)
217220
let promise: EventLoopPromise<Void> = channel.eventLoop.newPromise()
218221

219222
XCTAssertNoThrow(try channel.pipeline.add(handler: ActiveVerificationHandler(promise)).then {
220223
channel.register()
221224
}.then {
222-
channel.connect(to: serverChannel.localAddress!)
225+
channel.connect(to: try! SocketAddress(ipAddress: "127.0.0.1", port: 9999))
226+
}.then {
227+
channel.close()
223228
}.wait())
224229

225-
XCTAssertNoThrow(try channel.close().wait())
226230
XCTAssertNoThrow(try channel.closeFuture.wait())
227231
XCTAssertNoThrow(try promise.futureResult.wait())
232+
XCTAssertNoThrow(try connectPromise.futureResult.wait())
228233
}
229234

230235
public func testWriteServerSocketChannel() throws {

0 commit comments

Comments
 (0)