diff --git a/Sources/VaporAWSLambdaRuntime/LambdaServer.swift b/Sources/VaporAWSLambdaRuntime/LambdaServer.swift index de1fbf1..9eb1d4c 100644 --- a/Sources/VaporAWSLambdaRuntime/LambdaServer.swift +++ b/Sources/VaporAWSLambdaRuntime/LambdaServer.swift @@ -91,11 +91,12 @@ public class LambdaServer: Server { } } - private let application: Application - private let responder: Responder - private let configuration: Configuration - private let eventLoop: EventLoop - private var lambdaLifecycle: Lambda.Lifecycle + let application: Application + let responder: Responder + let configuration: Configuration + let eventLoop: EventLoop + let lambdaHandler: ByteBufferLambdaHandler + let lambdaLifecycle: Lambda.Lifecycle init(application: Application, responder: Responder, @@ -123,6 +124,7 @@ public class LambdaServer: Server { ) { $0.eventLoop.makeSucceededFuture(handler) } + self.lambdaHandler = handler } public func start(hostname _: String?, port _: Int?) throws { @@ -142,9 +144,6 @@ public class LambdaServer: Server { } public func shutdown() { - // this should only be executed after someone has called `app.shutdown()` - // on lambda the ones calling should always be us! - // If we have called shutdown, the lambda server already is shutdown. - // That means, we have nothing to do here. + self.lambdaLifecycle.shutdown() } } diff --git a/Tests/VaporAWSLambdaRuntimeTests/LambdaServerTests.swift b/Tests/VaporAWSLambdaRuntimeTests/LambdaServerTests.swift new file mode 100644 index 0000000..bfa9e4b --- /dev/null +++ b/Tests/VaporAWSLambdaRuntimeTests/LambdaServerTests.swift @@ -0,0 +1,74 @@ +import AWSLambdaTesting +@testable import AWSLambdaRuntimeCore +@testable import AWSLambdaEvents +import Logging +import NIO +import Vapor +@testable import VaporAWSLambdaRuntime +import XCTest + +final class LambdaServerTests: XCTestCase { + func testLambdaServer() { + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } + + let app = Application(.development, .shared(eventLoopGroup)) + defer { XCTAssertNoThrow(app.shutdown()) } + + struct Name: Codable { + let name: String + } + + struct Hello: Content { + let hello: String + } + + app.get("hello") { (_) -> Hello in + Hello(hello: "world") + } + + app.post("hello") { req -> Hello in + let name = try req.content.decode(Name.self) + return Hello(hello: name.name) + } + + let server = app.lambda.server.shared + let handler = server.lambdaHandler as! APIGatewayV2Handler + + let request = APIGateway.V2.Request( + version: "1.1", + routeKey: "/test/123", + rawPath: "/test/123", + rawQueryString: "", + cookies: nil, + headers: [:], + queryStringParameters: nil, + pathParameters: nil, + context: .init( + accountId: "123", + apiId: "abc", + domainName: "apigateway.region", + domainPrefix: "", + stage: "default", + requestId: "123", + http: .init( + method: .GET, + path: "/test/123", + protocol: "https", + sourceIp: "127.0.0.1", + userAgent: "test-user-agent" + ), + authorizer: nil, + time: "123", + timeEpoch: UInt64(Date().timeIntervalSince1970) + ), + stageVariables: nil, + body: nil, + isBase64Encoded: false) + + var response: APIGateway.V2.Response? + XCTAssertNoThrow(response = try Lambda.test(handler, with: request)) + +// XCTAssertNoThrow(try server.onShutdown.wait()) + } +}