Skip to content

Commit 4727ed1

Browse files
committed
fixup
1 parent 5c5ca9a commit 4727ed1

12 files changed

+57
-135
lines changed

Sources/SwiftAwsLambda/Lambda+Codable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class LambdaCodableCodec<In: Decodable, Out: Encodable> {
6464

6565
/// Default implementation of `Encodable` -> `[UInt8]` encoding and `[UInt8]` -> `Decodable' decoding
6666
public extension LambdaCodableHandler {
67-
func handle(context: Lambda.Context, payload: [UInt8], callback: @escaping (LambdaResult) -> Void) {
67+
func handle(context: Lambda.Context, payload: [UInt8], callback: @escaping LambdaCallback) {
6868
switch self.codec.decode(payload) {
6969
case .failure(let error):
7070
return callback(.failure(Errors.requestDecoding(error)))

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ public enum Lambda {
4141
self.run(handler: handler)
4242
}
4343

44-
/// Run a Lambda defined by implementing the `LambdaHandler` protocol.
44+
/// Run a Lambda defined by implementing the `LambdaHandler` protocol via a `LambdaHandlerFactory`.
4545
///
4646
/// - note: This is a blocking operation that will run forever, as it's lifecycle is managed by the AWS Lambda Runtime Engine.
4747
@inlinable
48-
public static func run(_ provider: @escaping LambdaHandlerProvider) {
49-
self.run(provider: provider)
48+
public static func run(_ factory: @escaping LambdaHandlerFactory) {
49+
self.run(factory: factory)
5050
}
5151

52-
/// Run a Lambda defined by implementing the `LambdaHandler` protocol.
52+
/// Run a Lambda defined by implementing the `LambdaHandler` protocol via a factory.
5353
///
5454
/// - note: This is a blocking operation that will run forever, as it's lifecycle is managed by the AWS Lambda Runtime Engine.
5555
@inlinable
56-
public static func run(_ provider: @escaping (EventLoop) throws -> LambdaHandler) {
57-
self.run(provider: provider)
56+
public static func run(_ factory: @escaping (EventLoop) throws -> LambdaHandler) {
57+
self.run(factory: factory)
5858
}
5959

6060
// for testing and internal use
@@ -68,16 +68,16 @@ public enum Lambda {
6868
@usableFromInline
6969
@discardableResult
7070
internal static func run(configuration: Configuration = .init(), handler: LambdaHandler) -> LambdaLifecycleResult {
71-
return self.run(configuration: configuration, provider: { _, callback in callback(.success(handler)) })
71+
return self.run(configuration: configuration, factory: { _, callback in callback(.success(handler)) })
7272
}
7373

7474
// for testing and internal use
7575
@usableFromInline
7676
@discardableResult
77-
internal static func run(configuration: Configuration = .init(), provider: @escaping (EventLoop) throws -> LambdaHandler) -> LambdaLifecycleResult {
78-
self.run(provider: { (eventloop: EventLoop, callback: (Result<LambdaHandler, Error>) -> Void) -> Void in
77+
internal static func run(configuration: Configuration = .init(), factory: @escaping (EventLoop) throws -> LambdaHandler) -> LambdaLifecycleResult {
78+
self.run(factory: { (eventloop: EventLoop, callback: (Result<LambdaHandler, Error>) -> Void) -> Void in
7979
do {
80-
let handler = try provider(eventloop)
80+
let handler = try factory(eventloop)
8181
callback(.success(handler))
8282
} catch {
8383
callback(.failure(error))
@@ -88,22 +88,22 @@ public enum Lambda {
8888
// for testing and internal use
8989
@usableFromInline
9090
@discardableResult
91-
internal static func run(configuration: Configuration = .init(), provider: @escaping LambdaHandlerProvider) -> LambdaLifecycleResult {
91+
internal static func run(configuration: Configuration = .init(), factory: @escaping LambdaHandlerFactory) -> LambdaLifecycleResult {
9292
do {
9393
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) // only need one thread, will improve performance
9494
defer { try! eventLoopGroup.syncShutdownGracefully() }
95-
let result = try self.runAsync(eventLoopGroup: eventLoopGroup, configuration: configuration, provider: provider).wait()
95+
let result = try self.runAsync(eventLoopGroup: eventLoopGroup, configuration: configuration, factory: factory).wait()
9696
return .success(result)
9797
} catch {
9898
return .failure(error)
9999
}
100100
}
101101

102-
internal static func runAsync(eventLoopGroup: EventLoopGroup, configuration: Configuration, provider: @escaping LambdaHandlerProvider) -> EventLoopFuture<Int> {
102+
internal static func runAsync(eventLoopGroup: EventLoopGroup, configuration: Configuration, factory: @escaping LambdaHandlerFactory) -> EventLoopFuture<Int> {
103103
Backtrace.install()
104104
var logger = Logger(label: "Lambda")
105105
logger.logLevel = configuration.general.logLevel
106-
let lifecycle = Lifecycle(eventLoop: eventLoopGroup.next(), logger: logger, configuration: configuration, provider: provider)
106+
let lifecycle = Lifecycle(eventLoop: eventLoopGroup.next(), logger: logger, configuration: configuration, factory: factory)
107107
let signalSource = trap(signal: configuration.lifecycle.stopSignal) { signal in
108108
logger.info("intercepted signal: \(signal)")
109109
lifecycle.stop()
@@ -150,16 +150,16 @@ public enum Lambda {
150150
private let eventLoop: EventLoop
151151
private let logger: Logger
152152
private let configuration: Configuration
153-
private let provider: LambdaHandlerProvider
153+
private let factory: LambdaHandlerFactory
154154

155155
private var _state = State.idle
156156
private let stateLock = Lock()
157157

158-
init(eventLoop: EventLoop, logger: Logger, configuration: Configuration, provider: @escaping LambdaHandlerProvider) {
158+
init(eventLoop: EventLoop, logger: Logger, configuration: Configuration, factory: @escaping LambdaHandlerFactory) {
159159
self.eventLoop = eventLoop
160160
self.logger = logger
161161
self.configuration = configuration
162-
self.provider = provider
162+
self.factory = factory
163163
}
164164

165165
deinit {
@@ -188,7 +188,7 @@ public enum Lambda {
188188
var logger = self.logger
189189
logger[metadataKey: "lifecycleId"] = .string(self.configuration.lifecycle.id)
190190
let runner = LambdaRunner(eventLoop: self.eventLoop, configuration: self.configuration)
191-
return runner.initialize(logger: logger, provider: self.provider).flatMap { handler in
191+
return runner.initialize(logger: logger, factory: self.factory).flatMap { handler in
192192
self.state = .active(runner, handler)
193193
return self.run()
194194
}
@@ -338,23 +338,19 @@ public enum Lambda {
338338
}
339339
}
340340

341-
/// A result type for a Lambda that returns a `[UInt8]`.
342341
public typealias LambdaResult = Result<[UInt8], Error>
343342

344343
public typealias LambdaCallback = (LambdaResult) -> Void
345344

346-
/// A processing closure for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously.
345+
/// A processing closure for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously via`LambdaCallback` .
347346
public typealias LambdaClosure = (Lambda.Context, [UInt8], LambdaCallback) -> Void
348347

349-
/// A result type for a Lambda initialization.
350-
public typealias LambdaInitResult = Result<LambdaHandler, Error>
351-
352348
/// A callback to provide the result of Lambda initialization.
353-
public typealias LambdaInitCallBack = (LambdaInitResult) -> Void
349+
public typealias LambdaInitCallBack = (Result<LambdaHandler, Error>) -> Void
354350

355-
public typealias LambdaHandlerProvider = (EventLoop, LambdaInitCallBack) -> Any
351+
public typealias LambdaHandlerFactory = (EventLoop, LambdaInitCallBack) -> Void
356352

357-
/// A processing protocol for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously.
353+
/// A processing protocol for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously via `LambdaCallback`.
358354
public protocol LambdaHandler {
359355
/// Handles the Lambda request.
360356
func handle(context: Lambda.Context, payload: [UInt8], callback: @escaping LambdaCallback)

Sources/SwiftAwsLambda/LambdaRunner.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ internal struct LambdaRunner {
3333
/// Run the user provided initializer. This *must* only be called once.
3434
///
3535
/// - Returns: An `EventLoopFuture<LambdaHandler>` fulfilled with the outcome of the initialization.
36-
func initialize(logger: Logger, provider: @escaping LambdaHandlerProvider) -> EventLoopFuture<LambdaHandler> {
36+
func initialize(logger: Logger, factory: @escaping LambdaHandlerFactory) -> EventLoopFuture<LambdaHandler> {
3737
logger.debug("initializing lambda")
38-
// 1. craete the handler from the provider
39-
let future = bootstrap(eventLoop: self.eventLoop, lifecycleId: self.lifecycleId, offload: self.offload, provider: provider)
38+
// 1. craete the handler from the factory
39+
let future = bootstrap(eventLoop: self.eventLoop, lifecycleId: self.lifecycleId, offload: self.offload, factory: factory)
4040
// 2. report initialization error if one occured
4141
return future.peekError { error in
4242
self.runtimeClient.reportInitializationError(logger: logger, error: error).peekError { reportingError in
@@ -91,19 +91,15 @@ internal struct LambdaRunner {
9191
}
9292
}
9393

94-
private func bootstrap(eventLoop: EventLoop, lifecycleId: String, offload: Bool, provider: @escaping LambdaHandlerProvider) -> EventLoopFuture<LambdaHandler> {
94+
private func bootstrap(eventLoop: EventLoop, lifecycleId: String, offload: Bool, factory: @escaping LambdaHandlerFactory) -> EventLoopFuture<LambdaHandler> {
9595
let promise = eventLoop.makePromise(of: LambdaHandler.self)
9696
if offload {
9797
// offloading so user code never blocks the eventloop
9898
DispatchQueue(label: "lambda-\(lifecycleId)").async {
99-
_ = provider(eventLoop) { result in
100-
promise.completeWith(result)
101-
}
99+
factory(eventLoop, promise.completeWith)
102100
}
103101
} else {
104-
_ = provider(eventLoop) { result in
105-
promise.completeWith(result)
106-
}
102+
factory(eventLoop, promise.completeWith)
107103
}
108104
return promise.futureResult
109105
}

Tests/SwiftAwsLambdaTests/Lambda+CodeableTest+XCTest.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ extension CodableLambdaTest {
2929
("testFailure", testFailure),
3030
("testClosureSuccess", testClosureSuccess),
3131
("testClosureFailure", testClosureFailure),
32-
("testProviderFailure", testProviderFailure),
33-
("testProviderFailure2", testProviderFailure2),
32+
("testBootstrapFailure", testBootstrapFailure),
3433
]
3534
}
3635
}

Tests/SwiftAwsLambdaTests/Lambda+CodeableTest.swift

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,7 @@ class CodableLambdaTest: XCTestCase {
7777
assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes)
7878
}
7979

80-
func testProviderFailure() {
81-
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
82-
XCTAssertNoThrow(try server.start().wait())
83-
defer { XCTAssertNoThrow(try server.stop().wait()) }
84-
85-
struct Handler: LambdaCodableHandler {
86-
init(eventLoop: EventLoop, callback: (Result<LambdaHandler, Error>) -> Void) {
87-
callback(.failure(TestError("kaboom")))
88-
}
89-
90-
func handle(context: Lambda.Context, payload: Request, callback: @escaping LambdaCodableCallback<Response>) {
91-
callback(.failure(TestError("should not be called")))
92-
}
93-
}
94-
95-
let result = Lambda.run(provider: Handler.init)
96-
assertLambdaLifecycleResult(result, shouldFailWithError: TestError("kaboom"))
97-
}
98-
99-
func testProviderFailure2() {
80+
func testBootstrapFailure() {
10081
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
10182
XCTAssertNoThrow(try server.start().wait())
10283
defer { XCTAssertNoThrow(try server.stop().wait()) }
@@ -111,7 +92,7 @@ class CodableLambdaTest: XCTestCase {
11192
}
11293
}
11394

114-
let result = Lambda.run(provider: Handler.init)
95+
let result = Lambda.run(factory: Handler.init)
11596
assertLambdaLifecycleResult(result, shouldFailWithError: TestError("kaboom"))
11697
}
11798
}

Tests/SwiftAwsLambdaTests/Lambda+StringTest+XCTest.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ extension StringLambdaTest {
2929
("testFailure", testFailure),
3030
("testClosureSuccess", testClosureSuccess),
3131
("testClosureFailure", testClosureFailure),
32-
("testProviderFailure", testProviderFailure),
33-
("testProviderFailure2", testProviderFailure2),
32+
("testBootstrapFailure", testBootstrapFailure),
3433
]
3534
}
3635
}

Tests/SwiftAwsLambdaTests/Lambda+StringTest.swift

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,7 @@ class StringLambdaTest: XCTestCase {
7777
assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes)
7878
}
7979

80-
func testProviderFailure() {
81-
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
82-
XCTAssertNoThrow(try server.start().wait())
83-
defer { XCTAssertNoThrow(try server.stop().wait()) }
84-
85-
struct Handler: LambdaStringHandler {
86-
init(eventLoop: EventLoop, callback: (Result<LambdaHandler, Error>) -> Void) {
87-
callback(.failure(TestError("kaboom")))
88-
}
89-
90-
func handle(context: Lambda.Context, payload: String, callback: @escaping LambdaStringCallback) {
91-
callback(.failure(TestError("should not be called")))
92-
}
93-
}
94-
95-
let result = Lambda.run(provider: Handler.init)
96-
assertLambdaLifecycleResult(result, shouldFailWithError: TestError("kaboom"))
97-
}
98-
99-
func testProviderFailure2() {
80+
func testBootstrapFailure() {
10081
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
10182
XCTAssertNoThrow(try server.start().wait())
10283
defer { XCTAssertNoThrow(try server.stop().wait()) }
@@ -111,7 +92,7 @@ class StringLambdaTest: XCTestCase {
11192
}
11293
}
11394

114-
let result = Lambda.run(provider: Handler.init)
95+
let result = Lambda.run(factory: Handler.init)
11596
assertLambdaLifecycleResult(result, shouldFailWithError: TestError("kaboom"))
11697
}
11798
}

Tests/SwiftAwsLambdaTests/LambdaRuntimeClientTest+XCTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ extension LambdaRuntimeClientTest {
2727
return [
2828
("testSuccess", testSuccess),
2929
("testFailure", testFailure),
30-
("testProviderFailure", testProviderFailure),
30+
("testBootstrapFailure", testBootstrapFailure),
3131
("testGetWorkServerInternalError", testGetWorkServerInternalError),
3232
("testGetWorkServerNoBodyError", testGetWorkServerNoBodyError),
3333
("testGetWorkServerMissingHeaderRequestIDError", testGetWorkServerMissingHeaderRequestIDError),
3434
("testProcessResponseInternalServerError", testProcessResponseInternalServerError),
3535
("testProcessErrorInternalServerError", testProcessErrorInternalServerError),
36-
("testProcessInitErrorOnProviderFailure", testProcessInitErrorOnProviderFailure),
36+
("testProcessInitErrorOnBootstrapFailure", testProcessInitErrorOnBootstrapFailure),
3737
]
3838
}
3939
}

Tests/SwiftAwsLambdaTests/LambdaRuntimeClientTest.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class LambdaRuntimeClientTest: XCTestCase {
2828
XCTAssertEqual(behavior.state, 10)
2929
}
3030

31-
func testProviderFailure() {
31+
func testBootstrapFailure() {
3232
let behavior = Behavior()
33-
XCTAssertThrowsError(try runLambda(behavior: behavior, provider: { _, callback in callback(.failure(TestError("boom"))) })) { error in
33+
XCTAssertThrowsError(try runLambda(behavior: behavior, factory: { _, callback in callback(.failure(TestError("boom"))) })) { error in
3434
XCTAssertEqual(error as? TestError, TestError("boom"))
3535
}
3636
XCTAssertEqual(behavior.state, 1)
@@ -165,7 +165,7 @@ class LambdaRuntimeClientTest: XCTestCase {
165165
}
166166
}
167167

168-
func testProcessInitErrorOnProviderFailure() {
168+
func testProcessInitErrorOnBootstrapFailure() {
169169
struct Behavior: LambdaServerBehavior {
170170
func getWork() -> GetWorkResult {
171171
XCTFail("should not get work")
@@ -186,7 +186,7 @@ class LambdaRuntimeClientTest: XCTestCase {
186186
return .failure(.internalServerError)
187187
}
188188
}
189-
XCTAssertThrowsError(try runLambda(behavior: Behavior(), provider: { _, callback in callback(.failure(TestError("boom"))) })) { error in
189+
XCTAssertThrowsError(try runLambda(behavior: Behavior(), factory: { _, callback in callback(.failure(TestError("boom"))) })) { error in
190190
XCTAssertEqual(error as? TestError, TestError("boom"))
191191
}
192192
}

Tests/SwiftAwsLambdaTests/LambdaTest+XCTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ extension LambdaTest {
2828
("testSuccess", testSuccess),
2929
("testFailure", testFailure),
3030
("testInitializedOnce", testInitializedOnce),
31-
("testProviderFailure", testProviderFailure),
32-
("testProviderFailure2", testProviderFailure2),
33-
("testProviderFailureAndReportErrorFailure", testProviderFailureAndReportErrorFailure),
31+
("testBootstrapFailure", testBootstrapFailure),
32+
("testBootstrapFailure2", testBootstrapFailure2),
33+
("testBootstrapFailureAndReportErrorFailure", testBootstrapFailureAndReportErrorFailure),
3434
("testStartStop", testStartStop),
3535
("testTimeout", testTimeout),
3636
("testDisconnect", testDisconnect),

0 commit comments

Comments
 (0)