Skip to content

Commit 97fb5dc

Browse files
authored
Fixed an issue that the resolved environment was not cached (#60)
1 parent 5089893 commit 97fb5dc

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
plugins {
1717
id 'java-library'
18-
id 'com.diffplug.spotless' version '5.1.0'
18+
id 'com.diffplug.spotless' version '5.8.2'
1919
id 'maven-publish'
2020
id 'signing'
2121
}

examples/lambda/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ repositories {
99

1010
dependencies {
1111
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
12+
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
13+
implementation "org.apache.logging.log4j:log4j-core:2.13.3"
14+
implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3"
15+
implementation 'com.amazonaws:aws-lambda-java-log4j2:1.2.0'
1216
implementation rootProject
1317

1418
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender">
19+
<Appenders>
20+
<Lambda name="Lambda">
21+
<PatternLayout>
22+
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
23+
</PatternLayout>
24+
</Lambda>
25+
</Appenders>
26+
<Loggers>
27+
<Root level="debug">
28+
<AppenderRef ref="Lambda" />
29+
</Root>
30+
</Loggers>
31+
</Configuration>

src/main/java/software/amazon/cloudwatchlogs/emf/environment/EnvironmentProvider.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,10 @@ public CompletableFuture<Environment> resolveEnvironment() {
5454
CompletableFuture<Optional<Environment>> resolvedEnv = discoverEnvironmentAsync();
5555

5656
return resolvedEnv.thenApply(
57-
optionalEnv ->
58-
optionalEnv.orElseGet(
59-
() -> {
60-
cachedEnvironment = defaultEnvironment;
61-
return cachedEnvironment;
62-
}));
57+
optionalEnv -> {
58+
cachedEnvironment = optionalEnv.orElse(defaultEnvironment);
59+
return cachedEnvironment;
60+
});
6361
}
6462

6563
public Environment getDefaultEnvironment() {

src/test/java/software/amazon/cloudwatchlogs/emf/environment/EnvironmentProviderTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static org.junit.Assert.assertSame;
2020
import static org.junit.Assert.assertTrue;
21-
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.*;
2222
import static org.powermock.api.mockito.PowerMockito.when;
2323

2424
import com.github.javafaker.Faker;
@@ -82,6 +82,28 @@ public void testResolveEnvironmentReturnsLambdaEnvironment() {
8282
assertTrue(resolvedEnvironment.join() instanceof LambdaEnvironment);
8383
}
8484

85+
@Test
86+
public void testResolveEnvironmentFromCacheAfterSecondCall() {
87+
environmentProvider.cleanResolvedEnvironment();
88+
String lambdaFunctionName = faker.name().name();
89+
90+
PowerMockito.mockStatic(SystemWrapper.class);
91+
when(SystemWrapper.getenv("AWS_LAMBDA_FUNCTION_NAME")).thenReturn(lambdaFunctionName);
92+
93+
// First call
94+
CompletableFuture<Environment> resolvedEnvironment =
95+
environmentProvider.resolveEnvironment();
96+
97+
assertTrue(resolvedEnvironment.join() instanceof LambdaEnvironment);
98+
99+
// Second call
100+
assertTrue(environmentProvider.resolveEnvironment().join() instanceof LambdaEnvironment);
101+
102+
// The SystemWrapper.getenv should only be called once due to cache
103+
PowerMockito.verifyStatic(SystemWrapper.class);
104+
SystemWrapper.getenv("AWS_LAMBDA_FUNCTION_NAME");
105+
}
106+
85107
@Test
86108
public void testResolveEnvironmentReturnsLambdaFromOverride() {
87109
PowerMockito.mockStatic(EnvironmentConfigurationProvider.class);

0 commit comments

Comments
 (0)