@@ -17,23 +17,15 @@ import NIOCore
1717import XCTest
1818
1919class LambdaHandlerTest : XCTestCase {
20- // MARK: - LambdaHandler
20+ // MARK: - SimpleLambdaHandler
2121
2222 @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
23- func testBootstrapSuccess ( ) {
23+ func testBootstrapSimpleNoInit ( ) {
2424 let server = MockLambdaServer ( behavior: Behavior ( ) )
2525 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
2626 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
2727
28- struct TestBootstrapHandler : LambdaHandler {
29- var initialized = false
30-
31- init ( context: LambdaInitializationContext ) async throws {
32- XCTAssertFalse ( self . initialized)
33- try await Task . sleep ( nanoseconds: 100 * 1000 * 1000 ) // 0.1 seconds
34- self . initialized = true
35- }
36-
28+ struct TestBootstrapHandler : SimpleLambdaHandler {
3729 func handle( _ event: String , context: LambdaContext ) async throws -> String {
3830 event
3931 }
@@ -46,45 +38,44 @@ class LambdaHandlerTest: XCTestCase {
4638 }
4739
4840 @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
49- func testBootstrapFailure ( ) {
50- let server = MockLambdaServer ( behavior: FailedBootstrapBehavior ( ) )
41+ func testBootstrapSimpleInit ( ) {
42+ let server = MockLambdaServer ( behavior: Behavior ( ) )
5143 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
5244 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
5345
54- struct TestBootstrapHandler : LambdaHandler {
55- typealias Event = String
56- typealias Output = Void
57-
46+ struct TestBootstrapHandler : SimpleLambdaHandler {
5847 var initialized = false
5948
60- init ( context : LambdaInitializationContext ) async throws {
49+ init ( ) {
6150 XCTAssertFalse ( self . initialized)
62- try await Task . sleep ( nanoseconds: 100 * 1000 * 1000 ) // 0.1 seconds
63- throw TestError ( " kaboom " )
51+ self . initialized = true
6452 }
6553
66- func handle( _ event: String , context: LambdaContext ) async throws {
67- XCTFail ( " How can this be called if init failed " )
54+ func handle( _ event: String , context: LambdaContext ) async throws -> String {
55+ event
6856 }
6957 }
7058
7159 let maxTimes = Int . random ( in: 10 ... 20 )
7260 let configuration = LambdaConfiguration ( lifecycle: . init( maxTimes: maxTimes) )
7361 let result = Lambda . run ( configuration: configuration, handlerType: TestBootstrapHandler . self)
74- assertLambdaRuntimeResult ( result, shouldFailWithError : TestError ( " kaboom " ) )
62+ assertLambdaRuntimeResult ( result, shouldHaveRun : maxTimes )
7563 }
7664
65+ // MARK: - LambdaHandler
66+
7767 @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
78- func testBootstrapInitSimple ( ) {
68+ func testBootstrapSuccess ( ) {
7969 let server = MockLambdaServer ( behavior: Behavior ( ) )
8070 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
8171 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
8272
8373 struct TestBootstrapHandler : LambdaHandler {
8474 var initialized = false
8575
86- init ( ) {
76+ init ( context : LambdaInitializationContext ) async throws {
8777 XCTAssertFalse ( self . initialized)
78+ try await Task . sleep ( nanoseconds: 100 * 1000 * 1000 ) // 0.1 seconds
8879 self . initialized = true
8980 }
9081
@@ -100,21 +91,32 @@ class LambdaHandlerTest: XCTestCase {
10091 }
10192
10293 @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
103- func testBootstrapNoInit ( ) {
104- let server = MockLambdaServer ( behavior: Behavior ( ) )
94+ func testBootstrapFailure ( ) {
95+ let server = MockLambdaServer ( behavior: FailedBootstrapBehavior ( ) )
10596 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
10697 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
10798
10899 struct TestBootstrapHandler : LambdaHandler {
109- func handle( _ event: String , context: LambdaContext ) async throws -> String {
110- event
100+ typealias Event = String
101+ typealias Output = Void
102+
103+ var initialized = false
104+
105+ init ( context: LambdaInitializationContext ) async throws {
106+ XCTAssertFalse ( self . initialized)
107+ try await Task . sleep ( nanoseconds: 100 * 1000 * 1000 ) // 0.1 seconds
108+ throw TestError ( " kaboom " )
109+ }
110+
111+ func handle( _ event: String , context: LambdaContext ) async throws {
112+ XCTFail ( " How can this be called if init failed " )
111113 }
112114 }
113115
114116 let maxTimes = Int . random ( in: 10 ... 20 )
115117 let configuration = LambdaConfiguration ( lifecycle: . init( maxTimes: maxTimes) )
116118 let result = Lambda . run ( configuration: configuration, handlerType: TestBootstrapHandler . self)
117- assertLambdaRuntimeResult ( result, shouldHaveRun : maxTimes )
119+ assertLambdaRuntimeResult ( result, shouldFailWithError : TestError ( " kaboom " ) )
118120 }
119121
120122 @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
@@ -123,7 +125,7 @@ class LambdaHandlerTest: XCTestCase {
123125 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
124126 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
125127
126- struct Handler : LambdaHandler {
128+ struct Handler : SimpleLambdaHandler {
127129 func handle( _ event: String , context: LambdaContext ) async throws -> String {
128130 event
129131 }
@@ -141,7 +143,7 @@ class LambdaHandlerTest: XCTestCase {
141143 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
142144 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
143145
144- struct Handler : LambdaHandler {
146+ struct Handler : SimpleLambdaHandler {
145147 func handle( _ event: String , context: LambdaContext ) async throws { }
146148 }
147149
@@ -158,7 +160,7 @@ class LambdaHandlerTest: XCTestCase {
158160 XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
159161 defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
160162
161- struct Handler : LambdaHandler {
163+ struct Handler : SimpleLambdaHandler {
162164 func handle( _ event: String , context: LambdaContext ) async throws -> String {
163165 throw TestError ( " boom " )
164166 }
0 commit comments