|
16 | 16 | import NIO
|
17 | 17 | import XCTest
|
18 | 18 |
|
19 |
| -class StringLambdaTest: XCTestCase { |
| 19 | +class LambdaHandlerTest: XCTestCase { |
| 20 | + // MARK: - Callback |
| 21 | + |
20 | 22 | func testCallbackSuccess() {
|
21 | 23 | let server = MockLambdaServer(behavior: Behavior())
|
22 | 24 | XCTAssertNoThrow(try server.start().wait())
|
@@ -77,6 +79,80 @@ class StringLambdaTest: XCTestCase {
|
77 | 79 | assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes)
|
78 | 80 | }
|
79 | 81 |
|
| 82 | + #if compiler(>=5.5) |
| 83 | + |
| 84 | + // MARK: - AsyncLambdaHandler |
| 85 | + |
| 86 | + @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) |
| 87 | + func testAsyncHandlerSuccess() { |
| 88 | + let server = MockLambdaServer(behavior: Behavior()) |
| 89 | + XCTAssertNoThrow(try server.start().wait()) |
| 90 | + defer { XCTAssertNoThrow(try server.stop().wait()) } |
| 91 | + |
| 92 | + struct Handler: AsyncLambdaHandler { |
| 93 | + typealias In = String |
| 94 | + typealias Out = String |
| 95 | + |
| 96 | + init(context: Lambda.InitializationContext) {} |
| 97 | + |
| 98 | + func handle(event: String, context: Lambda.Context) async throws -> String { |
| 99 | + event |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + let maxTimes = Int.random(in: 1 ... 10) |
| 104 | + let configuration = Lambda.Configuration(lifecycle: .init(maxTimes: maxTimes)) |
| 105 | + let result = Lambda.run(configuration: configuration, factory: Handler.init) |
| 106 | + assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes) |
| 107 | + } |
| 108 | + |
| 109 | + @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) |
| 110 | + func testVoidAsyncHandlerSuccess() { |
| 111 | + let server = MockLambdaServer(behavior: Behavior(result: .success(nil))) |
| 112 | + XCTAssertNoThrow(try server.start().wait()) |
| 113 | + defer { XCTAssertNoThrow(try server.stop().wait()) } |
| 114 | + |
| 115 | + struct Handler: AsyncLambdaHandler { |
| 116 | + typealias In = String |
| 117 | + typealias Out = Void |
| 118 | + |
| 119 | + init(context: Lambda.InitializationContext) {} |
| 120 | + |
| 121 | + func handle(event: String, context: Lambda.Context) async throws {} |
| 122 | + } |
| 123 | + |
| 124 | + let maxTimes = Int.random(in: 1 ... 10) |
| 125 | + let configuration = Lambda.Configuration(lifecycle: .init(maxTimes: maxTimes)) |
| 126 | + let result = Lambda.run(configuration: configuration, factory: Handler.init) |
| 127 | + assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes) |
| 128 | + } |
| 129 | + |
| 130 | + @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) |
| 131 | + func testAsyncHandlerFailure() { |
| 132 | + let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom")))) |
| 133 | + XCTAssertNoThrow(try server.start().wait()) |
| 134 | + defer { XCTAssertNoThrow(try server.stop().wait()) } |
| 135 | + |
| 136 | + struct Handler: AsyncLambdaHandler { |
| 137 | + typealias In = String |
| 138 | + typealias Out = String |
| 139 | + |
| 140 | + init(context: Lambda.InitializationContext) {} |
| 141 | + |
| 142 | + func handle(event: String, context: Lambda.Context) async throws -> String { |
| 143 | + throw TestError("boom") |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + let maxTimes = Int.random(in: 1 ... 10) |
| 148 | + let configuration = Lambda.Configuration(lifecycle: .init(maxTimes: maxTimes)) |
| 149 | + let result = Lambda.run(configuration: configuration, factory: Handler.init) |
| 150 | + assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes) |
| 151 | + } |
| 152 | + #endif |
| 153 | + |
| 154 | + // MARK: - EventLoop |
| 155 | + |
80 | 156 | func testEventLoopSuccess() {
|
81 | 157 | let server = MockLambdaServer(behavior: Behavior())
|
82 | 158 | XCTAssertNoThrow(try server.start().wait())
|
@@ -137,6 +213,8 @@ class StringLambdaTest: XCTestCase {
|
137 | 213 | assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes)
|
138 | 214 | }
|
139 | 215 |
|
| 216 | + // MARK: - Closure |
| 217 | + |
140 | 218 | func testClosureSuccess() {
|
141 | 219 | let server = MockLambdaServer(behavior: Behavior())
|
142 | 220 | XCTAssertNoThrow(try server.start().wait())
|
|
0 commit comments