Skip to content

Commit 779f848

Browse files
committed
Readded state check for Lifecycle. Assert start happens in correct loop.
1 parent 18589b7 commit 779f848

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Sources/AWSLambdaRuntimeCore/LambdaLifecycle.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ extension Lambda {
2727
private let configuration: Configuration
2828
private let factory: HandlerFactory
2929

30-
private var state = State.idle
30+
private var state = State.idle {
31+
willSet {
32+
precondition(newValue.order > state.order, "invalid state \(newValue) after \(state.order)")
33+
}
34+
}
3135

3236
/// Create a new `Lifecycle`.
3337
///
@@ -62,8 +66,12 @@ extension Lambda {
6266

6367
/// Start the `Lifecycle`.
6468
///
65-
/// - Returns: An `EventLoopFuture` that is fulfilled after the Lambda hander has been created and initiliazed, and a first run has been schduled.
69+
/// - Returns: An `EventLoopFuture` that is fulfilled after the Lambda hander has been created and initiliazed, and a first run has been scheduled.
70+
///
71+
/// - note: This method must be called on the `EventLoop` the `Lifecycle` has been initialized with.
6672
public func start() -> EventLoopFuture<Void> {
73+
assert(self.eventLoop.inEventLoop, "Start must be called on the `EventLoop` the `Lifecycle` has been initialized with.")
74+
6775
logger.info("lambda lifecycle starting with \(self.configuration)")
6876
self.state = .initializing
6977
// triggered when the Lambda has finished its last run

Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift

+12-5
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,19 @@ class LambdaTest: XCTestCase {
145145
}
146146
let result = Lambda.run(configuration: configuration, factory: { $0.makeSucceededFuture(EchoHandler()) })
147147

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)")
150160
}
151-
152-
XCTAssertGreaterThan(invocationCount, 0, "should have stopped before any request made")
153-
XCTAssertLessThan(invocationCount, maxTimes, "should have stopped before \(maxTimes)")
154161
}
155162

156163
func testTimeout() {

0 commit comments

Comments
 (0)