@@ -53,33 +53,60 @@ internal async Task TestFunction(string functionName)
53
53
LogType = LogType . Tail
54
54
} ;
55
55
56
- // run twice for cold and warm start
57
- for ( int i = 0 ; i < 2 ; i ++ )
56
+ // Test cold start
57
+ var coldStartResponse = await _lambdaClient . InvokeAsync ( request ) ;
58
+ ValidateResponse ( coldStartResponse , true ) ;
59
+
60
+ // Test warm start
61
+ var warmStartResponse = await _lambdaClient . InvokeAsync ( request ) ;
62
+ ValidateResponse ( warmStartResponse , false ) ;
63
+
64
+ // Assert cloudwatch
65
+ await AssertCloudWatch ( ) ;
66
+ }
67
+
68
+ private void ValidateResponse ( InvokeResponse response , bool isColdStart )
69
+ {
70
+ if ( string . IsNullOrEmpty ( response . LogResult ) )
58
71
{
59
- var response = await _lambdaClient . InvokeAsync ( request ) ;
72
+ Assert . Fail ( "No LogResult field returned in the response of Lambda invocation." ) ;
73
+ }
60
74
61
- if ( string . IsNullOrEmpty ( response . LogResult ) )
62
- {
63
- Assert . Fail ( "No LogResult field returned in the response of Lambda invocation." ) ;
64
- }
75
+ var payload = System . Text . Encoding . UTF8 . GetString ( response . Payload . ToArray ( ) ) ;
76
+ var parsedPayload = JsonSerializer . Deserialize < APIGatewayProxyResponse > ( payload ) ;
65
77
66
- var payload = System . Text . Encoding . UTF8 . GetString ( response . Payload . ToArray ( ) ) ;
67
- var parsedPayload = JsonSerializer . Deserialize < APIGatewayProxyResponse > ( payload ) ;
78
+ if ( parsedPayload == null )
79
+ {
80
+ Assert . Fail ( "Failed to parse payload." ) ;
81
+ }
68
82
69
- if ( parsedPayload == null )
70
- {
71
- Assert . Fail ( "Failed to parse payload." ) ;
72
- }
83
+ Assert . Equal ( 200 , parsedPayload . StatusCode ) ;
84
+ Assert . Equal ( "HELLO WORLD" , parsedPayload . Body ) ;
85
+
86
+ // Assert Output log from Lambda execution
87
+ AssertOutputLog ( response , isColdStart ) ;
88
+ }
89
+
90
+ private void AssertOutputLog ( InvokeResponse response , bool expectedColdStart )
91
+ {
92
+ var logResult = System . Text . Encoding . UTF8 . GetString ( Convert . FromBase64String ( response . LogResult ) ) ;
93
+ _testOutputHelper . WriteLine ( logResult ) ;
94
+ var output = OutputLogParser . ParseLogSegments ( logResult , out var report ) ;
95
+ var isColdStart = report . initDuration != "N/A" ;
73
96
74
- Assert . Equal ( 200 , parsedPayload . StatusCode ) ;
75
- Assert . Equal ( "HELLO WORLD" , parsedPayload . Body ) ;
97
+ Assert . Equal ( expectedColdStart , isColdStart ) ;
76
98
77
- // Assert Output log from Lambda execution
78
- AssertOutputLog ( response ) ;
99
+ if ( isColdStart )
100
+ {
101
+ AssertColdStart ( output [ 0 ] ) ;
102
+ AssertSingleMetric ( output [ 1 ] ) ;
103
+ AssertMetricsDimensionsMetadata ( output [ 2 ] ) ;
104
+ }
105
+ else
106
+ {
107
+ AssertSingleMetric ( output [ 0 ] ) ;
108
+ AssertMetricsDimensionsMetadata ( output [ 1 ] ) ;
79
109
}
80
-
81
- // Assert cloudwatch
82
- await AssertCloudWatch ( ) ;
83
110
}
84
111
85
112
private async Task AssertCloudWatch ( )
@@ -116,24 +143,6 @@ private async Task AssertCloudWatch()
116
143
}
117
144
}
118
145
119
- private void AssertOutputLog ( InvokeResponse response )
120
- {
121
- // Extract and parse log
122
- var logResult = System . Text . Encoding . UTF8 . GetString ( Convert . FromBase64String ( response . LogResult ) ) ;
123
- _testOutputHelper . WriteLine ( logResult ) ;
124
- var output = OutputLogParser . ParseLogSegments ( logResult , out var report ) ;
125
- var isColdStart = report . initDuration != "N/A" ;
126
- var index = 0 ;
127
- if ( isColdStart )
128
- {
129
- AssertColdStart ( output [ index ] ) ;
130
- index += 1 ;
131
- }
132
-
133
- AssertSingleMetric ( output [ index ] ) ;
134
- AssertMetricsDimensionsMetadata ( output [ index + 1 ] ) ;
135
- }
136
-
137
146
private void AssertMetricsDimensionsMetadata ( string output )
138
147
{
139
148
using JsonDocument doc = JsonDocument . Parse ( output ) ;
0 commit comments