File tree 3 files changed +26
-9
lines changed
Sources/AWSLambdaRuntimeCore
Tests/AWSLambdaRuntimeCoreTests
3 files changed +26
-9
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,12 @@ extension Lambda {
27
27
private let configuration : Configuration
28
28
private let factory : HandlerFactory
29
29
30
- private var state = State . idle
30
+ private var state = State . idle {
31
+ willSet {
32
+ assert ( self . eventLoop. inEventLoop, " State may only be changed on the `Lifecycle`'s `eventLoop` " )
33
+ precondition ( newValue. order > self . state. order, " invalid state \( newValue) after \( self . state. order) " )
34
+ }
35
+ }
31
36
32
37
/// Create a new `Lifecycle`.
33
38
///
@@ -62,8 +67,12 @@ extension Lambda {
62
67
63
68
/// Start the `Lifecycle`.
64
69
///
65
- /// - Returns: An `EventLoopFuture` that is fulfilled after the Lambda hander has been created and initiliazed, and a first run has been schduled.
70
+ /// - Returns: An `EventLoopFuture` that is fulfilled after the Lambda hander has been created and initiliazed, and a first run has been scheduled.
71
+ ///
72
+ /// - note: This method must be called on the `EventLoop` the `Lifecycle` has been initialized with.
66
73
public func start( ) -> EventLoopFuture < Void > {
74
+ assert ( self . eventLoop. inEventLoop, " Start must be called on the `EventLoop` the `Lifecycle` has been initialized with. " )
75
+
67
76
logger. info ( " lambda lifecycle starting with \( self . configuration) " )
68
77
self . state = . initializing
69
78
// triggered when the Lambda has finished its last run
Original file line number Diff line number Diff line change @@ -77,8 +77,9 @@ extension Lambda {
77
77
/// cancels the current run, if we are waiting for next invocation (long poll from Lambda control plane)
78
78
/// only needed for debugging purposes.
79
79
func cancelWaitingForNextInvocation( ) {
80
- guard self . isGettingNextInvocation else { return }
81
- self . runtimeClient. cancel ( )
80
+ if self . isGettingNextInvocation {
81
+ self . runtimeClient. cancel ( )
82
+ }
82
83
}
83
84
}
84
85
}
Original file line number Diff line number Diff line change @@ -145,12 +145,19 @@ class LambdaTest: XCTestCase {
145
145
}
146
146
let result = Lambda . run ( configuration: configuration, factory: { $0. makeSucceededFuture ( EchoHandler ( ) ) } )
147
147
148
- guard case . success( let invocationCount) = result else {
149
- return XCTFail ( " expected to have not failed " )
148
+ switch result {
149
+ case . success( let invocationCount) :
150
+ // this is an allowed outcome. the lambda was cancelled while it was processing a task.
151
+ // therefore: it was shut down before asking for a new invocation
152
+ XCTAssertGreaterThan ( invocationCount, 0 , " should have stopped before any request made " )
153
+ XCTAssertLessThan ( invocationCount, maxTimes, " should have stopped before \( maxTimes) " )
154
+ case . failure( HTTPClient . Errors. cancelled) :
155
+ // this is an allowed outcome. while the lambda was asking for a new invocation it was
156
+ // cancelled. for this reason the get new invocation request failed with .cancelled.
157
+ break
158
+ case . failure( let error) :
159
+ XCTFail ( " Unexpected error: \( error) " )
150
160
}
151
-
152
- XCTAssertGreaterThan ( invocationCount, 0 , " should have stopped before any request made " )
153
- XCTAssertLessThan ( invocationCount, maxTimes, " should have stopped before \( maxTimes) " )
154
161
}
155
162
156
163
func testTimeout( ) {
You can’t perform that action at this time.
0 commit comments