Skip to content

Don't use hardcoded port for MockServer #350

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 2 commits into from
Aug 30, 2024
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
97 changes: 75 additions & 22 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class LambdaHandlerTest: XCTestCase {

func testBootstrapSimpleNoInit() {
let server = MockLambdaServer(behavior: Behavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct TestBootstrapHandler: SimpleLambdaHandler {
Expand All @@ -32,14 +34,19 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 10...20)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testBootstrapSimpleInit() {
let server = MockLambdaServer(behavior: Behavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct TestBootstrapHandler: SimpleLambdaHandler {
Expand All @@ -56,7 +63,10 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 10...20)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}
Expand All @@ -65,7 +75,9 @@ class LambdaHandlerTest: XCTestCase {

func testBootstrapSuccess() {
let server = MockLambdaServer(behavior: Behavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct TestBootstrapHandler: LambdaHandler {
Expand All @@ -83,14 +95,19 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 10...20)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testBootstrapFailure() {
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct TestBootstrapHandler: LambdaHandler {
Expand All @@ -108,14 +125,19 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 10...20)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
assertLambdaRuntimeResult(result, shouldFailWithError: TestError("kaboom"))
}

func testHandlerSuccess() {
let server = MockLambdaServer(behavior: Behavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: SimpleLambdaHandler {
Expand All @@ -125,30 +147,40 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 1...10)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testVoidHandlerSuccess() {
let server = MockLambdaServer(behavior: Behavior(result: .success(nil)))
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: SimpleLambdaHandler {
func handle(_ event: String, context: LambdaContext) async throws {}
}

let maxTimes = Int.random(in: 1...10)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)

let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testHandlerFailure() {
let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom"))))
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: SimpleLambdaHandler {
Expand All @@ -158,7 +190,10 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 1...10)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}
Expand All @@ -167,7 +202,9 @@ class LambdaHandlerTest: XCTestCase {

func testEventLoopSuccess() {
let server = MockLambdaServer(behavior: Behavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: EventLoopLambdaHandler {
Expand All @@ -181,14 +218,19 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 1...10)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testVoidEventLoopSuccess() {
let server = MockLambdaServer(behavior: Behavior(result: .success(nil)))
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: EventLoopLambdaHandler {
Expand All @@ -202,14 +244,19 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 1...10)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testEventLoopFailure() {
let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom"))))
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: EventLoopLambdaHandler {
Expand All @@ -223,14 +270,19 @@ class LambdaHandlerTest: XCTestCase {
}

let maxTimes = Int.random(in: 1...10)
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
let configuration = LambdaConfiguration(
lifecycle: .init(maxTimes: maxTimes),
runtimeEngine: .init(address: "127.0.0.1:\(port)")
)
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
}

func testEventLoopBootstrapFailure() {
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
XCTAssertNoThrow(try server.start().wait())
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

struct Handler: EventLoopLambdaHandler {
Expand All @@ -244,7 +296,8 @@ class LambdaHandlerTest: XCTestCase {
}
}

let result = Lambda.run(configuration: .init(), handlerType: Handler.self)
let configuration = LambdaConfiguration(runtimeEngine: .init(address: "127.0.0.1:\(port)"))
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
assertLambdaRuntimeResult(result, shouldFailWithError: TestError("kaboom"))
}
}
Expand Down
9 changes: 7 additions & 2 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,16 @@ class LambdaTest: XCTestCase {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }

let server = try await MockLambdaServer(behavior: Behavior()).start().get()
let server = MockLambdaServer(behavior: Behavior(), port: 0)
var port: Int?
XCTAssertNoThrow(port = try server.start().wait())
guard let port else { return XCTFail("Expected the server to have started") }
defer { XCTAssertNoThrow(try server.stop().wait()) }

let logger = Logger(label: "TestLogger")
let configuration = LambdaConfiguration(runtimeEngine: .init(requestTimeout: .milliseconds(100)))
let configuration = LambdaConfiguration(
runtimeEngine: .init(address: "127.0.0.1:\(port)", requestTimeout: .milliseconds(100))
)

let handler1 = Handler()
let task = Task.detached {
Expand Down
12 changes: 8 additions & 4 deletions Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ final class MockLambdaServer {
assert(shutdown)
}

func start() -> EventLoopFuture<MockLambdaServer> {
func start() -> EventLoopFuture<Int> {
let bootstrap = ServerBootstrap(group: group)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.childChannelInitializer { channel in
channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).flatMap { _ in
channel.pipeline.addHandler(
do {
try channel.pipeline.syncOperations.configureHTTPServerPipeline(withErrorHandling: true)
try channel.pipeline.syncOperations.addHandler(
HTTPHandler(logger: self.logger, keepAlive: self.keepAlive, behavior: self.behavior)
)
return channel.eventLoop.makeSucceededVoidFuture()
} catch {
return channel.eventLoop.makeFailedFuture(error)
}
}
return bootstrap.bind(host: self.host, port: self.port).flatMap { channel in
Expand All @@ -59,7 +63,7 @@ final class MockLambdaServer {
return channel.eventLoop.makeFailedFuture(ServerError.cantBind)
}
self.logger.info("\(self) started and listening on \(localAddress)")
return channel.eventLoop.makeSucceededFuture(self)
return channel.eventLoop.makeSucceededFuture(localAddress.port!)
}
}

Expand Down
7 changes: 5 additions & 2 deletions Tests/AWSLambdaRuntimeCoreTests/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ func runLambda(
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let logger = Logger(label: "TestLogger")
let configuration = LambdaConfiguration(runtimeEngine: .init(requestTimeout: .milliseconds(100)))
let server = MockLambdaServer(behavior: behavior, port: 0)
let port = try server.start().wait()
let configuration = LambdaConfiguration(
runtimeEngine: .init(address: "127.0.0.1:\(port)", requestTimeout: .milliseconds(100))
)
let terminator = LambdaTerminator()
let runner = LambdaRunner(eventLoop: eventLoopGroup.next(), configuration: configuration)
let server = try MockLambdaServer(behavior: behavior).start().wait()
defer { XCTAssertNoThrow(try server.stop().wait()) }
try runner.initialize(handlerProvider: handlerProvider, logger: logger, terminator: terminator).flatMap { handler in
runner.run(handler: handler, logger: logger)
Expand Down
Loading