-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathLambdaRuntimeClientTests.swift
141 lines (122 loc) · 5.39 KB
/
LambdaRuntimeClientTests.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//===----------------------------------------------------------------------===//
//
// 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 NIOPosix
import Testing
import struct Foundation.UUID
@testable import AWSLambdaRuntimeCore
@Suite
struct LambdaRuntimeClientTests {
let logger = {
var logger = Logger(label: "NewLambdaClientRuntimeTest")
logger.logLevel = .trace
return logger
}()
@Test
func testSimpleInvocations() async throws {
struct HappyBehavior: LambdaServerBehavior {
let requestId = UUID().uuidString
let event = "hello"
func getInvocation() -> GetInvocationResult {
.success((self.requestId, self.event))
}
func processResponse(requestId: String, response: String?) -> Result<Void, ProcessResponseError> {
#expect(self.requestId == requestId)
#expect(self.event == response)
return .success(())
}
func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError> {
Issue.record("should not report error")
return .failure(.internalServerError)
}
func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError> {
Issue.record("should not report init error")
return .failure(.internalServerError)
}
}
try await withMockServer(behaviour: HappyBehavior()) { port in
let configuration = LambdaRuntimeClient.Configuration(ip: "127.0.0.1", port: port)
try await LambdaRuntimeClient.withRuntimeClient(
configuration: configuration,
eventLoop: NIOSingletons.posixEventLoopGroup.next(),
logger: self.logger
) { runtimeClient in
do {
let (invocation, writer) = try await runtimeClient.nextInvocation()
let expected = ByteBuffer(string: "hello")
#expect(invocation.event == expected)
try await writer.writeAndFinish(expected)
}
do {
let (invocation, writer) = try await runtimeClient.nextInvocation()
let expected = ByteBuffer(string: "hello")
#expect(invocation.event == expected)
try await writer.write(ByteBuffer(string: "h"))
try await writer.write(ByteBuffer(string: "e"))
try await writer.write(ByteBuffer(string: "l"))
try await writer.write(ByteBuffer(string: "l"))
try await writer.write(ByteBuffer(string: "o"))
try await writer.finish()
}
}
}
}
@Test
func testCancellation() async throws {
struct HappyBehavior: LambdaServerBehavior {
let requestId = UUID().uuidString
let event = "hello"
func getInvocation() -> GetInvocationResult {
.success((self.requestId, self.event))
}
func processResponse(requestId: String, response: String?) -> Result<Void, ProcessResponseError> {
#expect(self.requestId == requestId)
#expect(self.event == response)
return .success(())
}
func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError> {
Issue.record("should not report error")
return .failure(.internalServerError)
}
func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError> {
Issue.record("should not report init error")
return .failure(.internalServerError)
}
}
try await withMockServer(behaviour: HappyBehavior()) { port in
try await LambdaRuntimeClient.withRuntimeClient(
configuration: .init(ip: "127.0.0.1", port: port),
eventLoop: NIOSingletons.posixEventLoopGroup.next(),
logger: self.logger
) { runtimeClient in
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
while true {
let (_, writer) = try await runtimeClient.nextInvocation()
// Wrap this is a task so cancellation isn't propagated to the write calls
try await Task {
try await writer.write(ByteBuffer(string: "hello"))
try await writer.finish()
}.value
}
}
// wait a small amount to ensure we are waiting for continuation
try await Task.sleep(for: .milliseconds(100))
group.cancelAll()
}
}
}
}
}