Skip to content

Commit 37094b1

Browse files
authored
Embedded channels should set local and remote address always (#3254)
Embedded channels should set local and remote address always ### Motivation: Currently, if connect or bind are called without a promise, the remote/local address does not get set because those were getting set in the whenSuccess of the promise. ### Modifications: If the user didn't provide a promise, create one ourselves, so we have something to listen for. ### Result: Calling connect/bind will always result in remote/local address getting set, regardless of whether you pass a promise
1 parent f3645a0 commit 37094b1

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

Sources/NIOEmbedded/AsyncTestingChannel.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ public final class NIOAsyncTestingChannel: Channel {
617617
/// - address: The address to fake-bind to.
618618
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
619619
public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
620-
promise?.futureResult.whenSuccess {
620+
let promise = promise ?? self.testingEventLoop.makePromise()
621+
promise.futureResult.whenSuccess {
621622
self.localAddress = address
622623
}
623624
if self.eventLoop.inEventLoop {
@@ -637,7 +638,8 @@ public final class NIOAsyncTestingChannel: Channel {
637638
/// - address: The address to fake-bind to.
638639
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
639640
public func connect(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
640-
promise?.futureResult.whenSuccess {
641+
let promise = promise ?? self.testingEventLoop.makePromise()
642+
promise.futureResult.whenSuccess {
641643
self.remoteAddress = address
642644
}
643645
if self.eventLoop.inEventLoop {

Sources/NIOEmbedded/Embedded.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,8 @@ public final class EmbeddedChannel: Channel {
10601060
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
10611061
public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
10621062
self.embeddedEventLoop.checkCorrectThread()
1063-
promise?.futureResult.whenSuccess {
1063+
let promise = promise ?? self.embeddedEventLoop.makePromise()
1064+
promise.futureResult.whenSuccess {
10641065
self.localAddress = address
10651066
}
10661067
self.pipeline.bind(to: address, promise: promise)
@@ -1075,7 +1076,8 @@ public final class EmbeddedChannel: Channel {
10751076
/// - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
10761077
public func connect(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
10771078
self.embeddedEventLoop.checkCorrectThread()
1078-
promise?.futureResult.whenSuccess {
1079+
let promise = promise ?? self.embeddedEventLoop.makePromise()
1080+
promise.futureResult.whenSuccess {
10791081
self.remoteAddress = address
10801082
}
10811083
self.pipeline.connect(to: address, promise: promise)

Tests/NIOEmbeddedTests/AsyncTestingChannelTests.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ class AsyncTestingChannelTests: XCTestCase {
445445
}
446446

447447
func testSetLocalAddressAfterSuccessfulBind() throws {
448-
449448
let channel = NIOAsyncTestingChannel()
450449
let bindPromise = channel.eventLoop.makePromise(of: Void.self)
451450
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
@@ -454,11 +453,19 @@ class AsyncTestingChannelTests: XCTestCase {
454453
XCTAssertEqual(channel.localAddress, socketAddress)
455454
}
456455
try bindPromise.futureResult.wait()
456+
}
457457

458+
func testSetLocalAddressAfterSuccessfulBindWithoutPromise() throws {
459+
let channel = NIOAsyncTestingChannel()
460+
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
461+
// Call bind on-loop so we know when to expect the result
462+
try channel.testingEventLoop.submit {
463+
channel.bind(to: socketAddress, promise: nil)
464+
}.wait()
465+
XCTAssertEqual(channel.localAddress, socketAddress)
458466
}
459467

460468
func testSetRemoteAddressAfterSuccessfulConnect() throws {
461-
462469
let channel = NIOAsyncTestingChannel()
463470
let connectPromise = channel.eventLoop.makePromise(of: Void.self)
464471
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
@@ -467,7 +474,16 @@ class AsyncTestingChannelTests: XCTestCase {
467474
XCTAssertEqual(channel.remoteAddress, socketAddress)
468475
}
469476
try connectPromise.futureResult.wait()
477+
}
470478

479+
func testSetRemoteAddressAfterSuccessfulConnectWithoutPromise() throws {
480+
let channel = NIOAsyncTestingChannel()
481+
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
482+
// Call connect on-loop so we know when to expect the result
483+
try channel.testingEventLoop.submit {
484+
channel.connect(to: socketAddress, promise: nil)
485+
}.wait()
486+
XCTAssertEqual(channel.remoteAddress, socketAddress)
471487
}
472488

473489
func testUnprocessedOutboundUserEventFailsOnEmbeddedChannel() throws {

Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ class EmbeddedChannelTest: XCTestCase {
463463
try bindPromise.futureResult.wait()
464464
}
465465

466+
func testSetLocalAddressAfterSuccessfulBindWithoutPromise() throws {
467+
let channel = EmbeddedChannel()
468+
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
469+
channel.bind(to: socketAddress, promise: nil)
470+
XCTAssertEqual(channel.localAddress, socketAddress)
471+
}
472+
466473
func testSetRemoteAddressAfterSuccessfulConnect() throws {
467474
let channel = EmbeddedChannel()
468475
let connectPromise = channel.eventLoop.makePromise(of: Void.self)
@@ -474,6 +481,13 @@ class EmbeddedChannelTest: XCTestCase {
474481
try connectPromise.futureResult.wait()
475482
}
476483

484+
func testSetRemoteAddressAfterSuccessfulConnectWithoutPromise() throws {
485+
let channel = EmbeddedChannel()
486+
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
487+
channel.connect(to: socketAddress, promise: nil)
488+
XCTAssertEqual(channel.remoteAddress, socketAddress)
489+
}
490+
477491
func testUnprocessedOutboundUserEventFailsOnEmbeddedChannel() {
478492
let channel = EmbeddedChannel()
479493
XCTAssertThrowsError(try channel.triggerUserOutboundEvent("event").wait()) { (error: Error) in

0 commit comments

Comments
 (0)