6262
6363import org .junit .jupiter .api .BeforeEach ;
6464import org .junit .jupiter .api .Test ;
65+ import org .junit .jupiter .params .ParameterizedTest ;
66+ import org .junit .jupiter .params .provider .ValueSource ;
6567import static org .awaitility .Awaitility .await ;
6668import static org .hamcrest .Matchers .lessThanOrEqualTo ;
6769import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
@@ -216,6 +218,7 @@ void setUp() {
216218 lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (60L );
217219 prometheusSinkConfig = mock (PrometheusSinkConfiguration .class );
218220 when (prometheusSinkConfig .getMaxRetries ()).thenReturn (5 );
221+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (0 ));
219222 when (prometheusSinkConfig .getSanitizeNames ()).thenReturn (false );
220223 when (prometheusSinkConfig .getUrl ()).thenReturn (remoteWriteUrl );
221224 when (prometheusSinkConfig .getContentType ()).thenReturn ("application/x-protobuf" );
@@ -292,16 +295,23 @@ private void getMetricsFromAMP(final String metricName, final String qs) throws
292295
293296 }
294297
295- @ Test
296- void TestSumMetrics () throws Exception {
298+ @ ParameterizedTest
299+ @ ValueSource (ints = {0 , 2 , 5 })
300+ void TestSumMetrics (final int window ) throws Exception {
301+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
302+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
297303 PrometheusSink sink = createObjectUnderTest ();
298304 long startTimeSeconds = testStartTime .getEpochSecond ();
299305 Instant time = Instant .now ();
300306 Collection <Record <Event >> records = getSumRecordList (NUM_RECORDS , sumMetricName , 0 );
301307 sink .doOutput (records );
308+ Thread .sleep (window *1000 );
302309
303310 await ().atMost (Duration .ofSeconds (60 ))
304311 .untilAsserted (() -> {
312+ if (window > 0 ) {
313+ sink .doOutput (Collections .emptyList ());
314+ }
305315 metricsInAMP = 0 ;
306316 Set <Double > expectedMetrics = new HashSet <>();
307317 for (Record record : records ) {
@@ -393,18 +403,25 @@ void TestSumMetricsFailuresWithDLQ() throws Exception {
393403 verify (eventHandle , times (NUM_RECORDS )).release (eq (true ));
394404 }
395405
396- @ Test
397- void TestSumMetricsFailuresWithoutDLQ () throws Exception {
406+ @ ParameterizedTest
407+ @ ValueSource (ints = {0 , 2 , 5 })
408+ void TestSumMetricsFailuresWithoutDLQ (final int window ) throws Exception {
409+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
410+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
398411 when (thresholdConfig .getMaxEvents ()).thenReturn (1 );
399412 PrometheusSink sink = createObjectUnderTest ();
400413
401414 long startTimeSeconds = testStartTime .getEpochSecond ();
402415 Instant time = Instant .now ();
403416 Collection <Record <Event >> records = getSumRecordList (NUM_RECORDS -1 , sumMetricName , 1 );
404417 sink .doOutput (records );
418+ Thread .sleep (window *1000 );
405419
406420 await ().atMost (Duration .ofSeconds (60 ))
407421 .untilAsserted (() -> {
422+ if (window > 0 ) {
423+ sink .doOutput (Collections .emptyList ());
424+ }
408425 metricsInAMP = 0 ;
409426 Set <Double > expectedMetrics = new HashSet <>();
410427 for (Record record : records ) {
@@ -467,16 +484,23 @@ private Collection<Record<Event>> getSumRecordList(int numberOfRecords, final St
467484 return records ;
468485 }
469486
470- @ Test
471- void TestGaugeMetrics () throws Exception {
487+ @ ParameterizedTest
488+ @ ValueSource (ints = {0 , 2 , 5 })
489+ void TestGaugeMetrics (final int window ) throws Exception {
490+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
491+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
472492
473493 PrometheusSink sink = createObjectUnderTest ();
474494 Collection <Record <Event >> records = getGaugeRecordList (NUM_RECORDS );
475495 sink .doOutput (records );
496+ Thread .sleep (window *1000 );
476497
477498 long startTimeSeconds = testStartTime .getEpochSecond ();
478499 await ().atMost (Duration .ofSeconds (60 ))
479500 .untilAsserted (() -> {
501+ if (window > 0 ) {
502+ sink .doOutput (Collections .emptyList ());
503+ }
480504 metricsInAMP = 0 ;
481505 long endTimeSeconds = Instant .now ().getEpochSecond ();
482506 getMetricsFromAMP (gaugeMetricName , "" );
@@ -504,18 +528,25 @@ void TestGaugeMetrics() throws Exception {
504528 verify (eventHandle , times (NUM_RECORDS )).release (eq (true ));
505529 }
506530
507- @ Test
508- void TestGaugeMetricsWithMaxRequestSizeLimitAndFlushTimeout () throws Exception {
531+ @ ParameterizedTest
532+ @ ValueSource (ints = {0 , 2 , 5 })
533+ void TestGaugeMetricsWithMaxRequestSizeLimitAndFlushTimeout (final int window ) throws Exception {
534+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
535+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
509536
510537 when (thresholdConfig .getMaxRequestSizeBytes ()).thenReturn (220L );
511538 lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (20L );
512539 PrometheusSink sink = createObjectUnderTest ();
513540 Collection <Record <Event >> records = getGaugeRecordList (NUM_RECORDS );
514541 sink .doOutput (records );
542+ Thread .sleep (window *1000 );
515543
516544 long startTimeSeconds = testStartTime .getEpochSecond ();
517545 await ().atMost (Duration .ofSeconds (60 ))
518546 .untilAsserted (() -> {
547+ if (window > 0 ) {
548+ sink .doOutput (Collections .emptyList ());
549+ }
519550 metricsInAMP = 0 ;
520551 sink .doOutput (Collections .emptyList ());
521552 long endTimeSeconds = Instant .now ().getEpochSecond ();
@@ -569,16 +600,23 @@ private Collection<Record<Event>> getGaugeRecordList(int numberOfRecords) {
569600 return records ;
570601 }
571602
572- @ Test
573- void TestSummaryMetrics () throws Exception {
603+ @ ParameterizedTest
604+ @ ValueSource (ints = {0 , 2 , 5 })
605+ void TestSummaryMetrics (final int window ) throws Exception {
606+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
607+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
574608
575609 PrometheusSink sink = createObjectUnderTest ();
576610 Collection <Record <Event >> records = getSummaryRecordList (NUM_RECORDS );
577611 sink .doOutput (records );
612+ Thread .sleep (window *1000 );
578613
579614 long startTimeSeconds = testStartTime .getEpochSecond ();
580615 await ().atMost (Duration .ofSeconds (60 ))
581616 .untilAsserted (() -> {
617+ if (window > 0 ) {
618+ sink .doOutput (Collections .emptyList ());
619+ }
582620 long endTimeSeconds = Instant .now ().getEpochSecond ()+10 ;
583621 metricsInAMP = 0 ;
584622 getMetricsFromAMP (summaryMetricName , "summary" );
@@ -674,16 +712,23 @@ private Collection<Record<Event>> getSummaryRecordList(int numberOfRecords) {
674712 return records ;
675713 }
676714
677- @ Test
678- void TestHistogramMetrics () throws Exception {
715+ @ ParameterizedTest
716+ @ ValueSource (ints = {0 , 2 , 5 })
717+ void TestHistogramMetrics (final int window ) throws Exception {
718+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
719+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
679720
680721 PrometheusSink sink = createObjectUnderTest ();
681722 Collection <Record <Event >> records = getHistogramRecordList (NUM_RECORDS );
682723 sink .doOutput (records );
724+ Thread .sleep (window *1000 );
683725
684726 long startTimeSeconds = testStartTime .getEpochSecond ();
685727 await ().atMost (Duration .ofSeconds (60 ))
686728 .untilAsserted (() -> {
729+ if (window > 0 ) {
730+ sink .doOutput (Collections .emptyList ());
731+ }
687732 metricsInAMP = 0 ;
688733 long endTimeSeconds = Instant .now ().getEpochSecond ()+10 ;
689734 getMetricsFromAMP (histogramMetricName , "histogram" );
@@ -734,16 +779,23 @@ void TestHistogramMetrics() throws Exception {
734779 }
735780
736781
737- @ Test
738- void TestExponentialHistogramMetrics () throws Exception {
782+ @ ParameterizedTest
783+ @ ValueSource (ints = {0 , 2 , 5 })
784+ void TestExponentialHistogramMetrics (final int window ) throws Exception {
785+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
786+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
739787
740788 PrometheusSink sink = createObjectUnderTest ();
741789 Collection <Record <Event >> records = getExponentialHistogramRecordList (NUM_RECORDS );
742790 sink .doOutput (records );
791+ Thread .sleep (window *1000 );
743792
744793 long startTimeSeconds = testStartTime .getEpochSecond ();
745794 await ().atMost (Duration .ofSeconds (60 ))
746795 .untilAsserted (() -> {
796+ if (window > 0 ) {
797+ sink .doOutput (Collections .emptyList ());
798+ }
747799 metricsInAMP = 0 ;
748800 long endTimeSeconds = Instant .now ().getEpochSecond ()+10 ;
749801 getMetricsFromAMP (exponentialHistogramMetricName , "exphistogram" );
@@ -797,8 +849,11 @@ void TestExponentialHistogramMetrics() throws Exception {
797849 verify (eventHandle , times (NUM_RECORDS )).release (eq (true ));
798850 }
799851
800- @ Test
801- public void TestMultipleMetrics () throws Exception {
852+ @ ParameterizedTest
853+ @ ValueSource (ints = {0 , 2 , 5 })
854+ public void TestMultipleMetrics (final int window ) throws Exception {
855+ lenient ().when (thresholdConfig .getFlushInterval ()).thenReturn (6L );
856+ when (prometheusSinkConfig .getOutOfOrderWindow ()).thenReturn (Duration .ofSeconds (window ));
802857 when (thresholdConfig .getMaxEvents ()).thenReturn (1 );
803858 long startTimeSeconds = testStartTime .getEpochSecond ();
804859 PrometheusSink sink = createObjectUnderTest ();
@@ -808,9 +863,13 @@ public void TestMultipleMetrics() throws Exception {
808863 records .addAll (getGaugeRecordList (NUM_RECORDS /5 ));
809864 records .addAll (getSumRecordList (NUM_RECORDS /5 , sumMetricName , 0 ));
810865 sink .doOutput (records );
866+ Thread .sleep (window *1000 );
811867
812868 await ().atMost (Duration .ofSeconds (60 ))
813869 .untilAsserted (() -> {
870+ if (window > 0 ) {
871+ sink .doOutput (Collections .emptyList ());
872+ }
814873
815874 int totalMetrics = 0 ;
816875 metricsInAMP = 0 ;
@@ -836,9 +895,8 @@ public void TestMultipleMetrics() throws Exception {
836895 assertThat (metricsInAMP , greaterThanOrEqualTo (1 ));
837896 totalMetrics += metricsInAMP ;
838897
839- assertThat ( totalMetrics , greaterThanOrEqualTo ( NUM_RECORDS ) );
898+ verify ( metricsSuccessCounter , times ( 10 )). increment ( 1 );
840899 });
841- verify (metricsSuccessCounter , times (10 )).increment (1 );
842900 }
843901
844902 private Collection <Record <Event >> getHistogramRecordList (int numberOfRecords ) {
0 commit comments