|
30 | 30 | import static org.junit.jupiter.api.Assertions.assertEquals; |
31 | 31 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
32 | 32 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | +import static org.mockito.ArgumentMatchers.any; |
33 | 34 | import static org.mockito.ArgumentMatchers.eq; |
34 | 35 | import static org.mockito.BDDMockito.given; |
35 | 36 | import static org.mockito.Mockito.atLeast; |
| 37 | +import static org.mockito.Mockito.times; |
36 | 38 | import static org.mockito.Mockito.verify; |
37 | 39 | import static org.mockito.Mockito.verifyNoInteractions; |
38 | 40 | import static org.mockito.Mockito.when; |
@@ -216,4 +218,45 @@ void testRetryBackOffTriggeredWhenExceptionOccurred() throws InterruptedExceptio |
216 | 218 | // Crawler shouldn't be invoked in this case |
217 | 219 | verifyNoInteractions(crawler); |
218 | 220 | } |
| 221 | + |
| 222 | + @Test |
| 223 | + void testCompletePartitionWithAcknowledgements() throws InterruptedException { |
| 224 | + WorkerScheduler workerScheduler = new WorkerScheduler(pluginName, buffer, |
| 225 | + coordinator, sourceConfig, crawler, pluginMetrics, acknowledgementSetManager); |
| 226 | + WorkerScheduler spyWorkerScheduler = org.mockito.Mockito.spy(workerScheduler); |
| 227 | + |
| 228 | + when(sourceConfig.isAcknowledgments()).thenReturn(true); |
| 229 | + |
| 230 | + PaginationCrawlerWorkerProgressState mockProgressState = new PaginationCrawlerWorkerProgressState(); |
| 231 | + |
| 232 | + EnhancedSourcePartition mockPartition = org.mockito.Mockito.mock(EnhancedSourcePartition.class); |
| 233 | + when(mockPartition.getProgressState()).thenReturn(Optional.of(mockProgressState)); |
| 234 | + |
| 235 | + org.mockito.Mockito.doAnswer(invocation -> { |
| 236 | + coordinator.completePartition(mockPartition); |
| 237 | + return acknowledgementSet; |
| 238 | + }).when(spyWorkerScheduler).createAcknowledgementSet(any(EnhancedSourcePartition.class)); |
| 239 | + |
| 240 | + given(coordinator.acquireAvailablePartition(SaasSourcePartition.PARTITION_TYPE)) |
| 241 | + .willReturn(Optional.of(mockPartition)) |
| 242 | + .willReturn(Optional.empty()); |
| 243 | + |
| 244 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 245 | + executorService.submit(spyWorkerScheduler); |
| 246 | + |
| 247 | + Thread.sleep(500); |
| 248 | + executorService.shutdownNow(); |
| 249 | + |
| 250 | + verify(mockPartition, atLeast(1)).getProgressState(); |
| 251 | + verify(sourceConfig, atLeast(1)).isAcknowledgments(); |
| 252 | + |
| 253 | + // Verify that createAcknowledgementSet was called |
| 254 | + verify(spyWorkerScheduler, times(1)).createAcknowledgementSet(eq(mockPartition)); |
| 255 | + |
| 256 | + // Verify that crawler.executePartition was called with acknowledgement set |
| 257 | + verify(crawler, times(1)).executePartition(any(SaasWorkerProgressState.class), eq(buffer), eq(acknowledgementSet)); |
| 258 | + |
| 259 | + // Verify that coordinator.completePartition was called (from the acknowledgement callback with true) |
| 260 | + verify(coordinator, times(1)).completePartition(eq(mockPartition)); |
| 261 | + } |
219 | 262 | } |
0 commit comments