Skip to content

Rename RequestWork to GetNextInvocation #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Sources/AWSLambdaRuntime/LambdaRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ extension Lambda {

func run(logger: Logger, handler: Handler) -> EventLoopFuture<Void> {
logger.debug("lambda invocation sequence starting")
// 1. request work from lambda runtime engine
return self.runtimeClient.requestWork(logger: logger).peekError { error in
logger.error("could not fetch work from lambda runtime engine: \(error)")
// 1. request invocation from lambda runtime engine
return self.runtimeClient.getNextInvocation(logger: logger).peekError { error in
logger.error("could not fetch invocation from lambda runtime engine: \(error)")
}.flatMap { invocation, payload in
// 2. send work to handler
// 2. send invocation to handler
let context = Context(logger: logger, eventLoop: self.eventLoop, invocation: invocation)
logger.debug("sending work to lambda handler \(handler)")
logger.debug("sending invocation to lambda handler \(handler)")
return handler.handle(context: context, payload: payload)
.mapResult { result in
if case .failure(let error) = result {
Expand Down
6 changes: 3 additions & 3 deletions Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ extension Lambda {
self.httpClient = HTTPClient(eventLoop: eventLoop, configuration: configuration)
}

/// Requests work from the Runtime Engine.
func requestWork(logger: Logger) -> EventLoopFuture<(Invocation, ByteBuffer)> {
let url = Consts.invocationURLPrefix + Consts.requestWorkURLSuffix
/// Requests invocation from the control plane.
func getNextInvocation(logger: Logger) -> EventLoopFuture<(Invocation, ByteBuffer)> {
let url = Consts.invocationURLPrefix + Consts.getNextInvocationURLSuffix
logger.debug("requesting work from lambda runtime engine using \(url)")
return self.httpClient.get(url: url).flatMapThrowing { response in
guard response.status == .ok else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntime/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import NIO
internal enum Consts {
private static let apiPrefix = "/2018-06-01"
static let invocationURLPrefix = "\(apiPrefix)/runtime/invocation"
static let requestWorkURLSuffix = "/next"
static let getNextInvocationURLSuffix = "/next"
static let postResponseURLSuffix = "/response"
static let postErrorURLSuffix = "/error"
static let postInitErrorURL = "\(apiPrefix)/runtime/init/error"
Expand Down
2 changes: 1 addition & 1 deletion Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private struct Behavior: LambdaServerBehavior {
self.result = result
}

func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
guard let payload = try? JSONEncoder().encode(Request(requestId: requestId)) else {
XCTFail("encoding error")
return .failure(.internalServerError)
Expand Down
2 changes: 1 addition & 1 deletion Tests/AWSLambdaRuntimeTests/Lambda+StringTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private struct Behavior: LambdaServerBehavior {
self.result = result
}

func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success((requestId: self.requestId, payload: self.payload))
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/AWSLambdaRuntimeTests/LambdaRunnerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LambdaRunnerTest: XCTestCase {
struct Behavior: LambdaServerBehavior {
let requestId = UUID().uuidString
let payload = "hello"
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success((self.requestId, self.payload))
}

Expand All @@ -47,7 +47,7 @@ class LambdaRunnerTest: XCTestCase {
struct Behavior: LambdaServerBehavior {
static let error = "boom"
let requestId = UUID().uuidString
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success((requestId: self.requestId, payload: "hello"))
}

Expand Down
22 changes: 11 additions & 11 deletions Tests/AWSLambdaRuntimeTests/LambdaRuntimeClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class LambdaRuntimeClientTest: XCTestCase {
XCTAssertEqual(behavior.state, 1)
}

func testGetWorkServerInternalError() {
func testGetInvocationServerInternalError() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.failure(.internalServerError)
}

Expand All @@ -62,9 +62,9 @@ class LambdaRuntimeClientTest: XCTestCase {
}
}

func testGetWorkServerNoBodyError() {
func testGetInvocationServerNoBodyError() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success(("1", ""))
}

Expand All @@ -88,9 +88,9 @@ class LambdaRuntimeClientTest: XCTestCase {
}
}

func testGetWorkServerMissingHeaderRequestIDError() {
func testGetInvocationServerMissingHeaderRequestIDError() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
// no request id -> no context
.success(("", "hello"))
}
Expand All @@ -117,7 +117,7 @@ class LambdaRuntimeClientTest: XCTestCase {

func testProcessResponseInternalServerError() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success((requestId: "1", payload: "payload"))
}

Expand All @@ -142,7 +142,7 @@ class LambdaRuntimeClientTest: XCTestCase {

func testProcessErrorInternalServerError() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success((requestId: "1", payload: "payload"))
}

Expand All @@ -167,8 +167,8 @@ class LambdaRuntimeClientTest: XCTestCase {

func testProcessInitErrorOnBootstrapFailure() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
XCTFail("should not get work")
func getInvocation() -> GetInvocationResult {
XCTFail("should not get invocation")
return .failure(.internalServerError)
}

Expand Down Expand Up @@ -217,7 +217,7 @@ class LambdaRuntimeClientTest: XCTestCase {
return .success(())
}

func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
self.state += 2
return .success(("1", "hello"))
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/AWSLambdaRuntimeTests/LambdaTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class LambdaTest: XCTestCase {

func testBootstrapFailureAndReportErrorFailure() {
struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
XCTFail("should not get work")
func getInvocation() -> GetInvocationResult {
XCTFail("should not get invocation")
return .failure(.internalServerError)
}

Expand Down Expand Up @@ -212,7 +212,7 @@ class LambdaTest: XCTestCase {
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Behavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.failure(.internalServerError)
}

Expand Down Expand Up @@ -299,7 +299,7 @@ private struct Behavior: LambdaServerBehavior {
self.result = result
}

func getWork() -> GetWorkResult {
func getInvocation() -> GetInvocationResult {
.success((requestId: self.requestId, payload: self.payload))
}

Expand Down Expand Up @@ -334,8 +334,8 @@ private struct Behavior: LambdaServerBehavior {
}

struct FailedBootstrapBehavior: LambdaServerBehavior {
func getWork() -> GetWorkResult {
XCTFail("should not get work")
func getInvocation() -> GetInvocationResult {
XCTFail("should not get invocation")
return .failure(.internalServerError)
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/AWSLambdaRuntimeTests/MockLambdaServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ internal final class HTTPHandler: ChannelInboundHandler {
case .failure(let error):
responseStatus = .init(statusCode: error.rawValue)
}
} else if request.head.uri.hasSuffix(Consts.requestWorkURLSuffix) {
switch self.behavior.getWork() {
} else if request.head.uri.hasSuffix(Consts.getNextInvocationURLSuffix) {
switch self.behavior.getInvocation() {
case .success(let (requestId, result)):
if requestId == "timeout" {
usleep((UInt32(result) ?? 0) * 1000)
Expand Down Expand Up @@ -213,13 +213,13 @@ internal final class HTTPHandler: ChannelInboundHandler {
}

internal protocol LambdaServerBehavior {
func getWork() -> GetWorkResult
func getInvocation() -> GetInvocationResult
func processResponse(requestId: String, response: String?) -> Result<Void, ProcessResponseError>
func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError>
func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError>
}

internal typealias GetWorkResult = Result<(String, String), GetWorkError>
internal typealias GetInvocationResult = Result<(String, String), GetWorkError>

internal enum GetWorkError: Int, Error {
case badRequest = 400
Expand Down