Skip to content

Commit 438b87d

Browse files
committed
Refactor
1 parent 4cb8a2c commit 438b87d

File tree

2 files changed

+14
-41
lines changed

2 files changed

+14
-41
lines changed

Sources/AWSLambdaTesting/LambdaMockClient.swift

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
import Logging
1818
import NIOCore
1919

20-
package struct LambdaMockWriter: LambdaResponseStreamWriter {
20+
struct LambdaMockWriter: LambdaResponseStreamWriter {
2121
var underlying: LambdaMockClient
2222

2323
package init(underlying: LambdaMockClient) {
@@ -41,43 +41,14 @@ package struct LambdaMockWriter: LambdaResponseStreamWriter {
4141
}
4242
}
4343

44-
package struct LambdaError: Error, Equatable {
45-
private enum Code: Equatable {
46-
case cannotCallNextEndpointWhenAlreadyWaitingForEvent
47-
case cannotCallNextEndpointWhenAlreadyProcessingAnEvent
48-
case cannotReportResultWhenNoEventHasBeenProcessed
49-
}
50-
51-
private let code: Code
52-
53-
private init(code: Code) {
54-
self.code = code
55-
}
56-
57-
package func shortDescription() -> String {
58-
switch self.code {
59-
case .cannotCallNextEndpointWhenAlreadyWaitingForEvent:
60-
"Cannot call the next endpoint when already waiting for an event"
61-
case .cannotCallNextEndpointWhenAlreadyProcessingAnEvent:
62-
"Cannot call the next endpoint when an event is already being processed"
63-
case .cannotReportResultWhenNoEventHasBeenProcessed:
64-
"Cannot report a result when no event has been processed"
65-
}
66-
}
67-
68-
package static let cannotCallNextEndpointWhenAlreadyWaitingForEvent = LambdaError(
69-
code: .cannotCallNextEndpointWhenAlreadyWaitingForEvent
70-
)
71-
package static let cannotCallNextEndpointWhenAlreadyProcessingAnEvent = LambdaError(
72-
code: .cannotCallNextEndpointWhenAlreadyProcessingAnEvent
73-
)
74-
package static let cannotReportResultWhenNoEventHasBeenProcessed = LambdaError(
75-
code: .cannotReportResultWhenNoEventHasBeenProcessed
76-
)
44+
enum LambdaError: Error, Equatable {
45+
case cannotCallNextEndpointWhenAlreadyWaitingForEvent
46+
case cannotCallNextEndpointWhenAlreadyProcessingAnEvent
47+
case cannotReportResultWhenNoEventHasBeenProcessed
7748
}
7849

79-
package final actor LambdaMockClient: LambdaRuntimeClientProtocol {
80-
package typealias Writer = LambdaMockWriter
50+
final actor LambdaMockClient: LambdaRuntimeClientProtocol {
51+
typealias Writer = LambdaMockWriter
8152

8253
private struct StateMachine {
8354
private enum State {
@@ -203,7 +174,7 @@ package final actor LambdaMockClient: LambdaRuntimeClientProtocol {
203174
let eventProcessedHandler: CheckedContinuation<ByteBuffer, any Error>
204175
}
205176

206-
package func invoke(event: ByteBuffer) async throws -> ByteBuffer {
177+
func invoke(event: ByteBuffer) async throws -> ByteBuffer {
207178
try await withCheckedThrowingContinuation { eventProcessedHandler in
208179
do {
209180
let metadata = try InvocationMetadata(
@@ -236,7 +207,7 @@ package final actor LambdaMockClient: LambdaRuntimeClientProtocol {
236207
}
237208
}
238209

239-
package func nextInvocation() async throws -> (Invocation, Writer) {
210+
func nextInvocation() async throws -> (Invocation, Writer) {
240211
let invocation = try await withCheckedThrowingContinuation { eventArrivedHandler in
241212
switch self.stateMachine.next(eventArrivedHandler) {
242213
case .readyToProcess(let event):
@@ -251,7 +222,7 @@ package final actor LambdaMockClient: LambdaRuntimeClientProtocol {
251222
return (invocation, Writer(underlying: self))
252223
}
253224

254-
package func write(_ buffer: ByteBuffer) async throws {
225+
func write(_ buffer: ByteBuffer) async throws {
255226
switch self.stateMachine.writeResult(buffer: buffer) {
256227
case .readyForMore:
257228
break
@@ -260,7 +231,7 @@ package final actor LambdaMockClient: LambdaRuntimeClientProtocol {
260231
}
261232
}
262233

263-
package func finish() async throws {
234+
func finish() async throws {
264235
try self.stateMachine.finish()
265236
}
266237
}

Sources/AWSLambdaTesting/LambdaRunLoopTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct LambdaRunLoopTests {
3434
let mockEchoHandler = MockEchoHandler()
3535

3636
@Test func testRunLoop() async throws {
37-
_ = Task { () in
37+
let runLoopTask = Task { () in
3838
try await Lambda.runLoop(
3939
runtimeClient: self.mockClient,
4040
handler: self.mockEchoHandler,
@@ -45,6 +45,8 @@ struct LambdaRunLoopTests {
4545
let inputEvent = ByteBuffer(string: "Test Invocation Event")
4646
let response = try await self.mockClient.invoke(event: inputEvent)
4747

48+
runLoopTask.cancel()
49+
4850
#expect(response == inputEvent)
4951
}
5052
}

0 commit comments

Comments
 (0)