Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sources/NIOEmbedded/AsyncTestingChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ public final class NIOAsyncTestingChannel: Channel {
/// - address: The address to fake-bind to.
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
promise?.futureResult.whenSuccess {
let promise = promise ?? self.testingEventLoop.makePromise()
promise.futureResult.whenSuccess {
self.localAddress = address
}
if self.eventLoop.inEventLoop {
Expand All @@ -637,7 +638,8 @@ public final class NIOAsyncTestingChannel: Channel {
/// - address: The address to fake-bind to.
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
public func connect(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
promise?.futureResult.whenSuccess {
let promise = promise ?? self.testingEventLoop.makePromise()
promise.futureResult.whenSuccess {
self.remoteAddress = address
}
if self.eventLoop.inEventLoop {
Expand Down
6 changes: 4 additions & 2 deletions Sources/NIOEmbedded/Embedded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,8 @@ public final class EmbeddedChannel: Channel {
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
self.embeddedEventLoop.checkCorrectThread()
promise?.futureResult.whenSuccess {
let promise = promise ?? self.embeddedEventLoop.makePromise()
promise.futureResult.whenSuccess {
self.localAddress = address
}
self.pipeline.bind(to: address, promise: promise)
Expand All @@ -1075,7 +1076,8 @@ public final class EmbeddedChannel: Channel {
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
public func connect(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
self.embeddedEventLoop.checkCorrectThread()
promise?.futureResult.whenSuccess {
let promise = promise ?? self.embeddedEventLoop.makePromise()
promise.futureResult.whenSuccess {
self.remoteAddress = address
}
self.pipeline.connect(to: address, promise: promise)
Expand Down
20 changes: 18 additions & 2 deletions Tests/NIOEmbeddedTests/AsyncTestingChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ class AsyncTestingChannelTests: XCTestCase {
}

func testSetLocalAddressAfterSuccessfulBind() throws {

let channel = NIOAsyncTestingChannel()
let bindPromise = channel.eventLoop.makePromise(of: Void.self)
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
Expand All @@ -454,11 +453,19 @@ class AsyncTestingChannelTests: XCTestCase {
XCTAssertEqual(channel.localAddress, socketAddress)
}
try bindPromise.futureResult.wait()
}

func testSetLocalAddressAfterSuccessfulBindWithoutPromise() throws {
let channel = NIOAsyncTestingChannel()
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
// Call bind on-loop so we know when to expect the result
try channel.testingEventLoop.submit {
channel.bind(to: socketAddress, promise: nil)
}.wait()
XCTAssertEqual(channel.localAddress, socketAddress)
}

func testSetRemoteAddressAfterSuccessfulConnect() throws {

let channel = NIOAsyncTestingChannel()
let connectPromise = channel.eventLoop.makePromise(of: Void.self)
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
Expand All @@ -467,7 +474,16 @@ class AsyncTestingChannelTests: XCTestCase {
XCTAssertEqual(channel.remoteAddress, socketAddress)
}
try connectPromise.futureResult.wait()
}

func testSetRemoteAddressAfterSuccessfulConnectWithoutPromise() throws {
let channel = NIOAsyncTestingChannel()
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
// Call connect on-loop so we know when to expect the result
try channel.testingEventLoop.submit {
channel.connect(to: socketAddress, promise: nil)
}.wait()
XCTAssertEqual(channel.remoteAddress, socketAddress)
}

func testUnprocessedOutboundUserEventFailsOnEmbeddedChannel() throws {
Expand Down
14 changes: 14 additions & 0 deletions Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ class EmbeddedChannelTest: XCTestCase {
try bindPromise.futureResult.wait()
}

func testSetLocalAddressAfterSuccessfulBindWithoutPromise() throws {
let channel = EmbeddedChannel()
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
channel.bind(to: socketAddress, promise: nil)
XCTAssertEqual(channel.localAddress, socketAddress)
}

func testSetRemoteAddressAfterSuccessfulConnect() throws {
let channel = EmbeddedChannel()
let connectPromise = channel.eventLoop.makePromise(of: Void.self)
Expand All @@ -474,6 +481,13 @@ class EmbeddedChannelTest: XCTestCase {
try connectPromise.futureResult.wait()
}

func testSetRemoteAddressAfterSuccessfulConnectWithoutPromise() throws {
let channel = EmbeddedChannel()
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
channel.connect(to: socketAddress, promise: nil)
XCTAssertEqual(channel.remoteAddress, socketAddress)
}

func testUnprocessedOutboundUserEventFailsOnEmbeddedChannel() {
let channel = EmbeddedChannel()
XCTAssertThrowsError(try channel.triggerUserOutboundEvent("event").wait()) { (error: Error) in
Expand Down
Loading