Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close stream channels when the parent channel becomes inactive #131

Merged
merged 1 commit into from
May 22, 2019
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
7 changes: 7 additions & 0 deletions Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
context.fireChannelActive()
}

public func channelInactive(context: ChannelHandlerContext) {
for channel in self.streams.values {
channel.receiveStreamClosed(nil)
}
context.fireChannelInactive()
}

public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
switch event {
case let evt as StreamClosedEvent:
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOHTTP2Tests/ConfiguringPipelineTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extension ConfiguringPipelineTests {
("testBasicPipelineCommunicates", testBasicPipelineCommunicates),
("testPipelineRespectsPositionRequest", testPipelineRespectsPositionRequest),
("testPreambleGetsWrittenOnce", testPreambleGetsWrittenOnce),
("testClosingParentChannelClosesStreamChannel", testClosingParentChannelClosesStreamChannel),
]
}
}
Expand Down
43 changes: 43 additions & 0 deletions Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,47 @@ class ConfiguringPipelineTests: XCTestCase {
// We don't expect anything else at this point.
XCTAssertNil(try self.serverChannel.readOutbound(as: IOData.self))
}

func testClosingParentChannelClosesStreamChannel() throws {
/// A channel handler that succeeds a promise when the channel becomes inactive.
final class InactiveHandler: ChannelInboundHandler {
typealias InboundIn = Any

let inactivePromise: EventLoopPromise<Void>

init(inactivePromise: EventLoopPromise<Void>) {
self.inactivePromise = inactivePromise
}

func channelInactive(context: ChannelHandlerContext) {
inactivePromise.succeed(())
}
}

let clientMultiplexer = try assertNoThrowWithValue(self.clientChannel.configureHTTP2Pipeline(mode: .client).wait())
XCTAssertNoThrow(try self.serverChannel.configureHTTP2Pipeline(mode: .server).wait())

XCTAssertNoThrow(try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel))

let inactivePromise: EventLoopPromise<Void> = self.clientChannel.eventLoop.makePromise()
let streamChannelPromise: EventLoopPromise<Channel> = self.clientChannel.eventLoop.makePromise()

clientMultiplexer.createStreamChannel(promise: streamChannelPromise) { channel, _ in
return channel.pipeline.addHandler(InactiveHandler(inactivePromise: inactivePromise))
}

(self.clientChannel.eventLoop as! EmbeddedEventLoop).run()

let streamChannel = try assertNoThrowWithValue(try streamChannelPromise.futureResult.wait())
// Close the parent channel, not the stream channel.
XCTAssertNoThrow(try self.clientChannel.close().wait())

XCTAssertNoThrow(try inactivePromise.futureResult.wait())
XCTAssertFalse(streamChannel.isActive)

XCTAssertThrowsError(try self.clientChannel.finish()) { error in
XCTAssertEqual(error as? ChannelError, .alreadyClosed)
}
XCTAssertNoThrow(try self.serverChannel.finish())
}
}