@@ -44,25 +44,25 @@ class LambdaLifecycleTest: XCTestCase {
44
44
}
45
45
}
46
46
47
- func testSyncShutdownIsCalledWhenLambdaShutsdown( ) {
48
- struct CallbackLambdaHandler : ByteBufferLambdaHandler {
49
- let handler : ( Lambda . Context , ByteBuffer ) -> ( EventLoopFuture < ByteBuffer ? > )
50
- let shutdown : ( Lambda . ShutdownContext ) -> EventLoopFuture < Void >
51
-
52
- init ( _ handler: @escaping ( Lambda . Context , ByteBuffer ) -> ( EventLoopFuture < ByteBuffer ? > ) , shutdown: @escaping ( Lambda . ShutdownContext ) -> EventLoopFuture < Void > ) {
53
- self . handler = handler
54
- self . shutdown = shutdown
55
- }
47
+ struct CallbackLambdaHandler : ByteBufferLambdaHandler {
48
+ let handler : ( Lambda . Context , ByteBuffer ) -> ( EventLoopFuture < ByteBuffer ? > )
49
+ let shutdown : ( Lambda . ShutdownContext ) -> EventLoopFuture < Void >
56
50
57
- func handle( context: Lambda . Context , event: ByteBuffer ) -> EventLoopFuture < ByteBuffer ? > {
58
- self . handler ( context, event)
59
- }
51
+ init ( _ handler: @escaping ( Lambda . Context , ByteBuffer ) -> ( EventLoopFuture < ByteBuffer ? > ) , shutdown: @escaping ( Lambda . ShutdownContext ) -> EventLoopFuture < Void > ) {
52
+ self . handler = handler
53
+ self . shutdown = shutdown
54
+ }
60
55
61
- func shutdown( context: Lambda . ShutdownContext ) -> EventLoopFuture < Void > {
62
- self . shutdown ( context)
63
- }
56
+ func handle( context: Lambda . Context , event: ByteBuffer ) -> EventLoopFuture < ByteBuffer ? > {
57
+ self . handler ( context, event)
64
58
}
65
59
60
+ func shutdown( context: Lambda . ShutdownContext ) -> EventLoopFuture < Void > {
61
+ self . shutdown ( context)
62
+ }
63
+ }
64
+
65
+ func testShutdownIsCalledWhenLambdaShutsdown( ) {
66
66
let server = MockLambdaServer ( behavior: BadBehavior ( ) )
67
67
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
68
68
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
@@ -87,6 +87,37 @@ class LambdaLifecycleTest: XCTestCase {
87
87
}
88
88
XCTAssertEqual ( count, 1 )
89
89
}
90
+
91
+ func testLambdaResultIfShutsdownIsUnclean( ) {
92
+ let server = MockLambdaServer ( behavior: BadBehavior ( ) )
93
+ XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
94
+ defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
95
+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
96
+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
97
+
98
+ var count = 0
99
+ let handler = CallbackLambdaHandler ( { XCTFail ( " Should not be reached " ) ; return $0. eventLoop. makeSucceededFuture ( $1) } ) { context in
100
+ count += 1
101
+ return context. eventLoop. makeFailedFuture ( TestError ( " kaboom " ) )
102
+ }
103
+
104
+ let eventLoop = eventLoopGroup. next ( )
105
+ let logger = Logger ( label: " TestLogger " )
106
+ let lifecycle = Lambda . Lifecycle ( eventLoop: eventLoop, logger: logger, factory: {
107
+ $0. makeSucceededFuture ( handler)
108
+ } )
109
+
110
+ XCTAssertNoThrow ( _ = try eventLoop. flatSubmit { lifecycle. start ( ) } . wait ( ) )
111
+ XCTAssertThrowsError ( _ = try lifecycle. shutdownFuture. wait ( ) ) { error in
112
+ guard case Lambda . RuntimeError . shutdownError( let shutdownError, . failure( let runtimeError) ) = error else {
113
+ XCTFail ( " Unexpected error " ) ; return
114
+ }
115
+
116
+ XCTAssertEqual ( shutdownError as? TestError , TestError ( " kaboom " ) )
117
+ XCTAssertEqual ( runtimeError as? Lambda . RuntimeError , . badStatusCode( . internalServerError) )
118
+ }
119
+ XCTAssertEqual ( count, 1 )
120
+ }
90
121
}
91
122
92
123
struct BadBehavior : LambdaServerBehavior {
0 commit comments