Skip to content

Commit

Permalink
Global cancelOnGracefulShutdown returns non nil value (#181)
Browse files Browse the repository at this point in the history
* Fix global cancelOnGracefulShutdown return type

* Deprecate cancelOnGracefulShutdown and add cancelWhenGracefulShutdown

* Add doc comment even for the deprecated function
  • Loading branch information
sidepelican authored Apr 26, 2024
1 parent f5780d9 commit 877d749
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
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

0 comments on commit 877d749

Please sign in to comment.