Skip to content

Conversation

sebsto
Copy link
Contributor

@sebsto sebsto commented Sep 1, 2025

Allows users to define on which port the Local server listens to, using the LOCAL_LAMBDA_PORT environment variable.

While being at it, I also added LOCAL_LAMBDA_HOST if the user wants to bind on a specific IP address.

I renamed LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT to LOCAL_LAMBDA_INVOCATION_ENDPOINT for consistency.

Motivation:

Addresses #556

Modifications:

  • When run outside of the Lambda execution environment, check for the value of LOCAL_LAMBDA_PORT and passes it down to the Lambda HTTP Local Server and runtime client.

  • Add a unit test

Result:

LAMBDA_USE_LOCAL_DEPS=../.. LOCAL_LAMBDA_PORT=8888 swift run                  

2025-09-01T21:55:22+0200 info LambdaRuntime: host="127.0.0.1" port=8888 [AWSLambdaRuntime] Server started and listening

@sebsto sebsto added this to the 2.0 milestone Sep 1, 2025
@sebsto sebsto self-assigned this Sep 1, 2025
@sebsto sebsto added the 🆕 semver/minor Adds new public API. label Sep 1, 2025
Copilot

This comment was marked as outdated.

@sebsto sebsto requested a review from Copilot September 2, 2025 06:53
Copilot

This comment was marked as outdated.

Copy link
Member

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor issue in the tests. I think you are going to have to group together all the tests that use LambdaRuntime and mark them with the .serialized test trait so they don't run on parallel


// Set environment variable
setenv("LOCAL_LAMBDA_PORT", "\(customPort)", 1)
defer { unsetenv("LOCAL_LAMBDA_PORT") }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given these tests are running in parallel, there is a chance the LOCAL_LAMBDA_PORT environment var setting will affect other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adam-fowler good point. I aggressively added .serialized on all tests using the server or the runtime client

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that might only run tests serially within each suite so two suites can still run in parallel, even though the tests internally run serially. Might be worth asking swift-testing folks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adam-fowler During the tests, we let the MockServer bind on any available port (port = 0) and share its actual port number with the runtime client. There is no risk of conflict.

See

I kept the .serialized only on the new test in case someone adds another test to this suite later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem isn't the fact you might have multiple servers binding to the same address (although that is a problem and am surprised it hasn't reared it head before). The problem is the environment variable is being set when other tests could be running and could affect those tests.

We can leave as is, but I think we will get the occasional test failure as multiple tests inherit the environment variable and bind to the same address.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adam-fowler

am surprised it hasn't reared it head before

In general, the unit tests don't use the local server. They use a MockServer that uses a random port number to avoid conflicts.

I think we will get the occasional test failure as multiple tests inherit the environment variable and bind to the same address

There are three groups of tests that use the real server :

  • LambdaRuntimeTests.swift
  • LambdaRuntime+ServiceLifecycle.swift
  • LambdaLocalServerTests.swift (this PR)

I will add a commit to group them in the same .serialized suite.

Copy link
Contributor Author

@sebsto sebsto Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@sebsto
Copy link
Contributor Author

sebsto commented Sep 3, 2025

fixes #556

@sebsto sebsto merged commit d8ee71f into swift-server:main Sep 3, 2025
35 checks passed
@sebsto sebsto deleted the sebsto/local_port branch September 3, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants