From 6e91d71949ea36e232bb5e45d7fc0bd827ccde9c Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Wed, 29 Sep 2021 09:17:43 +0200 Subject: [PATCH] Rename Lambda.Context to LambdaContext --- Examples/Benchmark/main.swift | 2 +- .../Deployment/Sources/Benchmark/main.swift | 2 +- .../HelloWorld/HelloWorldHandler.swift | 2 +- Examples/Echo/Lambda.swift | 2 +- Examples/ErrorHandling/Lambda.swift | 2 +- Examples/Foundation/Lambda.swift | 2 +- Examples/JSON/Lambda.swift | 2 +- Examples/LocalDebugging/MyLambda/Lambda.swift | 2 +- Examples/Testing/Sources/Lambda.swift | 2 +- .../AWSLambdaRuntime/Context+Foundation.swift | 2 +- .../Lambda+LocalServer.swift | 2 +- .../AWSLambdaRuntimeCore/LambdaContext.swift | 256 +++++++++--------- .../AWSLambdaRuntimeCore/LambdaHandler.swift | 10 +- .../AWSLambdaRuntimeCore/LambdaRunner.swift | 12 +- Sources/AWSLambdaTesting/Lambda+Testing.swift | 4 +- .../LambdaHandlerTest.swift | 16 +- .../LambdaHandlers.swift | 4 +- .../LambdaRuntimeTest.swift | 6 +- .../LambdaTest.swift | 60 ++-- .../Lambda+CodeableTest.swift | 38 +-- Tests/AWSLambdaTestingTests/Tests.swift | 8 +- 21 files changed, 223 insertions(+), 213 deletions(-) diff --git a/Examples/Benchmark/main.swift b/Examples/Benchmark/main.swift index 421d0cdc..4a76d1c6 100644 --- a/Examples/Benchmark/main.swift +++ b/Examples/Benchmark/main.swift @@ -24,7 +24,7 @@ struct MyLambda: EventLoopLambdaHandler { typealias Event = String typealias Output = String - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeSucceededFuture("hello, world!") } } diff --git a/Examples/Deployment/Sources/Benchmark/main.swift b/Examples/Deployment/Sources/Benchmark/main.swift index ce6d3e0d..454e265a 100644 --- a/Examples/Deployment/Sources/Benchmark/main.swift +++ b/Examples/Deployment/Sources/Benchmark/main.swift @@ -24,7 +24,7 @@ struct BenchmarkHandler: EventLoopLambdaHandler { typealias Event = String typealias Output = String - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeSucceededFuture("hello, world!") } } diff --git a/Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift b/Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift index ac795e65..1e3d189a 100644 --- a/Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift +++ b/Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift @@ -24,7 +24,7 @@ struct HelloWorldHandler: LambdaHandler { // setup your resources that you want to reuse here. } - func handle(_ event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: LambdaContext) async throws -> String { "hello, world" } } diff --git a/Examples/Echo/Lambda.swift b/Examples/Echo/Lambda.swift index 684ed45d..2b2b5763 100644 --- a/Examples/Echo/Lambda.swift +++ b/Examples/Echo/Lambda.swift @@ -25,7 +25,7 @@ struct MyLambda: LambdaHandler { // setup your resources that you want to reuse for every invocation here. } - func handle(_ input: String, context: Lambda.Context) async throws -> String { + func handle(_ input: String, context: LambdaContext) async throws -> String { // as an example, respond with the input's reversed String(input.reversed()) } diff --git a/Examples/ErrorHandling/Lambda.swift b/Examples/ErrorHandling/Lambda.swift index e9b30e9f..8bfe2c6e 100644 --- a/Examples/ErrorHandling/Lambda.swift +++ b/Examples/ErrorHandling/Lambda.swift @@ -23,7 +23,7 @@ struct MyLambda: LambdaHandler { init(context: Lambda.InitializationContext) async throws {} - func handle(_ request: Request, context: Lambda.Context) async throws -> Response { + func handle(_ request: Request, context: LambdaContext) async throws -> Response { // switch over the error type "requested" by the request, and trigger such error accordingly switch request.error { // no error here! diff --git a/Examples/Foundation/Lambda.swift b/Examples/Foundation/Lambda.swift index 0921ac88..6454db5d 100644 --- a/Examples/Foundation/Lambda.swift +++ b/Examples/Foundation/Lambda.swift @@ -35,7 +35,7 @@ struct MyLambda: LambdaHandler { self.calculator = ExchangeRatesCalculator() } - func handle(_ event: Request, context: Lambda.Context) async throws -> [Exchange] { + func handle(_ event: Request, context: LambdaContext) async throws -> [Exchange] { try await withCheckedThrowingContinuation { continuation in self.calculator.run(logger: context.logger) { result in switch result { diff --git a/Examples/JSON/Lambda.swift b/Examples/JSON/Lambda.swift index d009861d..91b8af7b 100644 --- a/Examples/JSON/Lambda.swift +++ b/Examples/JSON/Lambda.swift @@ -34,7 +34,7 @@ struct MyLambda: LambdaHandler { // setup your resources that you want to reuse for every invocation here. } - func handle(_ event: Request, context: Lambda.Context) async throws -> Response { + func handle(_ event: Request, context: LambdaContext) async throws -> Response { // as an example, respond with the input event's reversed body Response(body: String(event.body.reversed())) } diff --git a/Examples/LocalDebugging/MyLambda/Lambda.swift b/Examples/LocalDebugging/MyLambda/Lambda.swift index b6a5d865..8070ec93 100644 --- a/Examples/LocalDebugging/MyLambda/Lambda.swift +++ b/Examples/LocalDebugging/MyLambda/Lambda.swift @@ -27,7 +27,7 @@ struct MyLambda: LambdaHandler { // setup your resources that you want to reuse for every invocation here. } - func handle(_ request: Request, context: Lambda.Context) async throws -> Response { + func handle(_ request: Request, context: LambdaContext) async throws -> Response { // TODO: something useful Response(message: "Hello, \(request.name)!") } diff --git a/Examples/Testing/Sources/Lambda.swift b/Examples/Testing/Sources/Lambda.swift index 20d62aac..e0531bb1 100644 --- a/Examples/Testing/Sources/Lambda.swift +++ b/Examples/Testing/Sources/Lambda.swift @@ -25,7 +25,7 @@ struct MyLambda: LambdaHandler { // setup your resources that you want to reuse for every invocation here. } - func handle(_ event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: LambdaContext) async throws -> String { // as an example, respond with the event's reversed body String(event.reversed()) } diff --git a/Sources/AWSLambdaRuntime/Context+Foundation.swift b/Sources/AWSLambdaRuntime/Context+Foundation.swift index 0aa1c019..780e1509 100644 --- a/Sources/AWSLambdaRuntime/Context+Foundation.swift +++ b/Sources/AWSLambdaRuntime/Context+Foundation.swift @@ -15,7 +15,7 @@ import AWSLambdaRuntimeCore import struct Foundation.Date -extension Lambda.Context { +extension LambdaContext { var deadlineDate: Date { let secondsSinceEpoch = Double(Int64(bitPattern: self.deadline.rawValue)) / -1_000_000_000 return Date(timeIntervalSince1970: secondsSinceEpoch) diff --git a/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift b/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift index eff33aca..1e09d867 100644 --- a/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift +++ b/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift @@ -24,7 +24,7 @@ import NIOPosix // For example: // // try Lambda.withLocalServer { -// Lambda.run { (context: Lambda.Context, event: String, callback: @escaping (Result) -> Void) in +// Lambda.run { (context: LambdaContext, event: String, callback: @escaping (Result) -> Void) in // callback(.success("Hello, \(event)!")) // } // } diff --git a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift index 8b20a245..ca4af5c6 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift @@ -58,151 +58,149 @@ extension Lambda { // MARK: - Context -extension Lambda { - /// Lambda runtime context. - /// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument. - public struct Context: CustomDebugStringConvertible { - final class _Storage { - var requestID: String - var traceID: String - var invokedFunctionARN: String - var deadline: DispatchWallTime - var cognitoIdentity: String? - var clientContext: String? - var logger: Logger - var eventLoop: EventLoop - var allocator: ByteBufferAllocator - - init( - requestID: String, - traceID: String, - invokedFunctionARN: String, - deadline: DispatchWallTime, - cognitoIdentity: String?, - clientContext: String?, - logger: Logger, - eventLoop: EventLoop, - allocator: ByteBufferAllocator - ) { - self.requestID = requestID - self.traceID = traceID - self.invokedFunctionARN = invokedFunctionARN - self.deadline = deadline - self.cognitoIdentity = cognitoIdentity - self.clientContext = clientContext - self.logger = logger - self.eventLoop = eventLoop - self.allocator = allocator - } +/// Lambda runtime context. +/// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument. +public struct LambdaContext: CustomDebugStringConvertible { + final class _Storage { + var requestID: String + var traceID: String + var invokedFunctionARN: String + var deadline: DispatchWallTime + var cognitoIdentity: String? + var clientContext: String? + var logger: Logger + var eventLoop: EventLoop + var allocator: ByteBufferAllocator + + init( + requestID: String, + traceID: String, + invokedFunctionARN: String, + deadline: DispatchWallTime, + cognitoIdentity: String?, + clientContext: String?, + logger: Logger, + eventLoop: EventLoop, + allocator: ByteBufferAllocator + ) { + self.requestID = requestID + self.traceID = traceID + self.invokedFunctionARN = invokedFunctionARN + self.deadline = deadline + self.cognitoIdentity = cognitoIdentity + self.clientContext = clientContext + self.logger = logger + self.eventLoop = eventLoop + self.allocator = allocator } + } - private var storage: _Storage + private var storage: _Storage - /// The request ID, which identifies the request that triggered the function invocation. - public var requestID: String { - self.storage.requestID - } + /// The request ID, which identifies the request that triggered the function invocation. + public var requestID: String { + self.storage.requestID + } - /// The AWS X-Ray tracing header. - public var traceID: String { - self.storage.traceID - } + /// The AWS X-Ray tracing header. + public var traceID: String { + self.storage.traceID + } - /// The ARN of the Lambda function, version, or alias that's specified in the invocation. - public var invokedFunctionARN: String { - self.storage.invokedFunctionARN - } + /// The ARN of the Lambda function, version, or alias that's specified in the invocation. + public var invokedFunctionARN: String { + self.storage.invokedFunctionARN + } - /// The timestamp that the function times out - public var deadline: DispatchWallTime { - self.storage.deadline - } + /// The timestamp that the function times out + public var deadline: DispatchWallTime { + self.storage.deadline + } - /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. - public var cognitoIdentity: String? { - self.storage.cognitoIdentity - } + /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. + public var cognitoIdentity: String? { + self.storage.cognitoIdentity + } - /// For invocations from the AWS Mobile SDK, data about the client application and device. - public var clientContext: String? { - self.storage.clientContext - } + /// For invocations from the AWS Mobile SDK, data about the client application and device. + public var clientContext: String? { + self.storage.clientContext + } - /// `Logger` to log with - /// - /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. - public var logger: Logger { - self.storage.logger - } + /// `Logger` to log with + /// + /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. + public var logger: Logger { + self.storage.logger + } - /// The `EventLoop` the Lambda is executed on. Use this to schedule work with. - /// This is useful when implementing the `EventLoopLambdaHandler` protocol. - /// - /// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care. - /// Most importantly the `EventLoop` must never be blocked. - public var eventLoop: EventLoop { - self.storage.eventLoop - } + /// The `EventLoop` the Lambda is executed on. Use this to schedule work with. + /// This is useful when implementing the `EventLoopLambdaHandler` protocol. + /// + /// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care. + /// Most importantly the `EventLoop` must never be blocked. + public var eventLoop: EventLoop { + self.storage.eventLoop + } - /// `ByteBufferAllocator` to allocate `ByteBuffer` - /// This is useful when implementing `EventLoopLambdaHandler` - public var allocator: ByteBufferAllocator { - self.storage.allocator - } + /// `ByteBufferAllocator` to allocate `ByteBuffer` + /// This is useful when implementing `EventLoopLambdaHandler` + public var allocator: ByteBufferAllocator { + self.storage.allocator + } - init(requestID: String, - traceID: String, - invokedFunctionARN: String, - deadline: DispatchWallTime, - cognitoIdentity: String? = nil, - clientContext: String? = nil, - logger: Logger, - eventLoop: EventLoop, - allocator: ByteBufferAllocator) { - self.storage = _Storage( - requestID: requestID, - traceID: traceID, - invokedFunctionARN: invokedFunctionARN, - deadline: deadline, - cognitoIdentity: cognitoIdentity, - clientContext: clientContext, - logger: logger, - eventLoop: eventLoop, - allocator: allocator - ) - } + init(requestID: String, + traceID: String, + invokedFunctionARN: String, + deadline: DispatchWallTime, + cognitoIdentity: String? = nil, + clientContext: String? = nil, + logger: Logger, + eventLoop: EventLoop, + allocator: ByteBufferAllocator) { + self.storage = _Storage( + requestID: requestID, + traceID: traceID, + invokedFunctionARN: invokedFunctionARN, + deadline: deadline, + cognitoIdentity: cognitoIdentity, + clientContext: clientContext, + logger: logger, + eventLoop: eventLoop, + allocator: allocator + ) + } - public func getRemainingTime() -> TimeAmount { - let deadline = self.deadline.millisSinceEpoch - let now = DispatchWallTime.now().millisSinceEpoch + public func getRemainingTime() -> TimeAmount { + let deadline = self.deadline.millisSinceEpoch + let now = DispatchWallTime.now().millisSinceEpoch - let remaining = deadline - now - return .milliseconds(remaining) - } + let remaining = deadline - now + return .milliseconds(remaining) + } - public var debugDescription: String { - "\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))" - } + public var debugDescription: String { + "\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))" + } - /// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning. - public static func __forTestsOnly( - requestID: String, - traceID: String, - invokedFunctionARN: String, - timeout: DispatchTimeInterval, - logger: Logger, - eventLoop: EventLoop - ) -> Context { - Context( - requestID: requestID, - traceID: traceID, - invokedFunctionARN: invokedFunctionARN, - deadline: .now() + timeout, - logger: logger, - eventLoop: eventLoop, - allocator: ByteBufferAllocator() - ) - } + /// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning. + public static func __forTestsOnly( + requestID: String, + traceID: String, + invokedFunctionARN: String, + timeout: DispatchTimeInterval, + logger: Logger, + eventLoop: EventLoop + ) -> LambdaContext { + LambdaContext( + requestID: requestID, + traceID: traceID, + invokedFunctionARN: invokedFunctionARN, + deadline: .now() + timeout, + logger: logger, + eventLoop: eventLoop, + allocator: ByteBufferAllocator() + ) } } diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 16c1505a..d9fd68f8 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -37,12 +37,12 @@ public protocol LambdaHandler: EventLoopLambdaHandler { /// - context: Runtime `Context`. /// /// - Returns: A Lambda result ot type `Output`. - func handle(_ event: Event, context: Lambda.Context) async throws -> Output + func handle(_ event: Event, context: LambdaContext) async throws -> Output } @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) extension LambdaHandler { - public func handle(_ event: Event, context: Lambda.Context) -> EventLoopFuture { + public func handle(_ event: Event, context: LambdaContext) -> EventLoopFuture { let promise = context.eventLoop.makePromise(of: Output.self) promise.completeWithTask { try await self.handle(event, context: context) @@ -81,7 +81,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { /// /// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine. /// The `EventLoopFuture` should be completed with either a response of type `Output` or an `Error` - func handle(_ event: Event, context: Lambda.Context) -> EventLoopFuture + func handle(_ event: Event, context: LambdaContext) -> EventLoopFuture /// Encode a response of type `Output` to `ByteBuffer` /// Concrete Lambda handlers implement this method to provide coding functionality. @@ -105,7 +105,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { extension EventLoopLambdaHandler { /// Driver for `ByteBuffer` -> `Event` decoding and `Output` -> `ByteBuffer` encoding @inlinable - public func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture { + public func handle(_ event: ByteBuffer, context: LambdaContext) -> EventLoopFuture { let input: Event do { input = try self.decode(buffer: event) @@ -147,7 +147,7 @@ public protocol ByteBufferLambdaHandler { /// /// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine. /// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error` - func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture + func handle(_ event: ByteBuffer, context: LambdaContext) -> EventLoopFuture /// Clean up the Lambda resources asynchronously. /// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections. diff --git a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift index 542ede87..8497bf44 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift @@ -65,10 +65,12 @@ extension Lambda { }.flatMap { invocation, bytes in // 2. send invocation to handler self.isGettingNextInvocation = false - let context = Context(logger: logger, - eventLoop: self.eventLoop, - allocator: self.allocator, - invocation: invocation) + let context = LambdaContext( + logger: logger, + eventLoop: self.eventLoop, + allocator: self.allocator, + invocation: invocation + ) logger.debug("sending invocation to lambda handler \(handler)") return handler.handle(bytes, context: context) // Hopping back to "our" EventLoop is important in case the handler returns a future that @@ -100,7 +102,7 @@ extension Lambda { } } -extension Lambda.Context { +extension LambdaContext { init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Lambda.Invocation) { self.init(requestID: invocation.requestID, traceID: invocation.traceID, diff --git a/Sources/AWSLambdaTesting/Lambda+Testing.swift b/Sources/AWSLambdaTesting/Lambda+Testing.swift index d703cb31..7180a204 100644 --- a/Sources/AWSLambdaTesting/Lambda+Testing.swift +++ b/Sources/AWSLambdaTesting/Lambda+Testing.swift @@ -22,7 +22,7 @@ // // init(context: Lambda.InitializationContext) {} // -// func handle(_ event: String, context: Lambda.Context) async throws -> String { +// func handle(_ event: String, context: LambdaContext) async throws -> String { // "echo" + event // } // } @@ -79,7 +79,7 @@ extension Lambda { eventLoop: eventLoop ) - let context = Lambda.Context.__forTestsOnly( + let context = LambdaContext.__forTestsOnly( requestID: config.requestID, traceID: config.traceID, invokedFunctionARN: config.invokedFunctionARN, diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift index 17e60854..27c79e98 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift @@ -39,7 +39,7 @@ class LambdaHandlerTest: XCTestCase { self.initialized = true } - func handle(_ event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: LambdaContext) async throws -> String { event } } @@ -68,7 +68,7 @@ class LambdaHandlerTest: XCTestCase { throw TestError("kaboom") } - func handle(_ event: String, context: Lambda.Context) async throws { + func handle(_ event: String, context: LambdaContext) async throws { XCTFail("How can this be called if init failed") } } @@ -91,7 +91,7 @@ class LambdaHandlerTest: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: LambdaContext) async throws -> String { event } } @@ -114,7 +114,7 @@ class LambdaHandlerTest: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: String, context: Lambda.Context) async throws {} + func handle(_ event: String, context: LambdaContext) async throws {} } let maxTimes = Int.random(in: 1 ... 10) @@ -136,7 +136,7 @@ class LambdaHandlerTest: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: LambdaContext) async throws -> String { throw TestError("boom") } } @@ -159,7 +159,7 @@ class LambdaHandlerTest: XCTestCase { typealias Event = String typealias Output = String - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(event) } } @@ -181,7 +181,7 @@ class LambdaHandlerTest: XCTestCase { typealias Event = String typealias Output = Void - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(()) } } @@ -203,7 +203,7 @@ class LambdaHandlerTest: XCTestCase { typealias Event = String typealias Output = String - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeFailedFuture(TestError("boom")) } } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift index e2ba66f2..23899c63 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift @@ -19,7 +19,7 @@ struct EchoHandler: EventLoopLambdaHandler { typealias Event = String typealias Output = String - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(event) } } @@ -34,7 +34,7 @@ struct FailedHandler: EventLoopLambdaHandler { self.reason = reason } - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { context.eventLoop.makeFailedFuture(TestError(self.reason)) } } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift index 40b7ca83..460cfd64 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeTest.swift @@ -46,15 +46,15 @@ class LambdaRuntimeTest: XCTestCase { } struct CallbackLambdaHandler: ByteBufferLambdaHandler { - let handler: (Lambda.Context, ByteBuffer) -> (EventLoopFuture) + let handler: (LambdaContext, ByteBuffer) -> (EventLoopFuture) let shutdown: (Lambda.ShutdownContext) -> EventLoopFuture - init(_ handler: @escaping (Lambda.Context, ByteBuffer) -> (EventLoopFuture), shutdown: @escaping (Lambda.ShutdownContext) -> EventLoopFuture) { + init(_ handler: @escaping (LambdaContext, ByteBuffer) -> (EventLoopFuture), shutdown: @escaping (Lambda.ShutdownContext) -> EventLoopFuture) { self.handler = handler self.shutdown = shutdown } - func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: ByteBuffer, context: LambdaContext) -> EventLoopFuture { self.handler(context, event) } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift index c2819c3e..2bff7688 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift @@ -224,39 +224,45 @@ class LambdaTest: XCTestCase { let past2 = DispatchWallTime(millisSinceEpoch: Date(timeIntervalSinceNow: Double(-delta)).millisSinceEpoch) XCTAssertEqual(Double(past1.rawValue), Double(past2.rawValue), accuracy: 2_000_000.0) - let context = Lambda.Context(requestID: UUID().uuidString, - traceID: UUID().uuidString, - invokedFunctionARN: UUID().uuidString, - deadline: .now() + .seconds(1), - cognitoIdentity: nil, - clientContext: nil, - logger: Logger(label: "test"), - eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next(), - allocator: ByteBufferAllocator()) + let context = LambdaContext( + requestID: UUID().uuidString, + traceID: UUID().uuidString, + invokedFunctionARN: UUID().uuidString, + deadline: .now() + .seconds(1), + cognitoIdentity: nil, + clientContext: nil, + logger: Logger(label: "test"), + eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next(), + allocator: ByteBufferAllocator() + ) XCTAssertGreaterThan(context.deadline, .now()) - let expiredContext = Lambda.Context(requestID: context.requestID, - traceID: context.traceID, - invokedFunctionARN: context.invokedFunctionARN, - deadline: .now() - .seconds(1), - cognitoIdentity: context.cognitoIdentity, - clientContext: context.clientContext, - logger: context.logger, - eventLoop: context.eventLoop, - allocator: context.allocator) + let expiredContext = LambdaContext( + requestID: context.requestID, + traceID: context.traceID, + invokedFunctionARN: context.invokedFunctionARN, + deadline: .now() - .seconds(1), + cognitoIdentity: context.cognitoIdentity, + clientContext: context.clientContext, + logger: context.logger, + eventLoop: context.eventLoop, + allocator: context.allocator + ) XCTAssertLessThan(expiredContext.deadline, .now()) } func testGetRemainingTime() { - let context = Lambda.Context(requestID: UUID().uuidString, - traceID: UUID().uuidString, - invokedFunctionARN: UUID().uuidString, - deadline: .now() + .seconds(1), - cognitoIdentity: nil, - clientContext: nil, - logger: Logger(label: "test"), - eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next(), - allocator: ByteBufferAllocator()) + let context = LambdaContext( + requestID: UUID().uuidString, + traceID: UUID().uuidString, + invokedFunctionARN: UUID().uuidString, + deadline: .now() + .seconds(1), + cognitoIdentity: nil, + clientContext: nil, + logger: Logger(label: "test"), + eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next(), + allocator: ByteBufferAllocator() + ) XCTAssertLessThanOrEqual(context.getRemainingTime(), .seconds(1)) XCTAssertGreaterThan(context.getRemainingTime(), .milliseconds(800)) } diff --git a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift index 6471ca1b..6f10bfc9 100644 --- a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift +++ b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift @@ -43,7 +43,7 @@ class CodableLambdaTest: XCTestCase { let expected: Request - func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: Request, context: LambdaContext) -> EventLoopFuture { XCTAssertEqual(event, self.expected) return context.eventLoop.makeSucceededVoidFuture() } @@ -68,7 +68,7 @@ class CodableLambdaTest: XCTestCase { let expected: Request - func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: Request, context: LambdaContext) -> EventLoopFuture { XCTAssertEqual(event, self.expected) return context.eventLoop.makeSucceededFuture(Response(requestId: event.requestId)) } @@ -93,7 +93,7 @@ class CodableLambdaTest: XCTestCase { init(context: Lambda.InitializationContext) async throws {} - func handle(_ event: Request, context: Lambda.Context) async throws { + func handle(_ event: Request, context: LambdaContext) async throws { XCTAssertEqual(event, self.expected) } } @@ -122,7 +122,7 @@ class CodableLambdaTest: XCTestCase { init(context: Lambda.InitializationContext) async throws {} - func handle(_ event: Request, context: Lambda.Context) async throws -> Response { + func handle(_ event: Request, context: LambdaContext) async throws -> Response { XCTAssertEqual(event, self.expected) return Response(requestId: event.requestId) } @@ -146,22 +146,26 @@ class CodableLambdaTest: XCTestCase { #endif // convenience method - func newContext() -> Lambda.Context { - Lambda.Context(requestID: UUID().uuidString, - traceID: "abc123", - invokedFunctionARN: "aws:arn:", - deadline: .now() + .seconds(3), - cognitoIdentity: nil, - clientContext: nil, - logger: Logger(label: "test"), - eventLoop: self.eventLoopGroup.next(), - allocator: ByteBufferAllocator()) + func newContext() -> LambdaContext { + LambdaContext( + requestID: UUID().uuidString, + traceID: "abc123", + invokedFunctionARN: "aws:arn:", + deadline: .now() + .seconds(3), + cognitoIdentity: nil, + clientContext: nil, + logger: Logger(label: "test"), + eventLoop: self.eventLoopGroup.next(), + allocator: ByteBufferAllocator() + ) } func newInitContext() -> Lambda.InitializationContext { - Lambda.InitializationContext(logger: Logger(label: "test"), - eventLoop: self.eventLoopGroup.next(), - allocator: ByteBufferAllocator()) + Lambda.InitializationContext( + logger: Logger(label: "test"), + eventLoop: self.eventLoopGroup.next(), + allocator: ByteBufferAllocator() + ) } } diff --git a/Tests/AWSLambdaTestingTests/Tests.swift b/Tests/AWSLambdaTestingTests/Tests.swift index 8b888931..83ac4217 100644 --- a/Tests/AWSLambdaTestingTests/Tests.swift +++ b/Tests/AWSLambdaTestingTests/Tests.swift @@ -35,7 +35,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: Request, context: Lambda.Context) async throws -> Response { + func handle(_ event: Request, context: LambdaContext) async throws -> Response { Response(message: "echo" + event.name) } } @@ -59,7 +59,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: Request, context: Lambda.Context) async throws { + func handle(_ event: Request, context: LambdaContext) async throws { LambdaTestingTests.VoidLambdaHandlerInvokeCount += 1 } } @@ -79,7 +79,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: String, context: Lambda.Context) async throws { + func handle(_ event: String, context: LambdaContext) async throws { throw MyError() } } @@ -96,7 +96,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(_ event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: LambdaContext) async throws -> String { try await Task.sleep(nanoseconds: 500 * 1000 * 1000) return event }