forked from swift-server/swift-aws-lambda-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambdaRunLoopTests.swift
108 lines (94 loc) · 3.5 KB
/
LambdaRunLoopTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===----------------------------------------------------------------------===//
//
// 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 AWSLambdaRuntime
#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 {
context.logger.info("Test")
try await responseWriter.writeAndFinish(event)
}
}
struct FailingHandler: StreamingLambdaHandler {
func handle(
_ event: ByteBuffer,
responseWriter: some LambdaResponseStreamWriter,
context: LambdaContext
) async throws {
context.logger.info("Test")
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
let logStore = CollectEverythingLogHandler.LogStore()
group.addTask {
try await Lambda.runLoop(
runtimeClient: self.mockClient,
handler: self.mockEchoHandler,
logger: Logger(
label: "RunLoopTest",
factory: { _ in CollectEverythingLogHandler(logStore: logStore) }
)
)
}
let requestID = UUID().uuidString
let response = try await self.mockClient.invoke(event: inputEvent, requestID: requestID)
#expect(response == inputEvent)
logStore.assertContainsLog("Test", ("aws-request-id", .exactMatch(requestID)))
group.cancelAll()
}
}
@Test func testRunLoopError() async throws {
let inputEvent = ByteBuffer(string: "Test Invocation Event")
await withThrowingTaskGroup(of: Void.self) { group in
let logStore = CollectEverythingLogHandler.LogStore()
group.addTask {
try await Lambda.runLoop(
runtimeClient: self.mockClient,
handler: self.failingHandler,
logger: Logger(
label: "RunLoopTest",
factory: { _ in CollectEverythingLogHandler(logStore: logStore) }
)
)
}
let requestID = UUID().uuidString
await #expect(
throws: LambdaError.handlerError,
performing: {
try await self.mockClient.invoke(event: inputEvent, requestID: requestID)
}
)
logStore.assertContainsLog("Test", ("aws-request-id", .exactMatch(requestID)))
group.cancelAll()
}
}
}