-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftServiceLifecycle open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftServiceLifecycle project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftServiceLifecycle project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// An actor that provides a function to wait on cancellation/graceful shutdown. | ||
@usableFromInline | ||
actor CancellationWaiter { | ||
private var taskContinuation: CheckedContinuation<Void, Error>? | ||
|
||
@usableFromInline | ||
init() {} | ||
|
||
@usableFromInline | ||
func wait() async throws { | ||
try await withTaskCancellationHandler { | ||
try await withGracefulShutdownHandler { | ||
try await withCheckedThrowingContinuation { continuation in | ||
self.taskContinuation = continuation | ||
} | ||
} onGracefulShutdown: { | ||
Task { | ||
await self.finish() | ||
} | ||
} | ||
} onCancel: { | ||
Task { | ||
await self.finish(throwing: CancellationError()) | ||
} | ||
} | ||
} | ||
|
||
private func finish(throwing error: Error? = nil) { | ||
if let error { | ||
self.taskContinuation?.resume(throwing: error) | ||
} else { | ||
self.taskContinuation?.resume() | ||
} | ||
self.taskContinuation = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,4 +253,54 @@ final class GracefulShutdownTests: XCTestCase { | |
XCTAssertTrue(Task.isShuttingDownGracefully) | ||
} | ||
} | ||
|
||
func testWaitForGracefulShutdown() async throws { | ||
try await testGracefulShutdown { gracefulShutdownTestTrigger in | ||
try await withThrowingTaskGroup(of: Void.self) { group in | ||
group.addTask { | ||
try await Task.sleep(for: .milliseconds(10)) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
FranzBusch
Contributor
|
||
gracefulShutdownTestTrigger.triggerGracefulShutdown() | ||
} | ||
|
||
try await withGracefulShutdownHandler { | ||
try await gracefulShutdown() | ||
} onGracefulShutdown: { | ||
// No-op | ||
} | ||
|
||
try await group.waitForAll() | ||
} | ||
} | ||
} | ||
|
||
func testWaitForGracefulShutdown_WhenAlreadyShutdown() async throws { | ||
try await testGracefulShutdown { gracefulShutdownTestTrigger in | ||
gracefulShutdownTestTrigger.triggerGracefulShutdown() | ||
|
||
try await withGracefulShutdownHandler { | ||
try await Task.sleep(for: .milliseconds(10)) | ||
try await gracefulShutdown() | ||
} onGracefulShutdown: { | ||
// No-op | ||
} | ||
} | ||
} | ||
|
||
func testWaitForGracefulShutdown_Cancellation() async throws { | ||
do { | ||
try await testGracefulShutdown { _ in | ||
try await withThrowingTaskGroup(of: Void.self) { group in | ||
group.addTask { | ||
try await gracefulShutdown() | ||
} | ||
|
||
group.cancelAll() | ||
try await group.waitForAll() | ||
} | ||
} | ||
XCTFail("Expected CancellationError to be thrown") | ||
} catch { | ||
XCTAssertTrue(error is CancellationError) | ||
} | ||
} | ||
} |
Would it work the same if it was
Task.yield()
instead?