@@ -665,6 +665,88 @@ void output_will_flush_the_largest_group_until_below_aggregate_threshold_when_ag
665665 verify (s3ObjectsForceFlushedCounter , times (2 )).increment ();
666666 }
667667
668+ @ Test
669+ void flushAllRemainingGroups_when_no_groups_does_not_flush () {
670+ when (s3GroupManager .hasNoGroups ()).thenReturn (true );
671+
672+ final S3SinkService s3SinkService = createObjectUnderTest ();
673+ s3SinkService .flushAllRemainingGroups ();
674+
675+ verify (s3GroupManager ).hasNoGroups ();
676+ verify (s3GroupManager , never ()).getS3GroupEntries ();
677+ verify (s3ObjectsForceFlushedCounter , never ()).increment ();
678+ }
679+
680+ @ Test
681+ void flushAllRemainingGroups_when_groups_exist_flushes_all () throws IOException {
682+ when (s3GroupManager .hasNoGroups ()).thenReturn (false );
683+ when (s3GroupManager .getNumberOfGroups ()).thenReturn (2 );
684+
685+ final S3Group s3Group1 = mock (S3Group .class );
686+ final S3Group s3Group2 = mock (S3Group .class );
687+ final Buffer buffer1 = mock (Buffer .class );
688+ final Buffer buffer2 = mock (Buffer .class );
689+ final OutputStream outputStream1 = mock (OutputStream .class );
690+ final OutputStream outputStream2 = mock (OutputStream .class );
691+
692+ when (s3Group1 .getBuffer ()).thenReturn (buffer1 );
693+ when (s3Group1 .getOutputCodec ()).thenReturn (codec );
694+ when (buffer1 .getOutputStream ()).thenReturn (outputStream1 );
695+ when (buffer1 .getKey ()).thenReturn (UUID .randomUUID ().toString ());
696+ when (buffer1 .getEventCount ()).thenReturn (3 );
697+ when (buffer1 .getSize ()).thenReturn (100L );
698+ when (buffer1 .getDuration ()).thenReturn (Duration .ZERO );
699+
700+ when (s3Group2 .getBuffer ()).thenReturn (buffer2 );
701+ when (s3Group2 .getOutputCodec ()).thenReturn (codec );
702+ when (buffer2 .getOutputStream ()).thenReturn (outputStream2 );
703+ when (buffer2 .getKey ()).thenReturn (UUID .randomUUID ().toString ());
704+ when (buffer2 .getEventCount ()).thenReturn (5 );
705+ when (buffer2 .getSize ()).thenReturn (200L );
706+ when (buffer2 .getDuration ()).thenReturn (Duration .ZERO );
707+
708+ final CompletableFuture <Void > completedFuture = CompletableFuture .completedFuture (null );
709+ when (buffer1 .flushToS3 (any (Consumer .class ), any (Consumer .class ))).thenReturn (Optional .of (completedFuture ));
710+ when (buffer2 .flushToS3 (any (Consumer .class ), any (Consumer .class ))).thenReturn (Optional .of (completedFuture ));
711+
712+ when (s3GroupManager .getS3GroupEntries ()).thenReturn (List .of (s3Group1 , s3Group2 ));
713+ doNothing ().when (codec ).complete (any (OutputStream .class ));
714+
715+ final S3SinkService s3SinkService = createObjectUnderTest ();
716+
717+ try (final MockedStatic <CompletableFuture > completableFutureMockedStatic = mockStatic (CompletableFuture .class )) {
718+ final CompletableFuture <Void > mockCompletableFuture = mock (CompletableFuture .class );
719+ when (mockCompletableFuture .thenRun (any (Runnable .class ))).thenReturn (mockCompletableFuture );
720+ when (mockCompletableFuture .join ()).thenReturn (null );
721+ completableFutureMockedStatic .when (() -> CompletableFuture .allOf (any ())).thenReturn (mockCompletableFuture );
722+ s3SinkService .flushAllRemainingGroups ();
723+ }
724+
725+ verify (s3GroupManager ).hasNoGroups ();
726+ verify (s3GroupManager ).getNumberOfGroups ();
727+ verify (s3GroupManager ).getS3GroupEntries ();
728+ verify (s3GroupManager ).removeGroup (s3Group1 );
729+ verify (s3GroupManager ).removeGroup (s3Group2 );
730+ verify (codec , times (2 )).complete (any (OutputStream .class ));
731+ verify (buffer1 ).flushToS3 (any (Consumer .class ), any (Consumer .class ));
732+ verify (buffer2 ).flushToS3 (any (Consumer .class ), any (Consumer .class ));
733+ verify (s3ObjectsForceFlushedCounter , times (2 )).increment ();
734+ }
735+
736+ @ Test
737+ void flushAllRemainingGroups_when_empty_groups_list_does_not_increment_force_flush_counter () {
738+ when (s3GroupManager .hasNoGroups ()).thenReturn (false );
739+ when (s3GroupManager .getNumberOfGroups ()).thenReturn (0 );
740+ when (s3GroupManager .getS3GroupEntries ()).thenReturn (Collections .emptyList ());
741+
742+ final S3SinkService s3SinkService = createObjectUnderTest ();
743+ s3SinkService .flushAllRemainingGroups ();
744+
745+ verify (s3GroupManager ).hasNoGroups ();
746+ verify (s3GroupManager ).getS3GroupEntries ();
747+ verify (s3ObjectsForceFlushedCounter , never ()).increment ();
748+ }
749+
668750 private Collection <Record <Event >> generateRandomStringEventRecord () {
669751 return generateEventRecords (50 );
670752 }
0 commit comments