Skip to content

Commit

Permalink
ServerQuiescingHelper no longer leaking promises (#192)
Browse files Browse the repository at this point in the history
* ServerQuiescingHelper no longer leaking promises

Motivation:
ServerQuiescingHelper leaked promises when promise left scope and not succeeded

Modifications:
Failing promise within a deinit

* Minor fixes

* generating linux tests

* mini update
  • Loading branch information
carolinacass authored Feb 13, 2023
1 parent 98378d1 commit 6bd9bf5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Sources/NIOExtras/QuiescingHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ private final class CollectAcceptedChannelsHandler: ChannelInboundHandler {
/// try fullyShutdownPromise.futureResult.wait()
///
public final class ServerQuiescingHelper {
/// The `ServerQuiescingHelper` was never used to create a channel handler.
public struct UnusedQuiescingHelperError: Error {}
private let channelCollectorPromise: EventLoopPromise<ChannelCollector>

/// Initialize with a given `EventLoopGroup`.
Expand All @@ -226,6 +228,10 @@ public final class ServerQuiescingHelper {
self.channelCollectorPromise = group.next().makePromise()
}

deinit {
self.channelCollectorPromise.fail(UnusedQuiescingHelperError())
}

/// Create the `ChannelHandler` for the server `channel` to collect all accepted child `Channel`s.
///
/// - parameters:
Expand Down
3 changes: 2 additions & 1 deletion Tests/NIOExtrasTests/QuiescingHelperTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2018-2022 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2018-2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -30,6 +30,7 @@ extension QuiescingHelperTest {
("testShutdownIsImmediateWhenNoChannelsCollected", testShutdownIsImmediateWhenNoChannelsCollected),
("testQuiesceUserEventReceivedOnShutdown", testQuiesceUserEventReceivedOnShutdown),
("testQuiescingDoesNotSwallowCloseErrorsFromAcceptHandler", testQuiescingDoesNotSwallowCloseErrorsFromAcceptHandler),
("testShutdownIsImmediateWhenPromiseDoesNotSucceed", testShutdownIsImmediateWhenPromiseDoesNotSucceed),
]
}
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/NIOExtrasTests/QuiescingHelperTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,19 @@ public class QuiescingHelperTest: XCTestCase {
XCTAssert(error is DummyError)
}
}

///verifying that the promise fails when goes out of scope for shutdown
func testShutdownIsImmediateWhenPromiseDoesNotSucceed() throws {
let el = EmbeddedEventLoop()

let p: EventLoopPromise<Void> = el.makePromise()

do {
let quiesce = ServerQuiescingHelper(group: el)
quiesce.initiateShutdown(promise: p)
}
XCTAssertThrowsError(try p.futureResult.wait()) { error in
XCTAssertTrue(error is ServerQuiescingHelper.UnusedQuiescingHelperError)
}
}
}

0 comments on commit 6bd9bf5

Please sign in to comment.