swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.13k stars 102 forks source link

How to gracefully shutdown the local server? #188

Closed saltzmanjoelh closed 3 years ago

saltzmanjoelh commented 3 years ago

I'm firing up the local server in some tests. Is there a way to gracefully shut it down between tests or at the end of the XCTestCase?

I read that it can handle a TERM signal but I'm not sure how to send it directly to the local server and instead of the XCTest that is running.

tomerd commented 3 years ago

I'm firing up the local server in some tests.

how are you doing that?

note there is also AWSLambdaTesting module designed to help with unit tests without a full server

saltzmanjoelh commented 3 years ago

Sorry, I should have described that. I'm firing up the local lambda server.

As I'm describing my setup, I'm realizing that I should be breaking my code up a bit more to test the specific pieces of code. Thanks for the AWSLambdaTesting reference. I will take a look.

public static func main() {
  #if DEBUG && os(macOS)
  setenv("LOCAL_LAMBDA_SERVER_ENABLED", "true", 1)
  #endif

  // Initialize some immutable vars used in the callback

  Lambda.run { (context: Lambda.Context, request: APIGateway.V2.Request, callback: @escaping  (Result<APIGateway.V2.Response, Error>) -> Void) in
    // Handle request and call the callback
  }
}