@@ -16,6 +16,7 @@ import {
16
16
DEFAULT_NAMESPACE ,
17
17
MAX_DIMENSION_COUNT ,
18
18
MAX_METRICS_SIZE ,
19
+ MAX_METRIC_VALUES_SIZE ,
19
20
} from '../../src/constants' ;
20
21
import { setupDecoratorLambdaHandler } from '../helpers/metricsUtils' ;
21
22
import {
@@ -701,6 +702,34 @@ describe('Class: Metrics', () => {
701
702
) ;
702
703
} ) ;
703
704
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
+
704
733
test ( 'it should not publish metrics if stored metrics count has not reached max metric size threshold' , ( ) => {
705
734
// Prepare
706
735
const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
0 commit comments