@@ -17,23 +17,15 @@ import NIOCore
17
17
import XCTest
18
18
19
19
class LambdaHandlerTest : XCTestCase {
20
- // MARK: - LambdaHandler
20
+ // MARK: - SimpleLambdaHandler
21
21
22
22
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
23
- func testBootstrapSuccess ( ) {
23
+ func testBootstrapSimpleNoInit ( ) {
24
24
let server = MockLambdaServer ( behavior: Behavior ( ) )
25
25
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
26
26
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
27
27
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 {
37
29
func handle( _ event: String , context: LambdaContext ) async throws -> String {
38
30
event
39
31
}
@@ -46,45 +38,44 @@ class LambdaHandlerTest: XCTestCase {
46
38
}
47
39
48
40
@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 ( ) )
51
43
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
52
44
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
53
45
54
- struct TestBootstrapHandler : LambdaHandler {
55
- typealias Event = String
56
- typealias Output = Void
57
-
46
+ struct TestBootstrapHandler : SimpleLambdaHandler {
58
47
var initialized = false
59
48
60
- init ( context : LambdaInitializationContext ) async throws {
49
+ init ( ) {
61
50
XCTAssertFalse ( self . initialized)
62
- try await Task . sleep ( nanoseconds: 100 * 1000 * 1000 ) // 0.1 seconds
63
- throw TestError ( " kaboom " )
51
+ self . initialized = true
64
52
}
65
53
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
68
56
}
69
57
}
70
58
71
59
let maxTimes = Int . random ( in: 10 ... 20 )
72
60
let configuration = LambdaConfiguration ( lifecycle: . init( maxTimes: maxTimes) )
73
61
let result = Lambda . run ( configuration: configuration, handlerType: TestBootstrapHandler . self)
74
- assertLambdaRuntimeResult ( result, shouldFailWithError : TestError ( " kaboom " ) )
62
+ assertLambdaRuntimeResult ( result, shouldHaveRun : maxTimes )
75
63
}
76
64
65
+ // MARK: - LambdaHandler
66
+
77
67
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
78
- func testBootstrapInitSimple ( ) {
68
+ func testBootstrapSuccess ( ) {
79
69
let server = MockLambdaServer ( behavior: Behavior ( ) )
80
70
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
81
71
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
82
72
83
73
struct TestBootstrapHandler : LambdaHandler {
84
74
var initialized = false
85
75
86
- init ( ) {
76
+ init ( context : LambdaInitializationContext ) async throws {
87
77
XCTAssertFalse ( self . initialized)
78
+ try await Task . sleep ( nanoseconds: 100 * 1000 * 1000 ) // 0.1 seconds
88
79
self . initialized = true
89
80
}
90
81
@@ -100,21 +91,32 @@ class LambdaHandlerTest: XCTestCase {
100
91
}
101
92
102
93
@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 ( ) )
105
96
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
106
97
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
107
98
108
99
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 " )
111
113
}
112
114
}
113
115
114
116
let maxTimes = Int . random ( in: 10 ... 20 )
115
117
let configuration = LambdaConfiguration ( lifecycle: . init( maxTimes: maxTimes) )
116
118
let result = Lambda . run ( configuration: configuration, handlerType: TestBootstrapHandler . self)
117
- assertLambdaRuntimeResult ( result, shouldHaveRun : maxTimes )
119
+ assertLambdaRuntimeResult ( result, shouldFailWithError : TestError ( " kaboom " ) )
118
120
}
119
121
120
122
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
@@ -123,7 +125,7 @@ class LambdaHandlerTest: XCTestCase {
123
125
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
124
126
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
125
127
126
- struct Handler : LambdaHandler {
128
+ struct Handler : SimpleLambdaHandler {
127
129
func handle( _ event: String , context: LambdaContext ) async throws -> String {
128
130
event
129
131
}
@@ -141,7 +143,7 @@ class LambdaHandlerTest: XCTestCase {
141
143
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
142
144
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
143
145
144
- struct Handler : LambdaHandler {
146
+ struct Handler : SimpleLambdaHandler {
145
147
func handle( _ event: String , context: LambdaContext ) async throws { }
146
148
}
147
149
@@ -158,7 +160,7 @@ class LambdaHandlerTest: XCTestCase {
158
160
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
159
161
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
160
162
161
- struct Handler : LambdaHandler {
163
+ struct Handler : SimpleLambdaHandler {
162
164
func handle( _ event: String , context: LambdaContext ) async throws -> String {
163
165
throw TestError ( " boom " )
164
166
}
0 commit comments