forked from awslabs/swift-aws-lambda-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaRunLoopTests.swift
More file actions
94 lines (80 loc) · 2.71 KB
/
Copy pathLambdaRunLoopTests.swift
File metadata and controls
94 lines (80 loc) · 2.71 KB
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
//===----------------------------------------------------------------------===//
//
// 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 {
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()
}
}
}