-
Notifications
You must be signed in to change notification settings - Fork 113
Improve testing utils #60
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
519688e
add a testing harness to ease testing of lambdas
tomerd 57b4274
Some improvements
fabianfett 67c9d72
Fixed syntax. Now `using config`
fabianfett 4c85713
final fixes
fabianfett ebc17ee
Update Tests/AWSLambdaTestingTests/Tests.swift
fabianfett 27428fd
Update Tests/AWSLambdaTestingTests/Tests.swift
fabianfett bd035f7
Update Tests/AWSLambdaTestingTests/Tests.swift
fabianfett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// @testable for access of internal functions - this would only work for testing by design | ||
@testable import AWSLambdaRuntime | ||
import Dispatch | ||
import Logging | ||
import NIO | ||
|
||
extension Lambda { | ||
public struct TestConfig { | ||
public var requestId: String | ||
public var traceId: String | ||
public var invokedFunctionArn: String | ||
public var timeout: DispatchTimeInterval | ||
|
||
public init(requestId: String = "\(DispatchTime.now().uptimeNanoseconds)", | ||
traceId: String = "Root=\(DispatchTime.now().uptimeNanoseconds);Parent=\(DispatchTime.now().uptimeNanoseconds);Sampled=1", | ||
invokedFunctionArn: String = "arn:aws:lambda:us-west-1:\(DispatchTime.now().uptimeNanoseconds):function:custom-runtime", | ||
timeout: DispatchTimeInterval = .seconds(5)) { | ||
self.requestId = requestId | ||
self.traceId = traceId | ||
self.invokedFunctionArn = invokedFunctionArn | ||
self.timeout = timeout | ||
} | ||
} | ||
|
||
public static func test(_ closure: @escaping StringLambdaClosure, | ||
with payload: String, | ||
using config: TestConfig = .init()) throws -> String { | ||
try Self.test(StringLambdaClosureWrapper(closure), with: payload, using: config) | ||
} | ||
|
||
public static func test(_ closure: @escaping StringVoidLambdaClosure, | ||
with payload: String, | ||
using config: TestConfig = .init()) throws { | ||
_ = try Self.test(StringVoidLambdaClosureWrapper(closure), with: payload, using: config) | ||
} | ||
|
||
public static func test<In: Decodable, Out: Encodable>( | ||
_ closure: @escaping CodableLambdaClosure<In, Out>, | ||
with payload: In, | ||
using config: TestConfig = .init() | ||
) throws -> Out { | ||
try Self.test(CodableLambdaClosureWrapper(closure), with: payload, using: config) | ||
} | ||
|
||
public static func test<In: Decodable>( | ||
_ closure: @escaping CodableVoidLambdaClosure<In>, | ||
with payload: In, | ||
using config: TestConfig = .init() | ||
) throws { | ||
_ = try Self.test(CodableVoidLambdaClosureWrapper(closure), with: payload, using: config) | ||
} | ||
|
||
public static func test<In, Out, Handler: EventLoopLambdaHandler>( | ||
_ handler: Handler, | ||
with payload: In, | ||
using config: TestConfig = .init() | ||
) throws -> Out where Handler.In == In, Handler.Out == Out { | ||
let logger = Logger(label: "test") | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
defer { | ||
try! eventLoopGroup.syncShutdownGracefully() | ||
} | ||
let eventLoop = eventLoopGroup.next() | ||
let context = Context(requestId: config.requestId, | ||
traceId: config.traceId, | ||
invokedFunctionArn: config.invokedFunctionArn, | ||
deadline: .now() + config.timeout, | ||
logger: logger, | ||
eventLoop: eventLoop) | ||
|
||
return try eventLoop.flatSubmit { | ||
handler.handle(context: context, payload: payload) | ||
}.wait() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import AWSLambdaRuntime | ||
import AWSLambdaTesting | ||
import NIO | ||
import XCTest | ||
|
||
class LambdaTestingTests: XCTestCase { | ||
func testCodableClosure() { | ||
struct Request: Codable { | ||
let name: String | ||
} | ||
|
||
struct Response: Codable { | ||
let message: String | ||
} | ||
|
||
let myLambda = { (_: Lambda.Context, request: Request, callback: (Result<Response, Error>) -> Void) in | ||
callback(.success(Response(message: "echo" + request.name))) | ||
} | ||
|
||
let request = Request(name: UUID().uuidString) | ||
var response: Response? | ||
XCTAssertNoThrow(response = try Lambda.test(myLambda, with: request)) | ||
XCTAssertEqual(response?.message, "echo" + request.name) | ||
} | ||
|
||
func testCodableVoidClosure() { | ||
struct Request: Codable { | ||
let name: String | ||
} | ||
|
||
let myLambda = { (_: Lambda.Context, _: Request, callback: (Result<Void, Error>) -> Void) in | ||
callback(.success(())) | ||
} | ||
|
||
let request = Request(name: UUID().uuidString) | ||
XCTAssertNoThrow(try Lambda.test(myLambda, with: request)) | ||
} | ||
|
||
func testLambdaHandler() { | ||
struct Request: Codable { | ||
let name: String | ||
} | ||
|
||
struct Response: Codable { | ||
let message: String | ||
} | ||
|
||
struct MyLambda: LambdaHandler { | ||
typealias In = Request | ||
typealias Out = Response | ||
|
||
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) { | ||
XCTAssertFalse(context.eventLoop.inEventLoop) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
callback(.success(Response(message: "echo" + payload.name))) | ||
} | ||
} | ||
|
||
let request = Request(name: UUID().uuidString) | ||
var response: Response? | ||
XCTAssertNoThrow(response = try Lambda.test(MyLambda(), with: request)) | ||
XCTAssertEqual(response?.message, "echo" + request.name) | ||
} | ||
|
||
func testEventLoopLambdaHandler() { | ||
struct MyLambda: EventLoopLambdaHandler { | ||
typealias In = String | ||
typealias Out = String | ||
|
||
func handle(context: Lambda.Context, payload: String) -> EventLoopFuture<String> { | ||
XCTAssertTrue(context.eventLoop.inEventLoop) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return context.eventLoop.makeSucceededFuture("echo" + payload) | ||
} | ||
} | ||
|
||
let input = UUID().uuidString | ||
var result: String? | ||
XCTAssertNoThrow(result = try Lambda.test(MyLambda(), with: input)) | ||
XCTAssertEqual(result, "echo" + input) | ||
} | ||
|
||
func testFailure() { | ||
struct MyError: Error {} | ||
|
||
struct MyLambda: LambdaHandler { | ||
typealias In = String | ||
typealias Out = Void | ||
|
||
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) { | ||
callback(.failure(MyError())) | ||
} | ||
} | ||
|
||
XCTAssertThrowsError(try Lambda.test(MyLambda(), with: UUID().uuidString)) { error in | ||
XCTAssert(error is MyError) | ||
} | ||
} | ||
|
||
func testAsyncLongRunning() { | ||
var executed: Bool = false | ||
let myLambda = { (_: Lambda.Context, _: String, callback: @escaping (Result<Void, Error>) -> Void) in | ||
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.5) { | ||
executed = true | ||
callback(.success(())) | ||
} | ||
} | ||
|
||
XCTAssertNoThrow(try Lambda.test(myLambda, with: UUID().uuidString)) | ||
XCTAssertTrue(executed) | ||
} | ||
|
||
func testConfigValues() { | ||
let timeout: TimeInterval = 4 | ||
let config = Lambda.TestConfig( | ||
requestId: UUID().uuidString, | ||
traceId: UUID().uuidString, | ||
invokedFunctionArn: "arn:\(UUID().uuidString)", | ||
timeout: .seconds(4) | ||
) | ||
|
||
let myLambda = { (ctx: Lambda.Context, _: String, callback: @escaping (Result<Void, Error>) -> Void) in | ||
XCTAssertEqual(ctx.requestId, config.requestId) | ||
XCTAssertEqual(ctx.traceId, config.traceId) | ||
XCTAssertEqual(ctx.invokedFunctionArn, config.invokedFunctionArn) | ||
|
||
let secondsSinceEpoch = Double(Int64(bitPattern: ctx.deadline.rawValue)) / -1_000_000_000 | ||
XCTAssertEqual(Date(timeIntervalSince1970: secondsSinceEpoch).timeIntervalSinceNow, timeout, accuracy: 0.1) | ||
|
||
callback(.success(())) | ||
} | ||
|
||
XCTAssertNoThrow(try Lambda.test(myLambda, with: UUID().uuidString, using: config)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.