|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +@testable import AWSLambdaRuntimeCore |
| 16 | +import Logging |
| 17 | +import NIO |
| 18 | +import NIOHTTP1 |
| 19 | +import XCTest |
| 20 | + |
| 21 | +class LambdaLifecycleTest: XCTestCase { |
| 22 | + func testShutdownFutureIsFulfilledWithStartUpError() { |
| 23 | + let server = MockLambdaServer(behavior: FailedBootstrapBehavior()) |
| 24 | + XCTAssertNoThrow(try server.start().wait()) |
| 25 | + defer { XCTAssertNoThrow(try server.stop().wait()) } |
| 26 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 27 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 28 | + |
| 29 | + let eventLoop = eventLoopGroup.next() |
| 30 | + let logger = Logger(label: "TestLogger") |
| 31 | + let testError = TestError("kaboom") |
| 32 | + let lifecycle = Lambda.Lifecycle(eventLoop: eventLoop, logger: logger, factory: { |
| 33 | + $0.eventLoop.makeFailedFuture(testError) |
| 34 | + }) |
| 35 | + |
| 36 | + // eventLoop.submit in this case returns an EventLoopFuture<EventLoopFuture<ByteBufferHandler>> |
| 37 | + // which is why we need `wait().wait()` |
| 38 | + XCTAssertThrowsError(_ = try eventLoop.flatSubmit { lifecycle.start() }.wait()) { error in |
| 39 | + XCTAssertEqual(testError, error as? TestError) |
| 40 | + } |
| 41 | + |
| 42 | + XCTAssertThrowsError(_ = try lifecycle.shutdownFuture.wait()) { error in |
| 43 | + XCTAssertEqual(testError, error as? TestError) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + struct CallbackLambdaHandler: ByteBufferLambdaHandler { |
| 48 | + let handler: (Lambda.Context, ByteBuffer) -> (EventLoopFuture<ByteBuffer?>) |
| 49 | + let shutdown: (Lambda.ShutdownContext) -> EventLoopFuture<Void> |
| 50 | + |
| 51 | + init(_ handler: @escaping (Lambda.Context, ByteBuffer) -> (EventLoopFuture<ByteBuffer?>), shutdown: @escaping (Lambda.ShutdownContext) -> EventLoopFuture<Void>) { |
| 52 | + self.handler = handler |
| 53 | + self.shutdown = shutdown |
| 54 | + } |
| 55 | + |
| 56 | + func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> { |
| 57 | + self.handler(context, event) |
| 58 | + } |
| 59 | + |
| 60 | + func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture<Void> { |
| 61 | + self.shutdown(context) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + func testShutdownIsCalledWhenLambdaShutsdown() { |
| 66 | + let server = MockLambdaServer(behavior: BadBehavior()) |
| 67 | + XCTAssertNoThrow(try server.start().wait()) |
| 68 | + defer { XCTAssertNoThrow(try server.stop().wait()) } |
| 69 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 70 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 71 | + |
| 72 | + var count = 0 |
| 73 | + let handler = CallbackLambdaHandler({ XCTFail("Should not be reached"); return $0.eventLoop.makeSucceededFuture($1) }) { context in |
| 74 | + count += 1 |
| 75 | + return context.eventLoop.makeSucceededFuture(Void()) |
| 76 | + } |
| 77 | + |
| 78 | + let eventLoop = eventLoopGroup.next() |
| 79 | + let logger = Logger(label: "TestLogger") |
| 80 | + let lifecycle = Lambda.Lifecycle(eventLoop: eventLoop, logger: logger, factory: { |
| 81 | + $0.eventLoop.makeSucceededFuture(handler) |
| 82 | + }) |
| 83 | + |
| 84 | + XCTAssertNoThrow(_ = try eventLoop.flatSubmit { lifecycle.start() }.wait()) |
| 85 | + XCTAssertThrowsError(_ = try lifecycle.shutdownFuture.wait()) { error in |
| 86 | + XCTAssertEqual(.badStatusCode(HTTPResponseStatus.internalServerError), error as? Lambda.RuntimeError) |
| 87 | + } |
| 88 | + XCTAssertEqual(count, 1) |
| 89 | + } |
| 90 | + |
| 91 | + func testLambdaResultIfShutsdownIsUnclean() { |
| 92 | + let server = MockLambdaServer(behavior: BadBehavior()) |
| 93 | + XCTAssertNoThrow(try server.start().wait()) |
| 94 | + defer { XCTAssertNoThrow(try server.stop().wait()) } |
| 95 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 96 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 97 | + |
| 98 | + var count = 0 |
| 99 | + let handler = CallbackLambdaHandler({ XCTFail("Should not be reached"); return $0.eventLoop.makeSucceededFuture($1) }) { context in |
| 100 | + count += 1 |
| 101 | + return context.eventLoop.makeFailedFuture(TestError("kaboom")) |
| 102 | + } |
| 103 | + |
| 104 | + let eventLoop = eventLoopGroup.next() |
| 105 | + let logger = Logger(label: "TestLogger") |
| 106 | + let lifecycle = Lambda.Lifecycle(eventLoop: eventLoop, logger: logger, factory: { |
| 107 | + $0.eventLoop.makeSucceededFuture(handler) |
| 108 | + }) |
| 109 | + |
| 110 | + XCTAssertNoThrow(_ = try eventLoop.flatSubmit { lifecycle.start() }.wait()) |
| 111 | + XCTAssertThrowsError(_ = try lifecycle.shutdownFuture.wait()) { error in |
| 112 | + guard case Lambda.RuntimeError.shutdownError(let shutdownError, .failure(let runtimeError)) = error else { |
| 113 | + XCTFail("Unexpected error"); return |
| 114 | + } |
| 115 | + |
| 116 | + XCTAssertEqual(shutdownError as? TestError, TestError("kaboom")) |
| 117 | + XCTAssertEqual(runtimeError as? Lambda.RuntimeError, .badStatusCode(.internalServerError)) |
| 118 | + } |
| 119 | + XCTAssertEqual(count, 1) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +struct BadBehavior: LambdaServerBehavior { |
| 124 | + func getInvocation() -> GetInvocationResult { |
| 125 | + .failure(.internalServerError) |
| 126 | + } |
| 127 | + |
| 128 | + func processResponse(requestId: String, response: String?) -> Result<Void, ProcessResponseError> { |
| 129 | + XCTFail("should not report a response") |
| 130 | + return .failure(.internalServerError) |
| 131 | + } |
| 132 | + |
| 133 | + func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError> { |
| 134 | + XCTFail("should not report an error") |
| 135 | + return .failure(.internalServerError) |
| 136 | + } |
| 137 | + |
| 138 | + func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError> { |
| 139 | + XCTFail("should not report an error") |
| 140 | + return .failure(.internalServerError) |
| 141 | + } |
| 142 | +} |
0 commit comments