Skip to content

Commit 91ee9df

Browse files
committed
Fixed resource name and added unit tests and javadoc comments
1 parent ec43605 commit 91ee9df

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal/ClassPreLoader.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,37 @@
2222
import java.net.URLConnection;
2323
import java.util.Enumeration;
2424

25+
/**
26+
* Used to preload classes to support automatic priming for SnapStart
27+
*/
2528
public class ClassPreLoader {
2629
public static final String CLASSES_FILE = "classesloaded.txt";
27-
public static void preloadClass(String classname, boolean initialize) {
28-
try {
29-
Class.forName(classname, initialize, ClassPreLoader.class.getClassLoader());
30-
} catch (Throwable ignored) {
31-
}
32-
}
33-
public static void preloadClasses(boolean initialize) {
30+
31+
/**
32+
* Initializes the classes listed in the classesloaded resource
33+
*/
34+
public static void preloadClasses() {
3435
try {
3536
Enumeration<URL> files = ClassPreLoader.class.getClassLoader().getResources(CLASSES_FILE);
37+
// If there are multiple files, preload classes from all of them
3638
while (files.hasMoreElements()) {
3739
URL url = files.nextElement();
3840
URLConnection conn = url.openConnection();
3941
conn.setUseCaches(false);
4042
InputStream is = conn.getInputStream();
41-
preloadClassesFromStream(is, initialize);
43+
preloadClassesFromStream(is);
4244
}
4345
} catch (IOException ignored) {
46+
// No action is required if preloading fails for any reason
4447
}
45-
InputStream is = ClassPreLoader.class
46-
.getResourceAsStream( CLASSES_FILE);
47-
if (is != null)
48-
preloadClassesFromStream(is, initialize);
4948
}
50-
public static void preloadClassesFromStream(InputStream is, boolean initialize) {
49+
50+
/**
51+
* Loads the list of classes passed as a stream
52+
*
53+
* @param is
54+
*/
55+
private static void preloadClassesFromStream(InputStream is) {
5156
try (is;
5257
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
5358
BufferedReader reader = new BufferedReader(isr)) {
@@ -59,13 +64,11 @@ public static void preloadClassesFromStream(InputStream is, boolean initialize)
5964
}
6065
final String className = line.stripTrailing();
6166
if (!className.isBlank()) {
62-
preloadClass(className, initialize);
67+
Class.forName(className, true, ClassPreLoader.class.getClassLoader());
6368
}
6469
}
6570
} catch (Exception ignored) {
71+
// No action is required if preloading fails for any reason
6672
}
6773
}
68-
public void invokePreloadClasses(boolean initialize) {
69-
preloadClasses(initialize);
70-
}
7174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package software.amazon.lambda.powertools.common.internal;
2+
3+
import static org.mockito.Mockito.*;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.InputStream;
7+
import java.net.URL;
8+
import java.net.URLConnection;
9+
import java.util.Collections;
10+
11+
import org.junit.jupiter.api.Test;
12+
13+
class ClassPreLoaderTest {
14+
15+
@Test
16+
void preloadClasses_shouldIgnoreInvalidClassesAndLoadValidClasses() throws Exception {
17+
// Mock the class loader with no resources
18+
ClassLoader classLoader = mock(ClassLoader.class);
19+
URL mockUrl = mock(URL.class);
20+
URLConnection mockConnection = mock(URLConnection.class);
21+
InputStream mockInputStream = new ByteArrayInputStream("java.lang.String\nInvalid.Class".getBytes());
22+
23+
when(mockUrl.openConnection()).thenReturn(mockConnection);
24+
when(mockConnection.getInputStream()).thenReturn(mockInputStream);
25+
when(classLoader.getResources(ClassPreLoader.CLASSES_FILE))
26+
.thenReturn(Collections.enumeration(Collections.singletonList(mockUrl)));
27+
28+
// Inject the mocked class loader
29+
Thread.currentThread().setContextClassLoader(classLoader);
30+
// Call the method under test
31+
ClassPreLoader.preloadClasses();
32+
33+
// Verify that only the valid class was loaded
34+
Class.forName("java.lang.String", true, ClassPreLoader.class.getClassLoader());
35+
}
36+
37+
@Test
38+
void preloadClasses_shouldHandleEmptyResources() throws Exception {
39+
// Mock the class loader with no resources
40+
ClassLoader classLoader = mock(ClassLoader.class);
41+
when(classLoader.getResources(ClassPreLoader.CLASSES_FILE))
42+
.thenReturn(Collections.emptyEnumeration());
43+
44+
// Inject the mocked class loader
45+
Thread.currentThread().setContextClassLoader(classLoader);
46+
47+
// Call the method under test
48+
ClassPreLoader.preloadClasses();
49+
50+
// Verify no interactions with the class loader
51+
verifyNoInteractions(classLoader);
52+
}
53+
}

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspect.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ private void validateMetricsAndRefreshOnFailure(Metrics metrics) {
158158

159159
@Override
160160
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
161-
System.out.println("before preloading");
162-
ClassPreLoader.preloadClasses(true);
163-
System.out.println("after preloading");
161+
ClassPreLoader.preloadClasses();
164162
}
163+
165164
@Override
166165
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
167166
}

0 commit comments

Comments
 (0)