Skip to content

Commit e6b2040

Browse files
author
Andrea Scuderi
committed
Fix Unit Test and Timeout
1 parent 671ad87 commit e6b2040

3 files changed

Lines changed: 38 additions & 34 deletions

File tree

Sources/BreezeDynamoDBService/BreezeDynamoDBService.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,25 @@ public struct BreezeDynamoDBService: BreezeDynamoDBServing {
3333
private let logger: Logger
3434
private let awsClient: AWSClient
3535
private let httpClient: HTTPClient
36-
36+
private let shutdownState: ShutdownState
37+
38+
/// Error types for BreezeDynamoDBService
39+
enum BreezeDynamoDBServiceError: Error {
40+
case alreadyShutdown
41+
}
42+
43+
/// Actor to manage shutdown state safely
44+
private actor ShutdownState {
45+
private var isShutdown = false
46+
47+
func markShutdown() throws {
48+
guard !isShutdown else {
49+
throw BreezeDynamoDBServiceError.alreadyShutdown
50+
}
51+
isShutdown = true
52+
}
53+
}
54+
3755
/// Initializes the BreezeDynamoDBService with the provided configuration.
3856
/// - Parameters:
3957
/// - config: The configuration for the DynamoDB service.
@@ -76,6 +94,7 @@ public struct BreezeDynamoDBService: BreezeDynamoDBServing {
7694
tableName: config.tableName,
7795
keyName: config.keyName
7896
)
97+
self.shutdownState = ShutdownState()
7998
logger.info("DBManager is ready.")
8099
}
81100

@@ -89,9 +108,10 @@ public struct BreezeDynamoDBService: BreezeDynamoDBServing {
89108
/// - Throws: An error if the shutdown process fails.
90109
/// This method ensures that the AWS client and HTTP client are properly shutdown before marking the service as shutdown.
91110
/// It also logs the shutdown process.
92-
/// This method is idempotent;
93-
/// - Important: This method must be called at leat once to ensure that resources are released properly. If the method is not called, it will lead to a crash.
111+
/// This method is idempotent and will throw if called multiple times to prevent double shutdown.
112+
/// - Important: This method must be called at least once to ensure that resources are released properly. If the method is not called, it will lead to a crash.
94113
public func onGracefulShutdown() async throws {
114+
try await shutdownState.markShutdown()
95115
logger.info("Stopping DynamoDBService...")
96116
try await awsClient.shutdown()
97117
logger.info("DynamoDBService is stopped.")

Sources/BreezeLambdaAPI/BreezeLambdaAPI.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import AWSLambdaRuntime
4040
public actor BreezeLambdaAPI<T: BreezeCodable>: Service {
4141

4242
let logger = Logger(label: "service-group-breeze-lambda-api")
43-
let timeout: TimeAmount
4443
private let serviceGroup: ServiceGroup
4544
private let apiConfig: any APIConfiguring
4645
private let dynamoDBService: BreezeDynamoDBService
@@ -57,10 +56,9 @@ public actor BreezeLambdaAPI<T: BreezeCodable>: Service {
5756
public init(apiConfig: APIConfiguring = BreezeAPIConfiguration()) async throws {
5857
do {
5958
self.apiConfig = apiConfig
60-
self.timeout = .seconds(apiConfig.dbTimeout)
6159
let config = try apiConfig.getConfig()
6260
let httpConfig = BreezeHTTPClientConfig(
63-
timeout: .seconds(60),
61+
timeout: .seconds(apiConfig.dbTimeout),
6462
logger: logger
6563
)
6664
let operation = try apiConfig.operation()

Tests/BreezeLambdaAPITests/BreezeLambdaAPITests.swift

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import Testing
1818
import ServiceLifecycle
1919
import ServiceLifecycleTestKit
2020
import BreezeDynamoDBService
21+
import Foundation
22+
import AsyncHTTPClient
2123

2224
struct APIConfiguration: APIConfiguring {
23-
var dbTimeout: Int64 = 30
25+
var dbTimeout: Int64 = 1
2426

2527
func operation() throws -> BreezeOperation {
2628
.list
@@ -34,21 +36,16 @@ struct APIConfiguration: APIConfiguring {
3436
struct BreezeLambdaAPITests {
3537

3638
let logger = Logger(label: "BreezeHTTPClientServiceTests")
37-
39+
3840
@Test
3941
func test_breezeLambdaAPI_whenValidEnvironment() async throws {
40-
do {
41-
try await testGracefulShutdown { gracefulShutdownTestTrigger in
42+
await testGracefulShutdown { gracefulShutdownTestTrigger in
4243
let (gracefulStream, continuation) = AsyncStream<Void>.makeStream()
43-
try await withThrowingTaskGroup(of: Void.self) { group in
44-
let sut = try await BreezeLambdaAPI<Product>(apiConfig: APIConfiguration())
44+
await withThrowingTaskGroup(of: Void.self) { group in
4545
group.addTask {
46-
try await withGracefulShutdownHandler{
47-
try await sut.run()
48-
} onGracefulShutdown: {
49-
logger.info("On Graceful Shutdown")
50-
continuation.yield()
51-
}
46+
let sut = try await BreezeLambdaAPI<Product>(apiConfig: APIConfiguration())
47+
try await sut.run()
48+
continuation.yield()
5249
}
5350
group.addTask {
5451
try await Task.sleep(nanoseconds: 1_000_000_000)
@@ -61,36 +58,25 @@ struct BreezeLambdaAPITests {
6158
}
6259
}
6360
}
64-
} catch {
65-
logger.error("Error during test: \(error)")
66-
throw error
67-
}
6861
}
6962

7063
@Test
7164
func test_breezeLambdaAPI_whenInvalidEnvironment() async throws {
7265
await #expect(throws: BreezeLambdaAPIError.self) {
73-
let (errorStream, continuation) = AsyncStream<Error>.makeStream()
66+
let (errorStream, continuation) = AsyncStream<Void>.makeStream()
7467
try await testGracefulShutdown { gracefulShutdownTestTrigger in
68+
let sut = try await BreezeLambdaAPI<Product>()
7569
try await withThrowingTaskGroup(of: Void.self) { group in
76-
let sut = try await BreezeLambdaAPI<Product>()
7770
group.addTask {
78-
try await withGracefulShutdownHandler {
79-
do {
80-
try await sut.run()
81-
} catch {
82-
continuation.yield(error)
83-
continuation.finish()
84-
}
85-
} onGracefulShutdown: {
86-
logger.info("Performing onGracefulShutdown")
87-
}
71+
try? await sut.run()
72+
continuation.yield()
8873
}
8974
group.addTask {
9075
try await Task.sleep(nanoseconds: 1_000_000_000)
9176
gracefulShutdownTestTrigger.triggerGracefulShutdown()
9277
}
9378
for await _ in errorStream {
79+
continuation.finish()
9480
logger.info("Error stream received")
9581
group.cancelAll()
9682
}

0 commit comments

Comments
 (0)