Skip to content

Global cancelOnGracefulShutdown returns non nil value #181

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

Merged
merged 3 commits into from
Apr 26, 2024
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
10 changes: 9 additions & 1 deletion Sources/ServiceLifecycle/GracefulShutdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum ValueOrGracefulShutdown<T: Sendable>: Sendable {
/// Cancels the closure when a graceful shutdown was triggered.
///
/// - Parameter operation: The actual operation.
public func cancelOnGracefulShutdown<T: Sendable>(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T? {
public func cancelWhenGracefulShutdown<T: Sendable>(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T {
return try await withThrowingTaskGroup(of: ValueOrGracefulShutdown<T>.self) { group in
group.addTask {
let value = try await operation()
Expand Down Expand Up @@ -154,6 +154,14 @@ public func cancelOnGracefulShutdown<T: Sendable>(_ operation: @Sendable @escapi
}
}

/// Cancels the closure when a graceful shutdown was triggered.
///
/// - Parameter operation: The actual operation.
@available(*, deprecated, renamed: "cancelWhenGracefulShutdown")
public func cancelOnGracefulShutdown<T: Sendable>(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T? {
return try await cancelWhenGracefulShutdown(operation)
}

extension Task where Success == Never, Failure == Never {
/// A Boolean value that indicates whether the task is gracefully shutting down
///
Expand Down
16 changes: 8 additions & 8 deletions Tests/ServiceLifecycleTests/GracefulShutdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ final class GracefulShutdownTests: XCTestCase {
}
}

func testCancelOnGracefulShutdown() async throws {
func testCancelWhenGracefulShutdown() async throws {
try await testGracefulShutdown { gracefulShutdownTestTrigger in
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await cancelOnGracefulShutdown {
try await cancelWhenGracefulShutdown {
try await Task.sleep(nanoseconds: 1_000_000_000_000)
}
}
Expand All @@ -233,9 +233,9 @@ final class GracefulShutdownTests: XCTestCase {
}
}

func testResumesCancelOnGracefulShutdownWithResult() async throws {
func testResumesCancelWhenGracefulShutdownWithResult() async throws {
await testGracefulShutdown { _ in
let result = await cancelOnGracefulShutdown {
let result = await cancelWhenGracefulShutdown {
await Task.yield()
return "hello"
}
Expand Down Expand Up @@ -354,11 +354,11 @@ final class GracefulShutdownTests: XCTestCase {
}
}

func testCancelOnGracefulShutdownSurvivesCancellation() async throws {
func testCancelWhenGracefulShutdownSurvivesCancellation() async throws {
await withTaskGroup(of: Void.self) { group in
group.addTask {
await withGracefulShutdownHandler {
await cancelOnGracefulShutdown {
await cancelWhenGracefulShutdown {
await OnlyCancellationWaiter().cancellation

try! await uncancellable {
Expand All @@ -374,14 +374,14 @@ final class GracefulShutdownTests: XCTestCase {
}
}

func testCancelOnGracefulShutdownSurvivesErrorThrown() async throws {
func testCancelWhenGracefulShutdownSurvivesErrorThrown() async throws {
struct MyError: Error, Equatable {}

await withTaskGroup(of: Void.self) { group in
group.addTask {
do {
try await withGracefulShutdownHandler {
try await cancelOnGracefulShutdown {
try await cancelWhenGracefulShutdown {
await OnlyCancellationWaiter().cancellation

try! await uncancellable {
Expand Down