Skip to content

Commit f642509

Browse files
committed
rename
1 parent c33538b commit f642509

File tree

6 files changed

+135
-14
lines changed

6 files changed

+135
-14
lines changed

Sources/AWSLambdaRuntimeCore/Lambda.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public enum Lambda {
100100
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
101101
internal static func run(
102102
configuration: LambdaConfiguration = .init(),
103-
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some CoreByteBufferLambdaHandler>
103+
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some NonInitializingByteBufferLambdaHandler>
104104
) -> Result<Int, Error> {
105105
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
106106
#if swift(<5.9)

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ extension EventLoopLambdaHandler {
398398
/// - note: This is a low level protocol designed to power the higher level ``EventLoopLambdaHandler`` and
399399
/// ``LambdaHandler`` based APIs.
400400
/// Most users are not expected to use this protocol.
401-
public protocol ByteBufferLambdaHandler: CoreByteBufferLambdaHandler {
401+
public protocol ByteBufferLambdaHandler: NonInitializingByteBufferLambdaHandler {
402402
/// Create a Lambda handler for the runtime.
403403
///
404404
/// Use this to initialize all your resources that you want to cache between invocations. This could be database
@@ -433,9 +433,16 @@ extension ByteBufferLambdaHandler {
433433
}
434434
}
435435

436-
// MARK: - FIXME
436+
// MARK: - NonInitializingByteBufferLambdaHandler
437437

438-
public protocol CoreByteBufferLambdaHandler {
438+
/// An `EventLoopFuture` based processing protocol for a Lambda that takes a `ByteBuffer` and returns
439+
/// an optional `ByteBuffer` asynchronously.
440+
///
441+
/// - note: This is a low level protocol designed to enable use cases where a frameworks initializes the
442+
/// runtime with a handler outside the normal initialization of
443+
/// ``ByteBufferLambdaHandler``, ``EventLoopLambdaHandler`` and ``LambdaHandler`` based APIs.
444+
/// Most users are not expected to use this protocol.
445+
public protocol NonInitializingByteBufferLambdaHandler {
439446
/// The Lambda handling method.
440447
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
441448
///

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal final class LambdaRunner {
3333
/// Run the user provided initializer. This *must* only be called once.
3434
///
3535
/// - Returns: An `EventLoopFuture<LambdaHandler>` fulfilled with the outcome of the initialization.
36-
func initialize<Handler: CoreByteBufferLambdaHandler>(
36+
func initialize<Handler: NonInitializingByteBufferLambdaHandler>(
3737
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
3838
logger: Logger,
3939
terminator: LambdaTerminator
@@ -63,7 +63,7 @@ internal final class LambdaRunner {
6363
}
6464
}
6565

66-
func run(handler: some CoreByteBufferLambdaHandler, logger: Logger) -> EventLoopFuture<Void> {
66+
func run(handler: some NonInitializingByteBufferLambdaHandler, logger: Logger) -> EventLoopFuture<Void> {
6767
logger.debug("lambda invocation sequence starting")
6868
// 1. request invocation from lambda runtime engine
6969
self.isGettingNextInvocation = true

Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import NIOCore
1919
/// `LambdaRuntime` manages the Lambda process lifecycle.
2020
///
2121
/// Use this API, if you build a higher level web framework which shall be able to run inside the Lambda environment.
22-
public final class LambdaRuntime<Handler: CoreByteBufferLambdaHandler> {
22+
public final class LambdaRuntime<Handler: NonInitializingByteBufferLambdaHandler> {
2323
private let eventLoop: EventLoop
2424
private let shutdownPromise: EventLoopPromise<Int>
2525
private let logger: Logger
@@ -200,7 +200,7 @@ public final class LambdaRuntime<Handler: CoreByteBufferLambdaHandler> {
200200
private enum State {
201201
case idle
202202
case initializing
203-
case active(LambdaRunner, any CoreByteBufferLambdaHandler)
203+
case active(LambdaRunner, any NonInitializingByteBufferLambdaHandler)
204204
case shuttingdown
205205
case shutdown
206206

@@ -252,7 +252,7 @@ public enum LambdaRuntimeFactory {
252252
_ handlerType: Handler.Type,
253253
eventLoop: any EventLoop,
254254
logger: Logger
255-
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
255+
) -> LambdaRuntime<some NonInitializingByteBufferLambdaHandler> {
256256
LambdaRuntime<CodableLambdaHandler<Handler>>(
257257
handlerProvider: CodableLambdaHandler<Handler>.makeHandler(context:),
258258
eventLoop: eventLoop,
@@ -271,7 +271,7 @@ public enum LambdaRuntimeFactory {
271271
_ handlerType: Handler.Type,
272272
eventLoop: any EventLoop,
273273
logger: Logger
274-
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
274+
) -> LambdaRuntime<some NonInitializingByteBufferLambdaHandler> {
275275
LambdaRuntime<CodableEventLoopLambdaHandler<Handler>>(
276276
handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler(context:),
277277
eventLoop: eventLoop,
@@ -282,15 +282,15 @@ public enum LambdaRuntimeFactory {
282282
/// Create a new `LambdaRuntime`.
283283
///
284284
/// - parameters:
285-
/// - handlerType: The ``EventLoopLambdaHandler`` type the `LambdaRuntime` shall create and manage.
285+
/// - handlerType: The ``ByteBufferLambdaHandler`` type the `LambdaRuntime` shall create and manage.
286286
/// - eventLoop: An `EventLoop` to run the Lambda on.
287287
/// - logger: A `Logger` to log the Lambda events.
288288
@inlinable
289289
public static func makeRuntime<Handler: ByteBufferLambdaHandler>(
290290
_ handlerType: Handler.Type,
291291
eventLoop: any EventLoop,
292292
logger: Logger
293-
) -> LambdaRuntime<some CoreByteBufferLambdaHandler> {
293+
) -> LambdaRuntime<some NonInitializingByteBufferLambdaHandler> {
294294
LambdaRuntime<Handler>(
295295
handlerProvider: Handler.makeHandler(context:),
296296
eventLoop: eventLoop,
@@ -301,11 +301,11 @@ public enum LambdaRuntimeFactory {
301301
/// Create a new `LambdaRuntime`.
302302
///
303303
/// - parameters:
304-
/// - handlerProvider: A provider of the ``EventLoopLambdaHandler`` the `LambdaRuntime` will manage.
304+
/// - handlerProvider: A provider of the ``CoreByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
305305
/// - eventLoop: An `EventLoop` to run the Lambda on.
306306
/// - logger: A `Logger` to log the Lambda events.
307307
@inlinable
308-
public static func makeRuntime<Handler: CoreByteBufferLambdaHandler>(
308+
public static func makeRuntime<Handler: NonInitializingByteBufferLambdaHandler>(
309309
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<Handler>,
310310
eventLoop: any EventLoop,
311311
logger: Logger
@@ -316,6 +316,31 @@ public enum LambdaRuntimeFactory {
316316
logger: logger
317317
)
318318
}
319+
320+
/// Create a new `LambdaRuntime`.
321+
///
322+
/// - parameters:
323+
/// - handlerProvider: A provider of the ``CoreByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
324+
/// - eventLoop: An `EventLoop` to run the Lambda on.
325+
/// - logger: A `Logger` to log the Lambda events.
326+
@inlinable
327+
public static func makeRuntime<Handler: NonInitializingByteBufferLambdaHandler>(
328+
handlerProvider: @escaping (LambdaInitializationContext) async throws -> Handler,
329+
eventLoop: any EventLoop,
330+
logger: Logger
331+
) -> LambdaRuntime<Handler> {
332+
return LambdaRuntime(
333+
handlerProvider: { context in
334+
let promise = eventLoop.makePromise(of: Handler.self)
335+
promise.completeWithTask {
336+
try await handlerProvider(context)
337+
}
338+
return promise.futureResult
339+
},
340+
eventLoop: eventLoop,
341+
logger: logger
342+
)
343+
}
319344
}
320345

321346
/// This is safe since lambda runtime synchronizes by dispatching all methods to a single `EventLoop`

Tests/AWSLambdaRuntimeCoreTests/LambdaRunnerTest.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,66 @@ class LambdaRunnerTest: XCTestCase {
131131
XCTAssertNotNil(error as? CustomError, "expecting error to match")
132132
}
133133
}
134+
135+
func testCustomAsyncProviderSuccess() {
136+
struct Behavior: LambdaServerBehavior {
137+
let requestId = UUID().uuidString
138+
let event = "hello"
139+
func getInvocation() -> GetInvocationResult {
140+
.success((self.requestId, self.event))
141+
}
142+
143+
func processResponse(requestId: String, response: String?) -> Result<Void, ProcessResponseError> {
144+
XCTAssertEqual(self.requestId, requestId, "expecting requestId to match")
145+
XCTAssertEqual(self.event, response, "expecting response to match")
146+
return .success(())
147+
}
148+
149+
func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError> {
150+
XCTFail("should not report error")
151+
return .failure(.internalServerError)
152+
}
153+
154+
func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError> {
155+
XCTFail("should not report init error")
156+
return .failure(.internalServerError)
157+
}
158+
}
159+
XCTAssertNoThrow(try runLambda(behavior: Behavior(), handlerProvider: { context async throws -> EchoHandler in
160+
EchoHandler()
161+
}))
162+
}
163+
164+
func testCustomAsyncProviderFailure() {
165+
struct Behavior: LambdaServerBehavior {
166+
let requestId = UUID().uuidString
167+
let event = "hello"
168+
func getInvocation() -> GetInvocationResult {
169+
.success((self.requestId, self.event))
170+
}
171+
172+
func processResponse(requestId: String, response: String?) -> Result<Void, ProcessResponseError> {
173+
XCTFail("should not report processing")
174+
return .failure(.internalServerError)
175+
}
176+
177+
func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError> {
178+
XCTFail("should not report error")
179+
return .failure(.internalServerError)
180+
}
181+
182+
func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError> {
183+
XCTAssertEqual(String(describing: CustomError()), error.errorMessage, "expecting error to match")
184+
return .success(())
185+
}
186+
}
187+
188+
struct CustomError: Error {}
189+
190+
XCTAssertThrowsError(try runLambda(behavior: Behavior(), handlerProvider: { context async throws -> EchoHandler in
191+
throw CustomError()
192+
})) { error in
193+
XCTAssertNotNil(error as? CustomError, "expecting error to match")
194+
}
195+
}
134196
}

Tests/AWSLambdaRuntimeCoreTests/Utils.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ func runLambda<Handler: EventLoopLambdaHandler>(
4141
})
4242
}
4343

44+
func runLambda<Handler: EventLoopLambdaHandler>(
45+
behavior: LambdaServerBehavior,
46+
handlerProvider: @escaping (LambdaInitializationContext) async throws -> Handler
47+
) throws {
48+
try runLambda(behavior: behavior, handlerProvider: { context in
49+
let handler = try await handlerProvider(context)
50+
return CodableEventLoopLambdaHandler(handler: handler, allocator: context.allocator)
51+
})
52+
}
53+
54+
func runLambda<Handler: ByteBufferLambdaHandler>(
55+
behavior: LambdaServerBehavior,
56+
handlerProvider: @escaping (LambdaInitializationContext) async throws -> Handler
57+
) throws {
58+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
59+
try runLambda(
60+
behavior: behavior,
61+
handlerProvider: { context in
62+
let promise = eventLoopGroup.next().makePromise(of: Handler.self)
63+
promise.completeWithTask {
64+
try await handlerProvider(context)
65+
}
66+
return promise.futureResult
67+
}
68+
)
69+
}
70+
4471
func runLambda(
4572
behavior: LambdaServerBehavior,
4673
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some ByteBufferLambdaHandler>

0 commit comments

Comments
 (0)