|
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift OpenFeature open source project
|
4 | 4 | //
|
5 |
| -// Copyright (c) 2024 the Swift OpenFeature project authors |
| 5 | +// Copyright (c) 2025 the Swift OpenFeature project authors |
6 | 6 | // Licensed under Apache License v2.0
|
7 | 7 | //
|
8 | 8 | // See LICENSE.txt for license information
|
9 | 9 | //
|
10 | 10 | // SPDX-License-Identifier: Apache-2.0
|
11 | 11 | //
|
12 | 12 | //===----------------------------------------------------------------------===//
|
| 13 | + |
| 14 | +import Foundation |
| 15 | +import HTTPTypes |
| 16 | +import OFREP |
| 17 | +import OpenAPIRuntime |
| 18 | +import Testing |
| 19 | +import ServiceLifecycle |
| 20 | +@testable import Logging |
| 21 | + |
| 22 | +@Suite("OFREP Provider") |
| 23 | +final class OFREPProviderTests { |
| 24 | + init() { |
| 25 | + LoggingSystem.bootstrapInternal { label in |
| 26 | + var handler = StreamLogHandler.standardOutput(label: label) |
| 27 | + handler.logLevel = .debug |
| 28 | + return handler |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + deinit { |
| 33 | + LoggingSystem.bootstrapInternal(SwiftLogNoOpLogHandler.init) |
| 34 | + } |
| 35 | + |
| 36 | + @Test("Graceful shutdown") |
| 37 | + func shutsDownTransport() async throws { |
| 38 | + /// A no-op service which is used to shut down the service group upon successful termination. |
| 39 | + struct ShutdownTrigger: Service, CustomStringConvertible { |
| 40 | + let description = "ShutdownTrigger" |
| 41 | + |
| 42 | + func run() async throws {} |
| 43 | + } |
| 44 | + |
| 45 | + let transport = RecordingOFREPClientTransport() |
| 46 | + let provider = OFREPProvider(transport: transport) |
| 47 | + |
| 48 | + await #expect(transport.numberOfShutdownCalls == 0) |
| 49 | + |
| 50 | + let group = ServiceGroup( |
| 51 | + configuration: .init( |
| 52 | + services: [ |
| 53 | + .init(service: provider), |
| 54 | + .init(service: ShutdownTrigger(), successTerminationBehavior: .gracefullyShutdownGroup), |
| 55 | + ], |
| 56 | + logger: Logger(label: "test") |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + try await group.run() |
| 61 | + |
| 62 | + await #expect(transport.numberOfShutdownCalls == 1) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +private actor RecordingOFREPClientTransport: OFREPClientTransport { |
| 67 | + var numberOfShutdownCalls = 0 |
| 68 | + |
| 69 | + func send( |
| 70 | + _ request: HTTPRequest, |
| 71 | + body: HTTPBody?, |
| 72 | + baseURL: URL, |
| 73 | + operationID: String |
| 74 | + ) async throws -> ( |
| 75 | + HTTPResponse, |
| 76 | + HTTPBody? |
| 77 | + ) { |
| 78 | + (HTTPResponse(status: 418), nil) |
| 79 | + } |
| 80 | + |
| 81 | + func shutdownGracefully() async throws { |
| 82 | + numberOfShutdownCalls += 1 |
| 83 | + } |
| 84 | +} |
0 commit comments