Skip to content

Commit

Permalink
If connectSocket() completes directly we need to mark channel as acti…
Browse files Browse the repository at this point in the history
…ve (#205)

Motivation:

If connectSocket() completes directly we failed to mark the Channel active by calling becomeActive0(...).

Modifications:

Correctly call becomeActive0(...)

Result:

Mark Channel active if connect succeed directly.
  • Loading branch information
normanmaurer authored and Lukasa committed Mar 21, 2018
1 parent 88b9125 commit 8bf013d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/NIO/SocketChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
registerForWritable()
} else {
self.updateCachedAddressesFromSocket()
promise?.succeed(result: ())
becomeActive0(promise: promise)
}
} catch let error {
promise?.fail(error: error)
Expand Down Expand Up @@ -854,7 +854,7 @@ final class SocketChannel: BaseSocketChannel<Socket> {
return .socketChannel(self, interested)
}

fileprivate init(socket: Socket, parent: Channel? = nil, eventLoop: SelectableEventLoop) throws {
init(socket: Socket, parent: Channel? = nil, eventLoop: SelectableEventLoop) throws {
self.pendingWrites = PendingStreamWritesManager(iovecs: eventLoop.iovecs, storageRefs: eventLoop.storageRefs)
try super.init(socket: socket, parent: parent, eventLoop: eventLoop, recvAllocator: AdaptiveRecvByteBufferAllocator())
}
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOTests/SocketChannelTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extension SocketChannelTest {
("testAcceptFailsWithENOMEM", testAcceptFailsWithENOMEM),
("testAcceptFailsWithEFAULT", testAcceptFailsWithEFAULT),
("testSetGetOptionClosedServerSocketChannel", testSetGetOptionClosedServerSocketChannel),
("testConnect", testConnect),
]
}
}
Expand Down
43 changes: 43 additions & 0 deletions Tests/NIOTests/SocketChannelTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,47 @@ public class SocketChannelTest : XCTestCase {
try assertSetGetOptionOnOpenAndClosed(channel: clientChannel, option: ChannelOptions.allowRemoteHalfClosure, value: true)
try assertSetGetOptionOnOpenAndClosed(channel: serverChannel, option: ChannelOptions.backlog, value: 100)
}

public func testConnect() throws {
final class ActiveVerificationHandler: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
typealias InboundOut = ByteBuffer

private let promise: EventLoopPromise<Void>

init(_ promise: EventLoopPromise<Void>) {
self.promise = promise
}

func channelActive(ctx: ChannelHandlerContext) {
promise.succeed(result: ())
}
}

class ConnectSocket: Socket {
init() throws {
try super.init(protocolFamily: PF_INET, type: Posix.SOCK_STREAM)
}

override func connect(to address: SocketAddress) throws -> Bool {
return true
}
}

let group = MultiThreadedEventLoopGroup(numThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
let socket = try ConnectSocket()
let channel = try SocketChannel(socket: socket, eventLoop: group.next() as! SelectableEventLoop)
let promise: EventLoopPromise<Void> = channel.eventLoop.newPromise()

XCTAssertNoThrow(try channel.register().wait())
XCTAssertNoThrow(try channel.pipeline.add(handler: ActiveVerificationHandler(promise)).wait())
XCTAssertNoThrow(try channel.connect(to: SocketAddress.init(ipAddress: "127.0.0.1", port: 0)).wait())

try channel.close().wait()
try channel.closeFuture.wait()
try promise.futureResult.wait()
}
}

0 comments on commit 8bf013d

Please sign in to comment.