Skip to content

Commit a1d1b43

Browse files
committed
Fix reportError
1 parent 8f6c5a8 commit a1d1b43

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Tests/AWSLambdaRuntimeCoreTests/LambdaMockClient.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ struct LambdaMockWriter: LambdaResponseStreamWriter {
3838
}
3939

4040
func reportError(_ error: any Error) async throws {
41-
try await self.underlying.write(ByteBuffer(string: "\(error)"))
42-
try await self.underlying.finish()
41+
await self.underlying.reportError(error)
4342
}
4443
}
4544

@@ -105,6 +104,12 @@ final actor LambdaMockClient: LambdaRuntimeClientProtocol {
105104
case fail(LambdaError)
106105
}
107106

107+
enum CancelProcessingAction {
108+
case none
109+
110+
case cancelContinuation(CheckedContinuation<ByteBuffer, any Error>)
111+
}
112+
108113
mutating func next(_ eventArrivedHandler: CheckedContinuation<Invocation, any Error>) -> NextAction {
109114
switch self.state {
110115
case .initialState:
@@ -185,6 +190,15 @@ final actor LambdaMockClient: LambdaRuntimeClientProtocol {
185190
return .cancelContinuation(eventArrivedHandler)
186191
}
187192
}
193+
194+
mutating func cancelProcessing() -> CancelProcessingAction {
195+
switch self.state {
196+
case .initialState, .waitingForNextEvent:
197+
return .none
198+
case .handlerIsProcessing(_, let eventProcessedHandler):
199+
return .cancelContinuation(eventProcessedHandler)
200+
}
201+
}
188202
}
189203

190204
private var stateMachine: StateMachine = .init()
@@ -268,4 +282,13 @@ final actor LambdaMockClient: LambdaRuntimeClientProtocol {
268282
func finish() async throws {
269283
try self.stateMachine.finish()
270284
}
285+
286+
func reportError(_ error: any Error) {
287+
switch self.stateMachine.cancelProcessing() {
288+
case .none:
289+
break
290+
case .cancelContinuation(let continuation):
291+
continuation.resume(throwing: error)
292+
}
293+
}
271294
}

Tests/AWSLambdaRuntimeCoreTests/LambdaRunLoopTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct LambdaRunLoopTests {
6767
@Test func testRunLoopError() async throws {
6868
let inputEvent = ByteBuffer(string: "Test Invocation Event")
6969

70-
try await withThrowingTaskGroup(of: Void.self) { group in
70+
await withThrowingTaskGroup(of: Void.self) { group in
7171
group.addTask {
7272
try await Lambda.runLoop(
7373
runtimeClient: self.mockClient,
@@ -76,8 +76,12 @@ struct LambdaRunLoopTests {
7676
)
7777
}
7878

79-
let response = try await self.mockClient.invoke(event: inputEvent)
80-
#expect(String(buffer: response) == "\(LambdaError.handlerError)")
79+
await #expect(
80+
throws: LambdaError.handlerError,
81+
performing: {
82+
try await self.mockClient.invoke(event: inputEvent)
83+
}
84+
)
8185

8286
group.cancelAll()
8387
}

0 commit comments

Comments
 (0)