File tree Expand file tree Collapse file tree 5 files changed +63
-8
lines changed
main/java/software/amazon/cloudwatchlogs/emf/environment
test/java/software/amazon/cloudwatchlogs/emf/environment Expand file tree Collapse file tree 5 files changed +63
-8
lines changed Original file line number Diff line number Diff line change 15
15
16
16
plugins {
17
17
id ' java-library'
18
- id ' com.diffplug.spotless' version ' 5.1.0 '
18
+ id ' com.diffplug.spotless' version ' 5.8.2 '
19
19
id ' maven-publish'
20
20
id ' signing'
21
21
}
Original file line number Diff line number Diff line change @@ -9,6 +9,10 @@ repositories {
9
9
10
10
dependencies {
11
11
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'
12
16
implementation rootProject
13
17
14
18
}
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change @@ -54,12 +54,10 @@ public CompletableFuture<Environment> resolveEnvironment() {
54
54
CompletableFuture <Optional <Environment >> resolvedEnv = discoverEnvironmentAsync ();
55
55
56
56
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
+ });
63
61
}
64
62
65
63
public Environment getDefaultEnvironment () {
Original file line number Diff line number Diff line change 18
18
19
19
import static org .junit .Assert .assertSame ;
20
20
import static org .junit .Assert .assertTrue ;
21
- import static org .mockito .Mockito .mock ;
21
+ import static org .mockito .Mockito .* ;
22
22
import static org .powermock .api .mockito .PowerMockito .when ;
23
23
24
24
import com .github .javafaker .Faker ;
@@ -82,6 +82,28 @@ public void testResolveEnvironmentReturnsLambdaEnvironment() {
82
82
assertTrue (resolvedEnvironment .join () instanceof LambdaEnvironment );
83
83
}
84
84
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
+
85
107
@ Test
86
108
public void testResolveEnvironmentReturnsLambdaFromOverride () {
87
109
PowerMockito .mockStatic (EnvironmentConfigurationProvider .class );
You can’t perform that action at this time.
0 commit comments