Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RemainingTime returning negative values #611

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Amazon.Lambda.RuntimeSupport
{
internal class LambdaContext : ILambdaContext
{
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private LambdaEnvironment _lambdaEnvironment;
private RuntimeApiHeaders _runtimeApiHeaders;
Expand Down Expand Up @@ -69,6 +69,6 @@ public LambdaContext(RuntimeApiHeaders runtimeApiHeaders, LambdaEnvironment lamb

public int MemoryLimitInMB => _memoryLimitInMB;

public TimeSpan RemainingTime => TimeSpan.FromMilliseconds(_deadlineMs - (DateTime.Now - UnixEpoch).TotalMilliseconds);
public TimeSpan RemainingTime => TimeSpan.FromMilliseconds(_deadlineMs - (DateTime.UtcNow - UnixEpoch).TotalMilliseconds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace Amazon.Lambda.RuntimeSupport.UnitTests
{
public class LambdaContextTests
{
private readonly TestEnvironmentVariables _environmentVariables;

public LambdaContextTests()
{
_environmentVariables = new TestEnvironmentVariables();
}

[Fact]
public void RemainingTimeIsPositive()
{
var deadline = DateTimeOffset.UtcNow.AddHours(1);
var deadlineMs = deadline.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture);

var headers = new Dictionary<string, IEnumerable<string>>
{
["Lambda-Runtime-Aws-Request-Id"] = new[] { Guid.NewGuid().ToString() },
["Lambda-Runtime-Deadline-Ms"] = new[] { deadlineMs },
["Lambda-Runtime-Invoked-Function-Arn"] = new[] { "my-function-arn" }
};

var runtimeApiHeaders = new RuntimeApiHeaders(headers);
var lambdaEnvironment = new LambdaEnvironment(_environmentVariables);

var context = new LambdaContext(runtimeApiHeaders, lambdaEnvironment);

Assert.True(context.RemainingTime >= TimeSpan.Zero, $"Remaining time is not a positive value: {context.RemainingTime}");
}
}
}