EmbeddedChannel should call fireChannelRegistered(...) when register(...) is called. (#34)

Motivation:

At the moment EmbeddedChannel calls fireChannelRegistered() when connect(...) is called. This is not correct, as it should be done when register(...) is called.

Modifications:

- Correctly call `fireChannelRegistered(...)` when register(...) is called
- Simplify init of EmbeddedChannel
- Fix EmbeddedChannelTest

Result:

Correct invocation of methods.
This commit is contained in:
Norman Maurer 2018-02-26 11:57:07 +01:00 committed by Cory Benfield
parent 21065ed8f0
commit 0797b5c293
2 changed files with 19 additions and 12 deletions

View File

@ -196,13 +196,13 @@ class EmbeddedChannelCore : ChannelCore {
func connect0(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
promise?.succeed(result: ())
pipeline.fireChannelRegistered0()
isActive = true
pipeline.fireChannelActive0()
}
func register0(promise: EventLoopPromise<Void>?) {
promise?.succeed(result: ())
pipeline.fireChannelRegistered0()
}
func write0(_ data: NIOAny, promise: EventLoopPromise<Void>?) {
@ -318,16 +318,23 @@ public class EmbeddedChannel : Channel {
return (buffer.removeFirst().forceAs(type: T.self))
}
public convenience init(handler: ChannelHandler, loop: EmbeddedEventLoop = EmbeddedEventLoop()) throws {
self.init(loop: loop)
try _pipeline.add(handler: handler).wait()
}
public init(loop: EmbeddedEventLoop = EmbeddedEventLoop()) {
/// Create a new instance.
///
/// During creation it will automatically also register itself on the `EmbeddedEventLoop`.
///
/// - parameters:
/// - handler: The `ChannelHandler` to add to the `ChannelPipeline` before register or `nil` if none should be added.
/// - loop: The `EmbeddedEventLoop` to use.
public init(handler: ChannelHandler? = nil, loop: EmbeddedEventLoop = EmbeddedEventLoop()) {
self.eventLoop = loop
self._pipeline = ChannelPipeline(channel: self)
// we should just register it directly and this will never throw.
if let handler = handler {
// This will be propagated via fireErrorCaught
_ = try? _pipeline.add(handler: handler).wait()
}
// This will never throw...
_ = try? register().wait()
}

View File

@ -88,12 +88,12 @@ class EmbeddedChannelTest: XCTestCase {
}
func testEmbeddedLifecycle() throws {
let channel = EmbeddedChannel()
let handler = ChannelLifecycleHandler()
XCTAssertEqual(handler.currentState, .unregistered)
_ = try channel.pipeline.add(handler: handler).wait()
XCTAssertEqual(handler.currentState, .unregistered)
let channel = EmbeddedChannel(handler: handler)
XCTAssertEqual(handler.currentState, .registered)
XCTAssertFalse(channel.isActive)
_ = try channel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake")).wait()