Skip to content
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

Propagate service error when gracefully shutting down #152

Merged
merged 1 commit into from
Aug 22, 2023
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
13 changes: 7 additions & 6 deletions Sources/ServiceLifecycle/ServiceGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,25 +309,25 @@ public actor ServiceGroup: Sendable {
}
}

case .serviceThrew(let service, let index, let error):
case .serviceThrew(let service, let index, let serviceError):
switch service.failureTerminationBehavior.behavior {
case .cancelGroup:
self.logger.debug(
"Service threw error. Cancelling group.",
metadata: [
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
self.loggingConfiguration.keys.errorKey: "\(error)",
self.loggingConfiguration.keys.errorKey: "\(serviceError)",
]
)
group.cancelAll()
return .failure(error)
return .failure(serviceError)

case .gracefullyShutdownGroup:
self.logger.debug(
"Service threw error. Shutting down group.",
metadata: [
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
self.loggingConfiguration.keys.errorKey: "\(error)",
self.loggingConfiguration.keys.errorKey: "\(serviceError)",
]
)
services[index] = nil
Expand All @@ -338,16 +338,17 @@ public actor ServiceGroup: Sendable {
group: &group,
gracefulShutdownManagers: gracefulShutdownManagers
)
return .failure(serviceError)
} catch {
return .failure(error)
return .failure(serviceError)
}

case .ignore:
self.logger.debug(
"Service threw error.",
metadata: [
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
self.loggingConfiguration.keys.errorKey: "\(error)",
self.loggingConfiguration.keys.errorKey: "\(serviceError)",
]
)
services[index] = nil
Expand Down
67 changes: 66 additions & 1 deletion Tests/ServiceLifecycleTests/ServiceGroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ final class ServiceGroupTests: XCTestCase {
]
)

await withThrowingTaskGroup(of: Void.self) { group in
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await serviceGroup.run()
}
Expand Down Expand Up @@ -474,6 +474,71 @@ final class ServiceGroupTests: XCTestCase {

// Let's exit from the first service
await service1.resumeRunContinuation(with: .success(()))

try await XCTAsyncAssertThrowsError(await group.next()) {
XCTAssertTrue($0 is ExampleError)
}
}
}

func testRun_whenServiceThrows_andShutdownGracefully_andOtherServiceThrows() async throws {
let service1 = MockService(description: "Service1")
let service2 = MockService(description: "Service2")
let service3 = MockService(description: "Service3")
let serviceGroup = self.makeServiceGroup(
services: [
.init(service: service1),
.init(service: service2, failureTerminationBehavior: .gracefullyShutdownGroup),
.init(service: service3),
]
)

try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await serviceGroup.run()
}

var eventIterator1 = service1.events.makeAsyncIterator()
await XCTAsyncAssertEqual(await eventIterator1.next(), .run)

var eventIterator2 = service2.events.makeAsyncIterator()
await XCTAsyncAssertEqual(await eventIterator2.next(), .run)

var eventIterator3 = service3.events.makeAsyncIterator()
await XCTAsyncAssertEqual(await eventIterator3.next(), .run)

await service2.resumeRunContinuation(with: .failure(ExampleError()))

// The last service should receive the shutdown signal first
await XCTAsyncAssertEqual(await eventIterator3.next(), .shutdownGracefully)

// Waiting to see that all two are still running
service1.sendPing()
service3.sendPing()
await XCTAsyncAssertEqual(await eventIterator1.next(), .runPing)
await XCTAsyncAssertEqual(await eventIterator3.next(), .runPing)

// Let's exit from the last service
await service3.resumeRunContinuation(with: .success(()))

// Waiting to see that the remaining is still running
service1.sendPing()
await XCTAsyncAssertEqual(await eventIterator1.next(), .runPing)

// The first service should now receive the signal
await XCTAsyncAssertEqual(await eventIterator1.next(), .shutdownGracefully)

// Waiting to see that the one remaining are still running
service1.sendPing()
await XCTAsyncAssertEqual(await eventIterator1.next(), .runPing)

// Let's throw from this service as well
struct OtherError: Error {}
await service1.resumeRunContinuation(with: .failure(OtherError()))

try await XCTAsyncAssertThrowsError(await group.next()) {
XCTAssertTrue($0 is ExampleError)
}
}
}

Expand Down