Skip to content

Commit 877d749

Browse files
authored
Global cancelOnGracefulShutdown returns non nil value (#181)
* Fix global cancelOnGracefulShutdown return type * Deprecate cancelOnGracefulShutdown and add cancelWhenGracefulShutdown * Add doc comment even for the deprecated function
1 parent f5780d9 commit 877d749

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Sources/ServiceLifecycle/GracefulShutdown.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ enum ValueOrGracefulShutdown<T: Sendable>: Sendable {
115115
/// Cancels the closure when a graceful shutdown was triggered.
116116
///
117117
/// - Parameter operation: The actual operation.
118-
public func cancelOnGracefulShutdown<T: Sendable>(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T? {
118+
public func cancelWhenGracefulShutdown<T: Sendable>(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T {
119119
return try await withThrowingTaskGroup(of: ValueOrGracefulShutdown<T>.self) { group in
120120
group.addTask {
121121
let value = try await operation()
@@ -154,6 +154,14 @@ public func cancelOnGracefulShutdown<T: Sendable>(_ operation: @Sendable @escapi
154154
}
155155
}
156156

157+
/// Cancels the closure when a graceful shutdown was triggered.
158+
///
159+
/// - Parameter operation: The actual operation.
160+
@available(*, deprecated, renamed: "cancelWhenGracefulShutdown")
161+
public func cancelOnGracefulShutdown<T: Sendable>(_ operation: @Sendable @escaping () async throws -> T) async rethrows -> T? {
162+
return try await cancelWhenGracefulShutdown(operation)
163+
}
164+
157165
extension Task where Success == Never, Failure == Never {
158166
/// A Boolean value that indicates whether the task is gracefully shutting down
159167
///

Tests/ServiceLifecycleTests/GracefulShutdownTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ final class GracefulShutdownTests: XCTestCase {
215215
}
216216
}
217217

218-
func testCancelOnGracefulShutdown() async throws {
218+
func testCancelWhenGracefulShutdown() async throws {
219219
try await testGracefulShutdown { gracefulShutdownTestTrigger in
220220
try await withThrowingTaskGroup(of: Void.self) { group in
221221
group.addTask {
222-
try await cancelOnGracefulShutdown {
222+
try await cancelWhenGracefulShutdown {
223223
try await Task.sleep(nanoseconds: 1_000_000_000_000)
224224
}
225225
}
@@ -233,9 +233,9 @@ final class GracefulShutdownTests: XCTestCase {
233233
}
234234
}
235235

236-
func testResumesCancelOnGracefulShutdownWithResult() async throws {
236+
func testResumesCancelWhenGracefulShutdownWithResult() async throws {
237237
await testGracefulShutdown { _ in
238-
let result = await cancelOnGracefulShutdown {
238+
let result = await cancelWhenGracefulShutdown {
239239
await Task.yield()
240240
return "hello"
241241
}
@@ -354,11 +354,11 @@ final class GracefulShutdownTests: XCTestCase {
354354
}
355355
}
356356

357-
func testCancelOnGracefulShutdownSurvivesCancellation() async throws {
357+
func testCancelWhenGracefulShutdownSurvivesCancellation() async throws {
358358
await withTaskGroup(of: Void.self) { group in
359359
group.addTask {
360360
await withGracefulShutdownHandler {
361-
await cancelOnGracefulShutdown {
361+
await cancelWhenGracefulShutdown {
362362
await OnlyCancellationWaiter().cancellation
363363

364364
try! await uncancellable {
@@ -374,14 +374,14 @@ final class GracefulShutdownTests: XCTestCase {
374374
}
375375
}
376376

377-
func testCancelOnGracefulShutdownSurvivesErrorThrown() async throws {
377+
func testCancelWhenGracefulShutdownSurvivesErrorThrown() async throws {
378378
struct MyError: Error, Equatable {}
379379

380380
await withTaskGroup(of: Void.self) { group in
381381
group.addTask {
382382
do {
383383
try await withGracefulShutdownHandler {
384-
try await cancelOnGracefulShutdown {
384+
try await cancelWhenGracefulShutdown {
385385
await OnlyCancellationWaiter().cancellation
386386

387387
try! await uncancellable {

0 commit comments

Comments
 (0)