1
+ using System ;
2
+ using AWS . Lambda . Powertools . Common . Core ;
3
+ using Xunit ;
4
+
5
+ namespace AWS . Lambda . Powertools . Common . Tests ;
6
+
7
+ public class LambdaLifecycleTrackerTests : IDisposable
8
+ {
9
+ public LambdaLifecycleTrackerTests ( )
10
+ {
11
+ // Reset before each test to ensure clean state
12
+ LambdaLifecycleTracker . Reset ( ) ;
13
+ Environment . SetEnvironmentVariable ( Constants . AWSInitializationTypeEnv , null ) ;
14
+ }
15
+
16
+ public void Dispose ( )
17
+ {
18
+ // Reset after each test
19
+ LambdaLifecycleTracker . Reset ( ) ;
20
+ Environment . SetEnvironmentVariable ( Constants . AWSInitializationTypeEnv , null ) ;
21
+ }
22
+
23
+ [ Fact ]
24
+ public void IsColdStart_FirstInvocation_ReturnsTrue ( )
25
+ {
26
+ // Act
27
+ var result = LambdaLifecycleTracker . IsColdStart ;
28
+
29
+ // Assert
30
+ Assert . True ( result ) ;
31
+ }
32
+
33
+ [ Fact ]
34
+ public void IsColdStart_SecondInvocation_ReturnsFalse ( )
35
+ {
36
+ // Arrange - first access to trigger cold start
37
+ _ = LambdaLifecycleTracker . IsColdStart ;
38
+
39
+ // Clear just the AsyncLocal value to simulate new invocation in same container
40
+ LambdaLifecycleTracker . Reset ( resetContainer : false ) ;
41
+
42
+ // Act - second invocation on same container
43
+ var result = LambdaLifecycleTracker . IsColdStart ;
44
+
45
+ // Assert
46
+ Assert . False ( result ) ;
47
+ }
48
+
49
+ [ Fact ]
50
+ public void IsColdStart_WithProvisionedConcurrency_ReturnsFalse ( )
51
+ {
52
+ // Arrange
53
+ Environment . SetEnvironmentVariable ( Constants . AWSInitializationTypeEnv , "provisioned-concurrency" ) ;
54
+
55
+ // Act
56
+ var result = LambdaLifecycleTracker . IsColdStart ;
57
+
58
+ // Assert
59
+ Assert . False ( result ) ;
60
+ }
61
+
62
+ [ Fact ]
63
+ public void IsColdStart_ReturnsSameValueWithinInvocation ( )
64
+ {
65
+ // Act - access multiple times in the same invocation
66
+ var firstAccess = LambdaLifecycleTracker . IsColdStart ;
67
+ var secondAccess = LambdaLifecycleTracker . IsColdStart ;
68
+ var thirdAccess = LambdaLifecycleTracker . IsColdStart ;
69
+
70
+ // Assert
71
+ Assert . True ( firstAccess ) ;
72
+ Assert . Equal ( firstAccess , secondAccess ) ;
73
+ Assert . Equal ( firstAccess , thirdAccess ) ;
74
+ }
75
+
76
+ [ Fact ]
77
+ public void Reset_ResetsState ( )
78
+ {
79
+ // Arrange
80
+ _ = LambdaLifecycleTracker . IsColdStart ; // First invocation
81
+
82
+ // Act
83
+ LambdaLifecycleTracker . Reset ( ) ;
84
+ var result = LambdaLifecycleTracker . IsColdStart ;
85
+
86
+ // Assert
87
+ Assert . True ( result ) ; // Should be true again after reset
88
+ }
89
+
90
+ [ Fact ]
91
+ public void Reset_ClearsEnvironmentSetting ( )
92
+ {
93
+ // Arrange
94
+ Environment . SetEnvironmentVariable ( Constants . AWSInitializationTypeEnv , "provisioned-concurrency" ) ;
95
+ _ = LambdaLifecycleTracker . IsColdStart ; // Load the environment variable
96
+
97
+ // Act
98
+ LambdaLifecycleTracker . Reset ( ) ;
99
+ Environment . SetEnvironmentVariable ( Constants . AWSInitializationTypeEnv , null ) ; // Clear the environment
100
+ var result = LambdaLifecycleTracker . IsColdStart ;
101
+
102
+ // Assert
103
+ Assert . True ( result ) ; // Should be true when env var is cleared
104
+ }
105
+ }
0 commit comments