Skip to content

Commit

Permalink
Remove Lambda runtime workarounds
Browse files Browse the repository at this point in the history
Remove the need to use reflection to set the HttpClient.
Throw OperationCanceledException instead of returning a dummy response.
  • Loading branch information
martincostello committed Nov 3, 2019
1 parent 8ac6b36 commit d7dff32
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/AwsLambdaTestServer/RuntimeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ internal async Task HandleNextAsync(HttpContext httpContext)
"Stopped listening for additional requests for Lambda function with ARN {FunctionArn}.",
_options.FunctionArn);

// Send a dummy response to prevent the listen loop from erroring
request = new LambdaTestRequest(new[] { (byte)'{', (byte)'}' }, "xx-lambda-test-server-stopped-xx");

// This dummy request wasn't enqueued, so it needs manually adding
_responses.GetOrAdd(request.AwsRequestId, (_) => Channel.CreateBounded<LambdaTestResponse>(1));
throw;
}

// Write the response for the Lambda runtime to pass to the function to invoke
Expand Down
13 changes: 1 addition & 12 deletions tests/AwsLambdaTestServer.Tests/FunctionRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.RuntimeSupport;
Expand All @@ -19,17 +18,7 @@ internal static async Task RunAsync<T>(HttpClient httpClient, CancellationToken
var serializer = new JsonSerializer();

using var handlerWrapper = HandlerWrapper.GetHandlerWrapper<MyRequest, MyResponse>(handler.SumAsync, serializer);
using var bootstrap = new LambdaBootstrap(handlerWrapper, handler.InitializeAsync);

if (httpClient != null)
{
// Replace the internal runtime API client with one using the specified HttpClient.
// See https://github.com/aws/aws-lambda-dotnet/blob/4f9142b95b376bd238bce6be43f4e1ec1f983592/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs#L41
var client = new RuntimeApiClient(httpClient);

var property = typeof(LambdaBootstrap).GetProperty("Client", BindingFlags.Instance | BindingFlags.NonPublic);
property.SetValue(bootstrap, client);
}
using var bootstrap = new LambdaBootstrap(httpClient, handlerWrapper, handler.InitializeAsync);

await bootstrap.RunAsync(cancellationToken);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/AwsLambdaTestServer.Tests/LambdaTestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,29 @@ void Configure(IServiceCollection services)
}
}

[Fact]
public async Task Function_Returns_If_No_Requests_Within_Timeout()
{
// Arrange
void Configure(IServiceCollection services)
{
services.AddLogging((builder) => builder.AddXUnit(this));
}

using var server = new LambdaTestServer(Configure);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));

await server.StartAsync(cts.Token);

using var httpClient = server.CreateClient();

// Act
await MyFunctionEntrypoint.RunAsync(httpClient, cts.Token);

// Assert
cts.IsCancellationRequested.ShouldBeTrue();
}

[Fact]
public void Finalizer_Does_Not_Throw()
{
Expand Down

0 comments on commit d7dff32

Please sign in to comment.