mirror of
https://github.com/swift-server/swift-service-lifecycle
synced 2026-11-26 04:57:53 +00:00
Motivation: Adding or raising the deployment platforms in the package manifest is a SemVer major breaking change as consumers must also add or raise their deployment platforms. This is a known limitation of SwiftPM. Unforunately this means that it's very difficult for non-leaf packages to adopt packages which declare their platforms in the manifest. Doing so puts the brakes on adoption and ecosystem growth. For 'core' packages like this one availability constraints should be expressed on declarations rather than in the manifest. Modifications: - Remove platforms from the package manifest - Add availability annotations to types which require it Result: This package can be more widely adopted
47 lines
2.1 KiB
Swift
47 lines
2.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftServiceLifecycle open source project
|
|
//
|
|
// Copyright (c) 2023 Apple Inc. and the SwiftServiceLifecycle project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftServiceLifecycle project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@_spi(TestKit) import ServiceLifecycle
|
|
|
|
/// This struct is used in testing graceful shutdown.
|
|
///
|
|
/// It is passed to the `operation` closure of the ``testGracefulShutdown(operation:)`` method and allows
|
|
/// to trigger the graceful shutdown for testing purposes.
|
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
|
public struct GracefulShutdownTestTrigger: Sendable {
|
|
private let gracefulShutdownManager: GracefulShutdownManager
|
|
|
|
init(gracefulShutdownManager: GracefulShutdownManager) {
|
|
self.gracefulShutdownManager = gracefulShutdownManager
|
|
}
|
|
|
|
/// Triggers the graceful shutdown.
|
|
public func triggerGracefulShutdown() {
|
|
self.gracefulShutdownManager.shutdownGracefully()
|
|
}
|
|
}
|
|
|
|
/// Use this method for testing your graceful shutdown behaviour.
|
|
///
|
|
/// Call the code that you want to test inside the `operation` closure and trigger the graceful shutdown by calling ``GracefulShutdownTestTrigger/triggerGracefulShutdown()``
|
|
/// on the ``GracefulShutdownTestTrigger`` that is passed to the `operation` closure.
|
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
|
public func testGracefulShutdown<T>(operation: (GracefulShutdownTestTrigger) async throws -> T) async rethrows -> T {
|
|
let gracefulShutdownManager = GracefulShutdownManager()
|
|
return try await TaskLocals.$gracefulShutdownManager.withValue(gracefulShutdownManager) {
|
|
let gracefulShutdownTestTrigger = GracefulShutdownTestTrigger(gracefulShutdownManager: gracefulShutdownManager)
|
|
return try await operation(gracefulShutdownTestTrigger)
|
|
}
|
|
}
|