Skip to content

Commit 602989b

Browse files
authored
Implement remoteAddress0 and localAddress0 on EmbeddedChannel (#2345)
# Motivation `EmbeddedChannel` is often used in testing and currently any code under testing that uses `context.localAddress` cannot be mocked, since `EmbeddedChannelCore` is always throwing. # Modification Use the same values for `localAddress` in `localAddress0()`. Same for `remoteAddress` # Result We can now properly test code that needs local/remote addresses with `EmbeddedChannel`
1 parent 184df88 commit 602989b

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

Sources/NIOEmbedded/Embedded.swift

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,30 @@ class EmbeddedChannelCore: ChannelCore {
299299
@usableFromInline
300300
var inboundBufferConsumer: Deque<(NIOAny) -> Void> = []
301301

302+
@usableFromInline
303+
var localAddress: SocketAddress?
304+
305+
@usableFromInline
306+
var remoteAddress: SocketAddress?
307+
302308
@usableFromInline
303309
func localAddress0() throws -> SocketAddress {
304310
self.eventLoop.preconditionInEventLoop()
305-
throw ChannelError.operationUnsupported
311+
if let localAddress = self.localAddress {
312+
return localAddress
313+
} else {
314+
throw ChannelError.operationUnsupported
315+
}
306316
}
307317

308318
@usableFromInline
309319
func remoteAddress0() throws -> SocketAddress {
310320
self.eventLoop.preconditionInEventLoop()
311-
throw ChannelError.operationUnsupported
321+
if let remoteAddress = self.remoteAddress {
322+
return remoteAddress
323+
} else {
324+
throw ChannelError.operationUnsupported
325+
}
312326
}
313327

314328
@usableFromInline
@@ -615,10 +629,24 @@ public final class EmbeddedChannel: Channel {
615629
public var embeddedEventLoop: EmbeddedEventLoop = EmbeddedEventLoop()
616630

617631
/// - see: `Channel.localAddress`
618-
public var localAddress: SocketAddress? = nil
632+
public var localAddress: SocketAddress? {
633+
get {
634+
self.channelcore.localAddress
635+
}
636+
set {
637+
self.channelcore.localAddress = newValue
638+
}
639+
}
619640

620641
/// - see: `Channel.remoteAddress`
621-
public var remoteAddress: SocketAddress? = nil
642+
public var remoteAddress: SocketAddress? {
643+
get {
644+
self.channelcore.remoteAddress
645+
}
646+
set {
647+
self.channelcore.remoteAddress = newValue
648+
}
649+
}
622650

623651
/// `nil` because `EmbeddedChannel`s don't have parents.
624652
public let parent: Channel? = nil

Tests/NIOEmbeddedTests/EmbeddedChannelTest+XCTest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftNIO open source project
44
//
5-
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
5+
// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -52,6 +52,8 @@ extension EmbeddedChannelTest {
5252
("testEmbeddedChannelWritabilityIsWritable", testEmbeddedChannelWritabilityIsWritable),
5353
("testFinishWithRecursivelyScheduledTasks", testFinishWithRecursivelyScheduledTasks),
5454
("testSyncOptionsAreSupported", testSyncOptionsAreSupported),
55+
("testLocalAddress0", testLocalAddress0),
56+
("testRemoteAddress0", testRemoteAddress0),
5557
]
5658
}
5759
}

Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,30 @@ class EmbeddedChannelTest: XCTestCase {
489489
XCTAssertEqual(try options?.getOption(ChannelOptions.autoRead), true)
490490
// (Setting options isn't supported.)
491491
}
492+
493+
func testLocalAddress0() throws {
494+
let channel = EmbeddedChannel()
495+
496+
XCTAssertThrowsError(try channel._channelCore.localAddress0()) { error in
497+
XCTAssertEqual(error as? ChannelError, ChannelError.operationUnsupported)
498+
}
499+
500+
let localAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 1234)
501+
channel.localAddress = localAddress
502+
503+
XCTAssertEqual(try channel._channelCore.localAddress0(), localAddress)
504+
}
505+
506+
func testRemoteAddress0() throws {
507+
let channel = EmbeddedChannel()
508+
509+
XCTAssertThrowsError(try channel._channelCore.remoteAddress0()) { error in
510+
XCTAssertEqual(error as? ChannelError, ChannelError.operationUnsupported)
511+
}
512+
513+
let remoteAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 1234)
514+
channel.remoteAddress = remoteAddress
515+
516+
XCTAssertEqual(try channel._channelCore.remoteAddress0(), remoteAddress)
517+
}
492518
}

0 commit comments

Comments
 (0)