|
1 | 1 | package com.scalar.db.dataloader.core.dataimport.processor; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 7 | import static org.mockito.ArgumentMatchers.any; |
|
43 | 44 | import org.junit.jupiter.api.BeforeEach; |
44 | 45 | import org.junit.jupiter.api.Test; |
45 | 46 | import org.junit.jupiter.api.extension.ExtendWith; |
| 47 | +import org.mockito.ArgumentCaptor; |
46 | 48 | import org.mockito.Mock; |
47 | 49 | import org.mockito.junit.jupiter.MockitoExtension; |
48 | 50 |
|
@@ -256,6 +258,37 @@ void process_withShutdown_shouldShutdownExecutorsGracefully() { |
256 | 258 | assertEquals(3, processor.getProcessedChunksCount().get(), "All chunks should be processed"); |
257 | 259 | } |
258 | 260 |
|
| 261 | + @Test |
| 262 | + void process_withUnexpectedExceptionInTransaction_shouldHandleGracefully() |
| 263 | + throws TransactionException { |
| 264 | + // Arrange |
| 265 | + BufferedReader reader = new BufferedReader(new StringReader("test data")); |
| 266 | + when(params.getScalarDbMode()).thenReturn(ScalarDbMode.TRANSACTION); |
| 267 | + when(params.getDistributedTransactionManager()).thenReturn(distributedTransactionManager); |
| 268 | + when(distributedTransactionManager.start()).thenThrow(new RuntimeException("Unexpected error")); |
| 269 | + |
| 270 | + TestImportProcessor processor = new TestImportProcessor(params); |
| 271 | + processor.addListener(eventListener); |
| 272 | + |
| 273 | + // Act |
| 274 | + processor.process(2, 1, reader); |
| 275 | + |
| 276 | + // Assert |
| 277 | + verify(eventListener, times(1)).onAllDataChunksCompleted(); |
| 278 | + |
| 279 | + // Capture and verify the transaction batch result |
| 280 | + ArgumentCaptor<ImportTransactionBatchResult> resultCaptor = |
| 281 | + ArgumentCaptor.forClass(ImportTransactionBatchResult.class); |
| 282 | + verify(eventListener, times(1)).onTransactionBatchCompleted(resultCaptor.capture()); |
| 283 | + |
| 284 | + ImportTransactionBatchResult result = resultCaptor.getValue(); |
| 285 | + assertFalse(result.isSuccess()); |
| 286 | + assertEquals(0, result.getTransactionBatchId()); |
| 287 | + assertEquals(1, result.getDataChunkId()); |
| 288 | + assertTrue(result.getErrors().get(0).contains("Unexpected error: RuntimeException")); |
| 289 | + assertTrue(result.getErrors().get(0).contains("Unexpected error")); |
| 290 | + } |
| 291 | + |
259 | 292 | /** |
260 | 293 | * A simple implementation of ImportProcessor for testing purposes. This class is used to test the |
261 | 294 | * thread executor behavior in ImportProcessor. |
|
0 commit comments