Skip to content

Commit 454772d

Browse files
meshwa19Meshwa Savalia
andauthored
Fix canary's heap usage to avoid system crash (#46)
Retain cached environment Co-authored-by: Meshwa Savalia <meshwas@amazon.com>
1 parent 4e6211b commit 454772d

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/Amazon.CloudWatch.EMF/Environment/EnvironmentProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class EnvironmentProvider : IEnvironmentProvider
1414
private readonly IResourceFetcher _resourceFetcher;
1515
private readonly ILoggerFactory _loggerFactory;
1616
private readonly ILogger<ECSEnvironment> _logger;
17-
private IEnvironment _cachedEnvironment;
17+
private static IEnvironment _cachedEnvironment;
1818

1919
public EnvironmentProvider(IConfiguration configuration, IResourceFetcher resourceFetcher)
2020
: this(configuration, resourceFetcher, NullLoggerFactory.Instance)
@@ -58,6 +58,14 @@ public IEnvironment ResolveEnvironment()
5858
return new DefaultEnvironment(_configuration, _loggerFactory);
5959
}
6060

61+
/// <summary>
62+
/// A helper method to clean the cached environment in tests.
63+
/// </summary>
64+
internal void CleanResolvedEnvironment()
65+
{
66+
_cachedEnvironment = null;
67+
}
68+
6169
private IEnvironment GetEnvironmentFromConfig()
6270
{
6371
switch (_configuration.EnvironmentOverride)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
2-
COPY bin/Release/netcoreapp3.1/publish/ App/
1+
FROM mcr.microsoft.com/dotnet/aspnet:6.0
2+
COPY bin/Release/net6.0/publish/ App/
33
WORKDIR /App
44
ENTRYPOINT ["dotnet", "Amazon.CloudWatch.EMF.Canary.dll"]

tests/Amazon.CloudWatch.EMF.Tests/Environment/EnvironmentProviderTests.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
using AutoFixture.AutoNSubstitute;
55
using NSubstitute;
66
using Xunit;
7+
using System;
78

89
namespace Amazon.CloudWatch.EMF.Tests.Environment
910
{
10-
public class EnvironmentProviderTests
11+
public class EnvironmentProviderTests: IDisposable
1112
{
13+
14+
private EnvironmentProvider environmentProvider;
15+
16+
public void Dispose(){
17+
environmentProvider.CleanResolvedEnvironment();
18+
}
19+
1220
[Fact]
1321
public void ResolveEnvironment_ReturnCachedEnv()
1422
{
@@ -17,8 +25,8 @@ public void ResolveEnvironment_ReturnCachedEnv()
1725
var configuration = fixture.Create<IConfiguration>();
1826
configuration.EnvironmentOverride.Returns(Environments.Local);
1927
var resourceFetcher = fixture.Create<IResourceFetcher>();
20-
var environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
21-
28+
environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
29+
2230
//Act
2331
var environment = environmentProvider.ResolveEnvironment();
2432
var environmentCache = environmentProvider.ResolveEnvironment();
@@ -35,8 +43,8 @@ public void ResolveEnvironment_ReturnsLambdaEnvironment()
3543
var configuration = fixture.Create<IConfiguration>();
3644
configuration.EnvironmentOverride.Returns(Environments.Lambda);
3745
var resourceFetcher = fixture.Create<IResourceFetcher>();
38-
var environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
39-
46+
environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
47+
4048
//Act
4149
var environment = environmentProvider.ResolveEnvironment();
4250

@@ -52,8 +60,8 @@ public void ResolveEnvironment_ReturnsLocalEnvironment()
5260
var configuration = fixture.Create<IConfiguration>();
5361
configuration.EnvironmentOverride.Returns(Environments.Local);
5462
var resourceFetcher = fixture.Create<IResourceFetcher>();
55-
var environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
56-
63+
environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
64+
5765
//Act
5866
var environment = environmentProvider.ResolveEnvironment();
5967

@@ -69,8 +77,8 @@ public void ResolveEnvironment_ReturnsEC2Environment()
6977
var configuration = fixture.Create<IConfiguration>();
7078
configuration.EnvironmentOverride.Returns(Environments.EC2);
7179
var resourceFetcher = fixture.Create<IResourceFetcher>();
72-
var environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
73-
80+
environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
81+
7482
//Act
7583
var environment = environmentProvider.ResolveEnvironment();
7684

@@ -86,8 +94,8 @@ public void ResolveEnvironment_ReturnsECSEnvironment()
8694
var configuration = fixture.Create<IConfiguration>();
8795
configuration.EnvironmentOverride.Returns(Environments.ECS);
8896
var resourceFetcher = fixture.Create<IResourceFetcher>();
89-
var environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
90-
97+
environmentProvider = new EnvironmentProvider(configuration, resourceFetcher);
98+
9199
//Act
92100
var environment = environmentProvider.ResolveEnvironment();
93101

0 commit comments

Comments
 (0)