Skip to content

Commit 23bf3e1

Browse files
committed
more
1 parent b0ab1b8 commit 23bf3e1

File tree

7 files changed

+59
-38
lines changed

7 files changed

+59
-38
lines changed

Sources/AWSLambdaRuntimeCore/Lambda.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public enum Lambda {
5858
}
5959
private static func run(
6060
configuration: LambdaConfiguration = .init(),
61-
initializationHandler: @escaping (LambdaInitializationContext) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>>
61+
initializationHandler: @escaping _InitializationHandler
6262
) -> Result<Int, Error> {
6363
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
6464
Backtrace.install()

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,16 @@ public protocol LambdaHandler {
7373

7474
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
7575
extension LambdaHandler {
76-
// FIXME: why public?
77-
public static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>> {
76+
internal static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>> {
7877
let promise = context.eventLoop.makePromise(of: Self.self)
7978
promise.completeWithTask {
8079
try await Self(context: context)
8180
}
8281
return promise.futureResult.map{ $0.handle(buffer:context:) }
8382
}
8483

85-
// FIXME: why public?
86-
public func handle(_ event: Event, context: LambdaContext) -> EventLoopFuture<Output> {
84+
@inlinable
85+
internal func handle(_ event: Event, context: LambdaContext) -> EventLoopFuture<Output> {
8786
let promise = context.eventLoop.makePromise(of: Output.self)
8887
// using an unchecked sendable wrapper for the handler
8988
// this is safe since lambda runtime is designed to calls the handler serially
@@ -159,14 +158,18 @@ extension LambdaHandler {
159158

160159
/// unchecked sendable wrapper for the handler
161160
/// this is safe since lambda runtime is designed to calls the handler serially
161+
@usableFromInline
162162
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
163-
fileprivate struct UncheckedSendableHandler<Underlying: LambdaHandler, Event, Output>: @unchecked Sendable where Event == Underlying.Event, Output == Underlying.Output {
163+
internal struct UncheckedSendableHandler<Underlying: LambdaHandler, Event, Output>: @unchecked Sendable where Event == Underlying.Event, Output == Underlying.Output {
164+
@usableFromInline
164165
let underlying: Underlying
165166

167+
@inlinable
166168
init(underlying: Underlying) {
167169
self.underlying = underlying
168170
}
169171

172+
@inlinable
170173
func handle(_ event: Event, context: LambdaContext) async throws -> Output {
171174
try await self.underlying.handle(event, context: context)
172175
}

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(logger: Logger, terminator: LambdaTerminator, initializationHandler: (LambdaInitializationContext) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>>) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>> {
36+
func initialize(logger: Logger, terminator: LambdaTerminator, initializationHandler: _InitializationHandler) -> EventLoopFuture<_LambdaHandler> {
3737
logger.debug("initializing lambda")
3838
// 1. create the handler from the factory
3939
// 2. report initialization error if one occurred
@@ -58,7 +58,7 @@ internal final class LambdaRunner {
5858
}
5959
}
6060

61-
func run(logger: Logger, handler: @escaping (ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>) -> EventLoopFuture<Void> {
61+
func run(logger: Logger, handler: @escaping _LambdaHandler) -> EventLoopFuture<Void> {
6262
logger.debug("lambda invocation sequence starting")
6363
// 1. request invocation from lambda runtime engine
6464
self.isGettingNextInvocation = true

Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import Logging
1616
import NIOConcurrencyHelpers
1717
import NIOCore
1818

19+
typealias _InitializationHandler = (LambdaInitializationContext) -> EventLoopFuture<_LambdaHandler>
20+
typealias _LambdaHandler = (ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>
21+
1922
/// `LambdaRuntime` manages the Lambda process lifecycle.
2023
///
2124
/// Use this API, if you build a higher level web framework which shall be able to run inside the Lambda environment.
2225
public final class LambdaRuntime {
2326
private let eventLoop: EventLoop
24-
private let initializationHandler: (LambdaInitializationContext) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>>
27+
private let initializationHandler: _InitializationHandler
2528
private let shutdownPromise: EventLoopPromise<Int>
2629
private let logger: Logger
2730
private let configuration: LambdaConfiguration
@@ -52,7 +55,7 @@ public final class LambdaRuntime {
5255
self.init(eventLoop: eventLoop, logger: logger, configuration: .init(), initializationHandler: Handler.makeHandler(context:))
5356
}
5457

55-
init(eventLoop: EventLoop, logger: Logger, configuration: LambdaConfiguration, initializationHandler: @escaping (LambdaInitializationContext) -> EventLoopFuture<(ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>>) {
58+
init(eventLoop: EventLoop, logger: Logger, configuration: LambdaConfiguration, initializationHandler: @escaping _InitializationHandler) {
5659
self.eventLoop = eventLoop
5760
self.shutdownPromise = eventLoop.makePromise(of: Int.self)
5861
self.logger = logger
@@ -186,7 +189,7 @@ public final class LambdaRuntime {
186189
private enum State {
187190
case idle
188191
case initializing
189-
case active(LambdaRunner, (ByteBuffer, LambdaContext) -> EventLoopFuture<ByteBuffer?>)
192+
case active(LambdaRunner, _LambdaHandler)
190193
case shuttingdown
191194
case shutdown
192195

Sources/AWSLambdaTesting/Lambda+Testing.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// }
3535

3636
#if compiler(>=5.5) && canImport(_Concurrency)
37-
import AWSLambdaRuntime
37+
@testable import AWSLambdaRuntime
3838
import Dispatch
3939
import Logging
4040
import NIOCore
@@ -63,15 +63,15 @@ extension Lambda {
6363
_ handlerType: Handler.Type,
6464
with event: Handler.Event,
6565
using config: TestConfig = .init()
66-
) throws -> Handler.Output {
66+
) async throws -> Handler.Output {
6767
let logger = Logger(label: "test")
68+
6869
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
6970
defer {
7071
try! eventLoopGroup.syncShutdownGracefully()
7172
}
7273
let eventLoop = eventLoopGroup.next()
7374

74-
let promise = eventLoop.makePromise(of: Handler.self)
7575
let initContext = LambdaInitializationContext.__forTestsOnly(
7676
logger: logger,
7777
eventLoop: eventLoop
@@ -86,14 +86,8 @@ extension Lambda {
8686
eventLoop: eventLoop
8787
)
8888

89-
promise.completeWithTask {
90-
try await Handler(context: initContext)
91-
}
92-
let handler = try promise.futureResult.wait()
93-
94-
return try eventLoop.flatSubmit {
95-
handler.handle(event, context: context)
96-
}.wait()
89+
let handler = try await Handler(context: initContext)
90+
return try await handler.handle(event, context: context)
9791
}
9892
}
9993
#endif

Tests/AWSLambdaTestingTests/Tests.swift

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,24 @@ import XCTest
2020

2121
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
2222
class LambdaTestingTests: XCTestCase {
23-
func testCodableClosure() {
23+
func testBasics() async throws {
24+
struct MyLambda: LambdaHandler {
25+
typealias Event = String
26+
typealias Output = String
27+
28+
init(context: LambdaInitializationContext) {}
29+
30+
func handle(_ event: String, context: LambdaContext) async throws -> String {
31+
return event
32+
}
33+
}
34+
35+
let uuid = UUID().uuidString
36+
let result = try await Lambda.test(MyLambda.self, with: uuid)
37+
XCTAssertEqual(result, uuid)
38+
}
39+
40+
func testCodableClosure() async throws {
2441
struct Request: Codable {
2542
let name: String
2643
}
@@ -41,36 +58,36 @@ class LambdaTestingTests: XCTestCase {
4158
}
4259

4360
let request = Request(name: UUID().uuidString)
44-
var response: Response?
45-
XCTAssertNoThrow(response = try Lambda.test(MyLambda.self, with: request))
46-
XCTAssertEqual(response?.message, "echo" + request.name)
61+
let response = try await Lambda.test(MyLambda.self, with: request)
62+
XCTAssertEqual(response.message, "echo" + request.name)
4763
}
4864

49-
// DIRTY HACK: To verify the handler was actually invoked, we change a global variable.
50-
static var VoidLambdaHandlerInvokeCount: Int = 0
51-
func testCodableVoidClosure() {
65+
func testCodableVoidClosure() async throws {
5266
struct Request: Codable {
5367
let name: String
5468
}
5569

5670
struct MyLambda: LambdaHandler {
71+
// DIRTY HACK: To verify the handler was actually invoked, we change a global variable.
72+
static var VoidLambdaHandlerInvokeCount: Int = 0
73+
5774
typealias Event = Request
5875
typealias Output = Void
5976

6077
init(context: LambdaInitializationContext) {}
6178

6279
func handle(_ event: Request, context: LambdaContext) async throws {
63-
LambdaTestingTests.VoidLambdaHandlerInvokeCount += 1
80+
Self.VoidLambdaHandlerInvokeCount += 1
6481
}
6582
}
6683

67-
Self.VoidLambdaHandlerInvokeCount = 0
6884
let request = Request(name: UUID().uuidString)
69-
XCTAssertNoThrow(try Lambda.test(MyLambda.self, with: request))
70-
XCTAssertEqual(Self.VoidLambdaHandlerInvokeCount, 1)
85+
MyLambda.VoidLambdaHandlerInvokeCount = 0
86+
try await Lambda.test(MyLambda.self, with: request)
87+
XCTAssertEqual(MyLambda.VoidLambdaHandlerInvokeCount, 1)
7188
}
7289

73-
func testInvocationFailure() {
90+
func testInvocationFailure() async throws {
7491
struct MyError: Error {}
7592

7693
struct MyLambda: LambdaHandler {
@@ -84,12 +101,15 @@ class LambdaTestingTests: XCTestCase {
84101
}
85102
}
86103

87-
XCTAssertThrowsError(try Lambda.test(MyLambda.self, with: UUID().uuidString)) { error in
104+
do {
105+
try await Lambda.test(MyLambda.self, with: UUID().uuidString)
106+
XCTFail("expected to throw")
107+
} catch {
88108
XCTAssert(error is MyError)
89109
}
90110
}
91111

92-
func testAsyncLongRunning() {
112+
func testAsyncLongRunning() async throws {
93113
struct MyLambda: LambdaHandler {
94114
typealias Event = String
95115
typealias Output = String
@@ -103,7 +123,8 @@ class LambdaTestingTests: XCTestCase {
103123
}
104124

105125
let uuid = UUID().uuidString
106-
XCTAssertEqual(try Lambda.test(MyLambda.self, with: uuid), uuid)
126+
let result = try await Lambda.test(MyLambda.self, with: uuid)
127+
XCTAssertEqual(result, uuid)
107128
}
108129
}
109130
#endif

docker/docker-compose.al2.57.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
image: swift-aws-lambda:al2-5.7
77
build:
88
args:
9-
base_image: "swiftlang/swift:nightly-main-amazonlinux2"
9+
swift_version: "5.7"
1010

1111
test:
1212
image: swift-aws-lambda:al2-5.7

0 commit comments

Comments
 (0)