@@ -15,6 +15,9 @@ import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
1515import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments' ;
1616import * as AWS from 'aws-sdk' ;
1717import { MetricUnits } from '../../src' ;
18+ import { getMetrics } from '../helpers/metricsUtils' ;
19+
20+ const ONE_MINUTE = 1000 * 60 ;
1821
1922const cloudwatchClient = new AWS . CloudWatch ( ) ;
2023const lambdaClient = new AWS . Lambda ( ) ;
@@ -80,10 +83,7 @@ describe('happy cases', () => {
8083 . promise ( ) ;
8184 }
8285
83- // THEN
84- // sleep to allow metrics to be collected
85- await new Promise ( ( resolve ) => setTimeout ( resolve , 15000 ) ) ;
86- } , 200000 ) ;
86+ } , ONE_MINUTE * 3 ) ;
8787
8888 it ( 'capture ColdStart Metric' , async ( ) => {
8989 const expectedDimensions = [
@@ -92,12 +92,8 @@ describe('happy cases', () => {
9292 { Name : Object . keys ( expectedDefaultDimensions ) [ 0 ] , Value : expectedDefaultDimensions . MyDimension } ,
9393 ] ;
9494 // Check coldstart metric dimensions
95- const coldStartMetrics = await cloudwatchClient
96- . listMetrics ( {
97- Namespace : expectedNamespace ,
98- MetricName : 'ColdStart' ,
99- } )
100- . promise ( ) ;
95+ const coldStartMetrics = await getMetrics ( cloudwatchClient , expectedNamespace , 'ColdStart' , 1 ) ;
96+
10197 expect ( coldStartMetrics . Metrics ?. length ) . toBe ( 1 ) ;
10298 const coldStartMetric = coldStartMetrics . Metrics ?. [ 0 ] ;
10399 expect ( coldStartMetric ?. Dimensions ) . toStrictEqual ( expectedDimensions ) ;
@@ -124,16 +120,12 @@ describe('happy cases', () => {
124120 // Despite lambda has been called twice, coldstart metric sum should only be 1
125121 const singleDataPoint = coldStartMetricStat . Datapoints ? coldStartMetricStat . Datapoints [ 0 ] : { } ;
126122 expect ( singleDataPoint ?. Sum ) . toBe ( 1 ) ;
127- } , 15000 ) ;
123+ } , ONE_MINUTE * 3 ) ;
128124
129125 it ( 'produce added Metric with the default and extra one dimensions' , async ( ) => {
130126 // Check metric dimensions
131- const metrics = await cloudwatchClient
132- . listMetrics ( {
133- Namespace : expectedNamespace ,
134- MetricName : expectedMetricName ,
135- } )
136- . promise ( ) ;
127+ const metrics = await getMetrics ( cloudwatchClient , expectedNamespace , expectedMetricName , 1 ) ;
128+
137129 expect ( metrics . Metrics ?. length ) . toBe ( 1 ) ;
138130 const metric = metrics . Metrics ?. [ 0 ] ;
139131 const expectedDimensions = [
@@ -144,16 +136,16 @@ describe('happy cases', () => {
144136 expect ( metric ?. Dimensions ) . toStrictEqual ( expectedDimensions ) ;
145137
146138 // Check coldstart metric value
147- const adjustedStartTime = new Date ( startTime . getTime ( ) - 60 * 1000 ) ;
148- const endTime = new Date ( new Date ( ) . getTime ( ) + 60 * 1000 ) ;
139+ const adjustedStartTime = new Date ( startTime . getTime ( ) - 3 * ONE_MINUTE ) ;
140+ const endTime = new Date ( new Date ( ) . getTime ( ) + ONE_MINUTE ) ;
149141 console . log ( `Manual command: aws cloudwatch get-metric-statistics --namespace ${ expectedNamespace } --metric-name ${ expectedMetricName } --start-time ${ Math . floor ( adjustedStartTime . getTime ( ) / 1000 ) } --end-time ${ Math . floor ( endTime . getTime ( ) / 1000 ) } --statistics 'Sum' --period 60 --dimensions '${ JSON . stringify ( expectedDimensions ) } '` ) ;
150142 const metricStat = await cloudwatchClient
151143 . getMetricStatistics (
152144 {
153145 Namespace : expectedNamespace ,
154- StartTime : new Date ( startTime . getTime ( ) - 60 * 1000 ) , // minus 1 minute ,
146+ StartTime : adjustedStartTime ,
155147 Dimensions : expectedDimensions ,
156- EndTime : new Date ( new Date ( ) . getTime ( ) + 60 * 1000 ) ,
148+ EndTime : endTime ,
157149 Period : 60 ,
158150 MetricName : expectedMetricName ,
159151 Statistics : [ 'Sum' ] ,
@@ -165,7 +157,7 @@ describe('happy cases', () => {
165157 // Since lambda has been called twice in this test and potentially more in others, metric sum should be at least of expectedMetricValue * invocationCount
166158 const singleDataPoint = metricStat . Datapoints ? metricStat . Datapoints [ 0 ] : { } ;
167159 expect ( singleDataPoint ?. Sum ) . toBeGreaterThanOrEqual ( parseInt ( expectedMetricValue ) * invocationCount ) ;
168- } , 15000 ) ;
160+ } , ONE_MINUTE * 3 ) ;
169161
170162 afterAll ( async ( ) => {
171163 if ( ! process . env . DISABLE_TEARDOWN ) {
@@ -181,5 +173,5 @@ describe('happy cases', () => {
181173 quiet : true ,
182174 } ) ;
183175 }
184- } , 200000 ) ;
176+ } , ONE_MINUTE * 3 ) ;
185177} ) ;
0 commit comments