@@ -19,43 +19,70 @@ import Logging
19
19
import NIO
20
20
21
21
extension Lambda {
22
+ public struct TestConfig {
23
+ public var requestId : String
24
+ public var traceId : String
25
+ public var invokedFunctionArn : String
26
+ public var timeout : Double
27
+
28
+ public init ( requestId: String = " \( DispatchTime . now ( ) . uptimeNanoseconds) " ,
29
+ traceId: String = " Root= \( DispatchTime . now ( ) . uptimeNanoseconds) ;Parent= \( DispatchTime . now ( ) . uptimeNanoseconds) ;Sampled=1 " ,
30
+ invokedFunctionArn: String = " arn:aws:lambda:us-west-1: \( DispatchTime . now ( ) . uptimeNanoseconds) :function:custom-runtime " ,
31
+ timeout: Double = 5 ) {
32
+ self . requestId = requestId
33
+ self . traceId = traceId
34
+ self . invokedFunctionArn = invokedFunctionArn
35
+ self . timeout = timeout
36
+ }
37
+ }
38
+
22
39
public static func test( _ closure: @escaping StringLambdaClosure ,
23
- with payload : String ,
24
- _ body : @escaping ( Result < String , Error > ) -> Void ) {
25
- Self . test ( StringLambdaClosureWrapper ( closure) , with : payload , body )
40
+ config : TestConfig = . init ( ) ,
41
+ with payload : String ) throws -> String {
42
+ try Self . test ( StringLambdaClosureWrapper ( closure) , config : config , with : payload )
26
43
}
27
44
28
45
public static func test( _ closure: @escaping StringVoidLambdaClosure ,
29
- with payload : String ,
30
- _ body : @escaping ( Result < Void , Error > ) -> Void ) {
31
- Self . test ( StringVoidLambdaClosureWrapper ( closure) , with : payload , body )
46
+ config : TestConfig = . init ( ) ,
47
+ with payload : String ) throws {
48
+ _ = try Self . test ( StringVoidLambdaClosureWrapper ( closure) , config : config , with : payload )
32
49
}
33
50
34
- public static func test< In: Decodable , Out: Encodable > ( _ closure: @escaping CodableLambdaClosure < In , Out > ,
35
- with payload: In ,
36
- _ body: @escaping ( Result < Out , Error > ) -> Void ) {
37
- Self . test ( CodableLambdaClosureWrapper ( closure) , with: payload, body)
51
+ public static func test< In: Decodable , Out: Encodable > (
52
+ _ closure: @escaping CodableLambdaClosure < In , Out > ,
53
+ config: TestConfig = . init( ) ,
54
+ with payload: In
55
+ ) throws -> Out {
56
+ try Self . test ( CodableLambdaClosureWrapper ( closure) , config: config, with: payload)
38
57
}
39
58
40
59
public static func test< In: Decodable > ( _ closure: @escaping CodableVoidLambdaClosure < In > ,
41
- with payload : In ,
42
- _ body : @escaping ( Result < Void , Error > ) -> Void ) {
43
- Self . test ( CodableVoidLambdaClosureWrapper ( closure) , with : payload , body )
60
+ config : TestConfig = . init ( ) ,
61
+ with payload : In ) throws {
62
+ _ = try Self . test ( CodableVoidLambdaClosureWrapper ( closure) , config : config , with : payload )
44
63
}
45
64
46
- public static func test< In, Out, Handler: EventLoopLambdaHandler > ( _ handler: Handler ,
47
- with payload: In ,
48
- _ body: @escaping ( Result < Out , Error > ) -> Void ) where Handler. In == In , Handler. Out == Out {
65
+ public static func test< In, Out, Handler: EventLoopLambdaHandler > (
66
+ _ handler: Handler ,
67
+ config: TestConfig = . init( ) ,
68
+ with payload: In
69
+ ) throws -> Out where Handler. In == In , Handler. Out == Out {
49
70
let logger = Logger ( label: " test " )
50
71
let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
51
- let context = Context ( requestId: " \( DispatchTime . now ( ) . uptimeNanoseconds) " ,
52
- traceId: " Root= \( DispatchTime . now ( ) . uptimeNanoseconds) ;Parent= \( DispatchTime . now ( ) . uptimeNanoseconds) ;Sampled=1 " ,
53
- invokedFunctionArn: " arn:aws:lambda:us-west-1: \( DispatchTime . now ( ) . uptimeNanoseconds) :function:custom-runtime " ,
54
- deadline: . now( ) + 5 ,
72
+ let eventLoop = eventLoopGroup. next ( )
73
+
74
+ let context = Context ( requestId: config. requestId,
75
+ traceId: config. traceId,
76
+ invokedFunctionArn: config. invokedFunctionArn,
77
+ deadline: . now( ) + config. timeout,
55
78
logger: logger,
56
- eventLoop: eventLoopGroup. next ( ) )
57
- handler. handle ( context: context, payload: payload) . whenComplete { result in
58
- body ( result)
59
- }
79
+ eventLoop: eventLoop)
80
+
81
+ let result = try eventLoop. flatSubmit {
82
+ handler. handle ( context: context, payload: payload)
83
+ } . wait ( )
84
+
85
+ try eventLoopGroup. syncShutdownGracefully ( )
86
+ return result
60
87
}
61
88
}
0 commit comments