@@ -451,6 +451,48 @@ void invalidConfig() {
451451 .hasMessage ("executor" );
452452 }
453453
454+ @ Test
455+ void periodicExport_SequentialBatches () throws Exception {
456+ MetricExporter mockExporter = mock (MetricExporter .class );
457+ when (mockExporter .getAggregationTemporality (any ()))
458+ .thenReturn (AggregationTemporality .CUMULATIVE );
459+ when (mockExporter .flush ()).thenReturn (CompletableResultCode .ofSuccess ());
460+ when (mockExporter .shutdown ()).thenReturn (CompletableResultCode .ofSuccess ());
461+
462+ CompletableResultCode batch1Result = new CompletableResultCode ();
463+ CompletableResultCode batch2Result = CompletableResultCode .ofSuccess ();
464+
465+ // Configure mock to return pending for 1st call, success for 2nd
466+ when (mockExporter .export (any ())).thenReturn (batch1Result ).thenReturn (batch2Result );
467+
468+ PeriodicMetricReader reader =
469+ PeriodicMetricReader .builder (mockExporter )
470+ .setInterval (
471+ Duration .ofSeconds (Integer .MAX_VALUE )) // Long interval to prevent auto-trigger
472+ .setMaxExportBatchSize (3 )
473+ .build ();
474+ // Setup metrics that will result in 2 batches (we have 6 points in
475+ // LONG_POINT_LIST)
476+ when (collectionRegistration .collectAllMetrics ())
477+ .thenReturn (Collections .singletonList (METRIC_DATA ));
478+ reader .register (collectionRegistration );
479+
480+ // Trigger manual flush
481+ CompletableResultCode flushResult = reader .forceFlush ();
482+ // Verify that the first batch WAS exported
483+ verify (mockExporter , times (1 )).export (any ());
484+ // At this point, batch 1 is stuck waiting. Batch 2 should NOT be exported yet.
485+ // We verify that export was only called once in total so far.
486+ verify (mockExporter , times (1 )).export (any ());
487+ // Now we complete the first batch
488+ batch1Result .succeed ();
489+ // Verify that the second batch IS NOW exported
490+ verify (mockExporter , times (2 )).export (any ());
491+ // Ensure the flush operation completes successfully
492+ assertThat (flushResult .join (5 , TimeUnit .SECONDS ).isSuccess ()).isTrue ();
493+ reader .shutdown ();
494+ }
495+
454496 @ Test
455497 void stringRepresentation () {
456498 when (metricExporter .toString ()).thenReturn ("MockMetricExporter{}" );
0 commit comments