From 7ee497fe95ff00e52a653fc626a30c9b41b38057 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 26 Mar 2018 21:31:43 +0200 Subject: [PATCH] Update EmbeddedChannel state before notify connect and close promise. Motivation: We need to update isActive before notify the promises of connect and close to correctly refect Channel.isActive in callbacks. Modifications: - Correctly update isActive before notify promises. - Add unit test. Result: Correct Channel state in callbacks. --- Sources/NIO/Embedded.swift | 4 ++-- .../NIOTests/EmbeddedChannelTest+XCTest.swift | 1 + Tests/NIOTests/EmbeddedChannelTest.swift | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index a0e6c91529..79a75bb5ce 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -175,10 +175,10 @@ class EmbeddedChannelCore: ChannelCore { return } isOpen = false + isActive = false promise?.succeed(result: ()) // As we called register() in the constructor of EmbeddedChannel we also need to ensure we call unregistered here. - isActive = false pipeline.fireChannelInactive0() pipeline.fireChannelUnregistered0() @@ -194,8 +194,8 @@ class EmbeddedChannelCore: ChannelCore { } func connect0(to address: SocketAddress, promise: EventLoopPromise?) { - promise?.succeed(result: ()) isActive = true + promise?.succeed(result: ()) pipeline.fireChannelActive0() } diff --git a/Tests/NIOTests/EmbeddedChannelTest+XCTest.swift b/Tests/NIOTests/EmbeddedChannelTest+XCTest.swift index 4b0dac7b18..6020b4e604 100644 --- a/Tests/NIOTests/EmbeddedChannelTest+XCTest.swift +++ b/Tests/NIOTests/EmbeddedChannelTest+XCTest.swift @@ -35,6 +35,7 @@ extension EmbeddedChannelTest { ("testEmbeddedLifecycle", testEmbeddedLifecycle), ("testEmbeddedChannelAndPipelineAndChannelCoreShareTheEventLoop", testEmbeddedChannelAndPipelineAndChannelCoreShareTheEventLoop), ("testSendingIncorrectDataOnEmbeddedChannel", testSendingIncorrectDataOnEmbeddedChannel), + ("testActiveWhenConnectPromiseFiresAndInactiveWhenClosePromiseFires", testActiveWhenConnectPromiseFiresAndInactiveWhenClosePromiseFires), ] } } diff --git a/Tests/NIOTests/EmbeddedChannelTest.swift b/Tests/NIOTests/EmbeddedChannelTest.swift index 166ca1b13a..13435ccb00 100644 --- a/Tests/NIOTests/EmbeddedChannelTest.swift +++ b/Tests/NIOTests/EmbeddedChannelTest.swift @@ -155,4 +155,23 @@ class EmbeddedChannelTest: XCTestCase { XCTAssertFalse(try channel.finish()) } + + func testActiveWhenConnectPromiseFiresAndInactiveWhenClosePromiseFires() throws { + let channel = EmbeddedChannel() + XCTAssertFalse(channel.isActive) + let connectPromise: EventLoopPromise = channel.eventLoop.newPromise() + connectPromise.futureResult.whenComplete { + XCTAssertTrue(channel.isActive) + } + channel.connect(to: try SocketAddress(ipAddress: "127.0.0.1", port: 0), promise: connectPromise) + try connectPromise.futureResult.wait() + + let closePromise: EventLoopPromise = channel.eventLoop.newPromise() + closePromise.futureResult.whenComplete { + XCTAssertFalse(channel.isActive) + } + + channel.close(promise: closePromise) + try closePromise.futureResult.wait() + } }