mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
We want that the runtime only depends on `FoundationEssentials` where available (ie. on linux) to ensure small binary size. ### Motivation: Smaller binary size is good for lambda deployment and cold-start times. The runtime should only depend on `FoundationEssentials`. ### Modifications: - replace `import Foundation` with `import FoundationEssentials` if `FoundationEssentials` is available. - I also applied the same treatment to tests to ensure that catch error where tests run on linux and we use API that is only available in `Foundation` which easily happens when you develop on macOS (where always full `Foundation` is available). ### Result: This should allow builds without linking full `Foundation`.
95 lines
2.7 KiB
Swift
95 lines
2.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Logging
|
|
import NIOCore
|
|
import Testing
|
|
|
|
@testable import AWSLambdaRuntimeCore
|
|
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
@Suite
|
|
struct LambdaRunLoopTests {
|
|
struct MockEchoHandler: StreamingLambdaHandler {
|
|
func handle(
|
|
_ event: ByteBuffer,
|
|
responseWriter: some LambdaResponseStreamWriter,
|
|
context: LambdaContext
|
|
) async throws {
|
|
try await responseWriter.writeAndFinish(event)
|
|
}
|
|
}
|
|
|
|
struct FailingHandler: StreamingLambdaHandler {
|
|
func handle(
|
|
_ event: ByteBuffer,
|
|
responseWriter: some LambdaResponseStreamWriter,
|
|
context: LambdaContext
|
|
) async throws {
|
|
throw LambdaError.handlerError
|
|
}
|
|
}
|
|
|
|
let mockClient = LambdaMockClient()
|
|
let mockEchoHandler = MockEchoHandler()
|
|
let failingHandler = FailingHandler()
|
|
|
|
@Test func testRunLoop() async throws {
|
|
let inputEvent = ByteBuffer(string: "Test Invocation Event")
|
|
|
|
try await withThrowingTaskGroup(of: Void.self) { group in
|
|
group.addTask {
|
|
try await Lambda.runLoop(
|
|
runtimeClient: self.mockClient,
|
|
handler: self.mockEchoHandler,
|
|
logger: Logger(label: "RunLoopTest")
|
|
)
|
|
}
|
|
|
|
let response = try await self.mockClient.invoke(event: inputEvent)
|
|
#expect(response == inputEvent)
|
|
|
|
group.cancelAll()
|
|
}
|
|
}
|
|
|
|
@Test func testRunLoopError() async throws {
|
|
let inputEvent = ByteBuffer(string: "Test Invocation Event")
|
|
|
|
await withThrowingTaskGroup(of: Void.self) { group in
|
|
group.addTask {
|
|
try await Lambda.runLoop(
|
|
runtimeClient: self.mockClient,
|
|
handler: self.failingHandler,
|
|
logger: Logger(label: "RunLoopTest")
|
|
)
|
|
}
|
|
|
|
await #expect(
|
|
throws: LambdaError.handlerError,
|
|
performing: {
|
|
try await self.mockClient.invoke(event: inputEvent)
|
|
}
|
|
)
|
|
|
|
group.cancelAll()
|
|
}
|
|
}
|
|
}
|