diff --git a/Sources/ServiceLifecycle/GracefulShutdown.swift b/Sources/ServiceLifecycle/GracefulShutdown.swift index e455087..c8056b0 100644 --- a/Sources/ServiceLifecycle/GracefulShutdown.swift +++ b/Sources/ServiceLifecycle/GracefulShutdown.swift @@ -115,7 +115,7 @@ enum ValueOrGracefulShutdown: Sendable { /// Cancels the closure when a graceful shutdown was triggered. /// /// - Parameter operation: The actual operation. -public func cancelOnGracefulShutdown(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T? { +public func cancelWhenGracefulShutdown(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T { return try await withThrowingTaskGroup(of: ValueOrGracefulShutdown.self) { group in group.addTask { let value = try await operation() @@ -154,6 +154,14 @@ public func cancelOnGracefulShutdown(_ operation: @Sendable @escapi } } +/// Cancels the closure when a graceful shutdown was triggered. +/// +/// - Parameter operation: The actual operation. +@available(*, deprecated, renamed: "cancelWhenGracefulShutdown") +public func cancelOnGracefulShutdown(_ 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 /// diff --git a/Tests/ServiceLifecycleTests/GracefulShutdownTests.swift b/Tests/ServiceLifecycleTests/GracefulShutdownTests.swift index d71892b..c281cf7 100644 --- a/Tests/ServiceLifecycleTests/GracefulShutdownTests.swift +++ b/Tests/ServiceLifecycleTests/GracefulShutdownTests.swift @@ -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) } } @@ -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" } @@ -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 { @@ -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 {