From dd5971e17c697cfe338d3d5f7148137ff58bb19e Mon Sep 17 00:00:00 2001 From: kenta okamura Date: Wed, 24 Apr 2024 23:24:36 +0900 Subject: [PATCH 1/3] Fix global cancelOnGracefulShutdown return type --- Sources/ServiceLifecycle/GracefulShutdown.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ServiceLifecycle/GracefulShutdown.swift b/Sources/ServiceLifecycle/GracefulShutdown.swift index e455087..d7aa81a 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 cancelOnGracefulShutdown(_ 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() From f5c32513b15f957789000c79269614c394549703 Mon Sep 17 00:00:00 2001 From: kenta okamura Date: Fri, 26 Apr 2024 21:13:25 +0900 Subject: [PATCH 2/3] Deprecate cancelOnGracefulShutdown and add cancelWhenGracefulShutdown --- Sources/ServiceLifecycle/GracefulShutdown.swift | 7 ++++++- .../GracefulShutdownTests.swift | 16 ++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Sources/ServiceLifecycle/GracefulShutdown.swift b/Sources/ServiceLifecycle/GracefulShutdown.swift index d7aa81a..abd4d6c 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,11 @@ public func cancelOnGracefulShutdown(_ operation: @Sendable @escapi } } +@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 { From 752ca5702ccd0a2fee555d6e0820ce59328d6f77 Mon Sep 17 00:00:00 2001 From: kenta okamura Date: Fri, 26 Apr 2024 21:19:04 +0900 Subject: [PATCH 3/3] Add doc comment even for the deprecated function --- Sources/ServiceLifecycle/GracefulShutdown.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/ServiceLifecycle/GracefulShutdown.swift b/Sources/ServiceLifecycle/GracefulShutdown.swift index abd4d6c..c8056b0 100644 --- a/Sources/ServiceLifecycle/GracefulShutdown.swift +++ b/Sources/ServiceLifecycle/GracefulShutdown.swift @@ -154,6 +154,9 @@ public func cancelWhenGracefulShutdown(_ operation: @Sendable @esca } } +/// 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)