1111import org .apache .parquet .avro .AvroParquetReader ;
1212import org .apache .parquet .hadoop .ParquetReader ;
1313import org .junit .jupiter .api .BeforeEach ;
14+ import org .junit .jupiter .api .Test ;
1415import org .junit .jupiter .api .extension .ExtendWith ;
1516import org .junit .jupiter .params .ParameterizedTest ;
1617import org .junit .jupiter .params .provider .EnumSource ;
4142import java .time .Duration ;
4243import java .util .Optional ;
4344import java .util .UUID ;
45+ import java .util .concurrent .Executors ;
46+ import java .util .concurrent .ScheduledExecutorService ;
47+ import java .util .concurrent .TimeUnit ;
4448
49+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4550import static org .mockito .ArgumentMatchers .any ;
4651import static org .mockito .ArgumentMatchers .anyInt ;
4752import static org .mockito .ArgumentMatchers .anyLong ;
53+ import static org .mockito .ArgumentMatchers .eq ;
4854import static org .mockito .Mockito .RETURNS_DEEP_STUBS ;
4955import static org .mockito .Mockito .doThrow ;
5056import static org .mockito .Mockito .mock ;
@@ -203,7 +209,7 @@ void test_flush_failure_then_error_metric_updated(EngineType engineType) throws
203209 readerMockedStatic .when (() -> AvroParquetReader .<GenericRecord >builder (any (InputFile .class ), any ())).thenReturn (builder );
204210 bufferAccumulatorMockedStatic .when (() -> BufferAccumulator .create (any (Buffer .class ), anyInt (), any (Duration .class ))).thenReturn (bufferAccumulator );
205211
206- dataFileLoader .run ();
212+ assertThrows ( RuntimeException . class , () -> dataFileLoader .run () );
207213 }
208214
209215 verify (bufferAccumulator ).add (any (Record .class ));
@@ -218,6 +224,55 @@ void test_flush_failure_then_error_metric_updated(EngineType engineType) throws
218224 verify (exportRecordErrorCounter ).increment (1 );
219225 }
220226
227+ @ Test
228+ void test_run_schedules_lease_renewal () throws Exception {
229+ final String bucket = UUID .randomUUID ().toString ();
230+ final String key = UUID .randomUUID ().toString ();
231+ when (dataFilePartition .getBucket ()).thenReturn (bucket );
232+ when (dataFilePartition .getKey ()).thenReturn (key );
233+ final DataFileProgressState progressState = mock (DataFileProgressState .class , RETURNS_DEEP_STUBS );
234+ when (dataFilePartition .getProgressState ()).thenReturn (Optional .of (progressState ));
235+ when (progressState .getEngineType ()).thenReturn (EngineType .MYSQL .toString ());
236+
237+ InputStream inputStream = mock (InputStream .class );
238+ when (s3ObjectReader .readFile (bucket , key )).thenReturn (inputStream );
239+
240+ final String randomString = UUID .randomUUID ().toString ();
241+ final BaseEventBuilder <Event > eventBuilder = mock (EventBuilder .class , RETURNS_DEEP_STUBS );
242+ final Event event = mock (Event .class );
243+ when (eventFactory .eventBuilder (any ())).thenReturn (eventBuilder );
244+ when (eventBuilder .withEventType (any ()).withData (any ()).build ()).thenReturn (event );
245+ when (event .toJsonString ()).thenReturn (randomString );
246+ when (recordConverter .convert (any (), any (), any (), any (), any (), any (), anyLong (), anyLong (), any (), any ())).thenReturn (event );
247+
248+ AvroParquetReader .Builder <GenericRecord > builder = mock (AvroParquetReader .Builder .class );
249+ ParquetReader <GenericRecord > parquetReader = mock (ParquetReader .class );
250+ BufferAccumulator <Record <Event >> bufferAccumulator = mock (BufferAccumulator .class );
251+ when (builder .build ()).thenReturn (parquetReader );
252+ when (parquetReader .read ()).thenReturn (mock (GenericRecord .class , RETURNS_DEEP_STUBS ), (GenericRecord ) null );
253+
254+ try (MockedStatic <AvroParquetReader > readerMockedStatic = mockStatic (AvroParquetReader .class );
255+ MockedStatic <BufferAccumulator > bufferAccumulatorMockedStatic = mockStatic (BufferAccumulator .class );
256+ MockedStatic <Executors > executorsMockedStatic = mockStatic (Executors .class )) {
257+
258+ ScheduledExecutorService mockScheduler = mock (ScheduledExecutorService .class );
259+ executorsMockedStatic .when (Executors ::newSingleThreadScheduledExecutor ).thenReturn (mockScheduler );
260+
261+ readerMockedStatic .when (() -> AvroParquetReader .<GenericRecord >builder (any (InputFile .class ), any ())).thenReturn (builder );
262+ bufferAccumulatorMockedStatic .when (() -> BufferAccumulator .create (any (Buffer .class ), anyInt (), any (Duration .class ))).thenReturn (bufferAccumulator );
263+
264+ DataFileLoader dataFileLoader = createObjectUnderTest ();
265+ dataFileLoader .run ();
266+
267+ verify (mockScheduler ).scheduleAtFixedRate (
268+ any (Runnable .class ),
269+ eq (DataFileLoader .LEASE_RENEWAL_INTERVAL .toMillis ()),
270+ eq (DataFileLoader .LEASE_RENEWAL_INTERVAL .toMillis ()),
271+ eq (TimeUnit .MILLISECONDS ));
272+ verify (mockScheduler ).shutdownNow ();
273+ }
274+ }
275+
221276 private DataFileLoader createObjectUnderTest () {
222277 final InputCodec codec = new ParquetInputCodec (eventFactory );
223278 return DataFileLoader .create (dataFilePartition , codec , buffer , s3ObjectReader , recordConverter ,
0 commit comments