Files
swift-nio-extras/Tests/NIOExtrasTests/QuiescingHelperTest.swift
T
Johannes Weiß 4b14b7f2b1 quiescing helper & demo (#2)
Motivation:

In a few cases quiescing a server application is useful but it's harder
than necessary with core-NIO. Therefore this adds a helper & a
demonstration.

Modifications:

- add `QuiescingHelper` which helps users to quiesce a channel by
  collecting all accepted channels and when needed sends them the
  quiescing user event. When all collected channels have closed the
  user will be notified and can just shut down the ELG.
- added a demo implementation with a simple HTTP server that quiesces
  when receiving a signal

Result:

Make it quite easy to quiesce a server and show users how to do it.
2018-05-17 20:27:15 +02:00

88 lines
3.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOExtras
public class QuiescingHelperTest: XCTestCase {
func testShutdownIsImmediateWhenNoChannelsCollected() throws {
let el = EmbeddedEventLoop()
let channel = EmbeddedChannel(handler: nil, loop: el)
// let's activate the server channel, nothing actually happens as this is an EmbeddedChannel
XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 1)).wait())
XCTAssertTrue(channel.isActive)
let quiesce = ServerQuiescingHelper(group: el)
_ = quiesce.makeServerChannelHandler(channel: channel)
let p: EventLoopPromise<Void> = el.newPromise()
quiesce.initiateShutdown(promise: p)
XCTAssertNoThrow(try p.futureResult.wait())
XCTAssertFalse(channel.isActive)
}
func testQuiesceUserEventReceivedOnShutdown() throws {
class WaitForQuiesceUserEvent: ChannelInboundHandler {
typealias InboundIn = Never
private let promise: EventLoopPromise<Void>
init(promise: EventLoopPromise<Void>) {
self.promise = promise
}
func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
if event is ChannelShouldQuiesceEvent {
self.promise.succeed(result: ())
}
}
}
let el = EmbeddedEventLoop()
let allShutdownPromise: EventLoopPromise<Void> = el.newPromise()
let serverChannel = EmbeddedChannel(handler: nil, loop: el)
// let's activate the server channel, nothing actually happens as this is an EmbeddedChannel
XCTAssertNoThrow(try serverChannel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 1)).wait())
let quiesce = ServerQuiescingHelper(group: el)
let collectionHandler = quiesce.makeServerChannelHandler(channel: serverChannel)
XCTAssertNoThrow(try serverChannel.pipeline.add(handler: collectionHandler).wait())
var waitForFutures: [EventLoopFuture<Void>] = []
var childChannels: [Channel] = []
// add a bunch of channels
for _ in 0..<128 {
let waitForPromise: EventLoopPromise<()> = el.newPromise()
let channel = EmbeddedChannel(handler: WaitForQuiesceUserEvent(promise: waitForPromise), loop: el)
waitForFutures.append(waitForPromise.futureResult)
childChannels.append(channel)
serverChannel.pipeline.fireChannelRead(NIOAny(channel))
}
// check that the server channel is active before initiating the shutdown
XCTAssertTrue(serverChannel.isActive)
quiesce.initiateShutdown(promise: allShutdownPromise)
// check that the server channel is closed as the first thing
XCTAssertFalse(serverChannel.isActive)
el.run()
// check that all the child channels have received the user event
XCTAssertNoThrow(try EventLoopFuture<Void>.andAll(waitForFutures, eventLoop: el).wait() as Void)
// now close all the child channels
childChannels.forEach { $0.close(promise: nil) }
el.run()
// check that the shutdown has completed
XCTAssertNoThrow(try allShutdownPromise.futureResult.wait())
}
}