diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift index 6f6c248b..7ba88ed1 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift @@ -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 { @@ -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 { @@ -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) } @@ -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 { @@ -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 { @@ -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 { @@ -125,14 +147,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 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 { @@ -140,7 +167,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) @@ -148,7 +178,9 @@ class LambdaHandlerTest: XCTestCase { 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 { @@ -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) } @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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")) } } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift index ea4ebc9d..9379f5ee 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift @@ -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 { diff --git a/Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift b/Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift index 63ed9d9c..14134aad 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift @@ -43,14 +43,18 @@ final class MockLambdaServer { assert(shutdown) } - func start() -> EventLoopFuture { + func start() -> EventLoopFuture { 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 @@ -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!) } } diff --git a/Tests/AWSLambdaRuntimeCoreTests/Utils.swift b/Tests/AWSLambdaRuntimeCoreTests/Utils.swift index 14e15b6c..8bbd4730 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/Utils.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/Utils.swift @@ -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)