@@ -16,6 +16,7 @@ import {
1616 DEFAULT_NAMESPACE ,
1717 MAX_DIMENSION_COUNT ,
1818 MAX_METRICS_SIZE ,
19+ MAX_METRIC_VALUES_SIZE ,
1920} from '../../src/constants' ;
2021import { setupDecoratorLambdaHandler } from '../helpers/metricsUtils' ;
2122import {
@@ -701,6 +702,34 @@ describe('Class: Metrics', () => {
701702 ) ;
702703 } ) ;
703704
705+ test ( 'it should publish metrics when the array of values reaches the maximum size' , ( ) => {
706+ // Prepare
707+ const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
708+ const consoleSpy = jest . spyOn ( console , 'log' ) ;
709+ const metricName = 'test-metric' ;
710+
711+ // Act
712+ for ( let i = 0 ; i <= MAX_METRIC_VALUES_SIZE ; i ++ ) {
713+ metrics . addMetric ( `${ metricName } ` , MetricUnits . Count , i ) ;
714+ }
715+ metrics . publishStoredMetrics ( ) ;
716+
717+ // Assess
718+ // 2 calls to console.log: 1 for the first batch of metrics, 1 for the second batch (explicit call)
719+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 2 ) ;
720+ const firstMetricsJson = JSON . parse (
721+ consoleSpy . mock . calls [ 0 ] [ 0 ]
722+ ) as EmfOutput ;
723+ const secondMetricsJson = JSON . parse (
724+ consoleSpy . mock . calls [ 1 ] [ 0 ]
725+ ) as EmfOutput ;
726+
727+ // The first batch of values should be an array of size MAX_METRIC_VALUES_SIZE
728+ expect ( firstMetricsJson [ metricName ] ) . toHaveLength ( MAX_METRIC_VALUES_SIZE ) ;
729+ // The second should be a single value (the last value added, which is 100 given we start from 0)
730+ expect ( secondMetricsJson [ metricName ] ) . toEqual ( 100 ) ;
731+ } ) ;
732+
704733 test ( 'it should not publish metrics if stored metrics count has not reached max metric size threshold' , ( ) => {
705734 // Prepare
706735 const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
0 commit comments