guozhangwang commented on code in PR #12279:
URL: https://github.com/apache/kafka/pull/12279#discussion_r899252040


##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/DefaultStateUpdaterTest.java:
##########
@@ -603,6 +638,77 @@ public void shouldDrainFailedTasksAndExceptions() throws 
Exception {
         verifyDrainingExceptionsAndFailedTasks(expectedExceptionAndTasks2, 
expectedExceptionAndTasks3, expectedExceptionAndTasks4);
     }
 
+    @Test
+    public void shouldAutoCommitTasksOnInterval() throws Exception {
+        final StreamTask task1 = 
createActiveStatefulTaskInStateRestoring(TASK_0_0, 
Collections.singletonList(TOPIC_PARTITION_A_0));
+        final StreamTask task2 = 
createActiveStatefulTaskInStateRestoring(TASK_0_2, 
Collections.singletonList(TOPIC_PARTITION_B_0));
+        final StandbyTask task3 = createStandbyTaskInStateRunning(TASK_1_0, 
Collections.singletonList(TOPIC_PARTITION_C_0));
+        final StandbyTask task4 = createStandbyTaskInStateRunning(TASK_1_1, 
Collections.singletonList(TOPIC_PARTITION_D_0));
+        when(changelogReader.completedChangelogs())
+                .thenReturn(Collections.emptySet());
+        when(changelogReader.allChangelogsCompleted())
+                .thenReturn(false);
+
+        stateUpdater.add(task1);
+        stateUpdater.add(task2);
+        stateUpdater.add(task3);
+        stateUpdater.add(task4);
+
+        sleep(COMMIT_INTERVAL);
+
+        verifyExceptionsAndFailedTasks();
+        verifyCommitTasks(false, task1, task2, task3, task4);
+    }
+
+    @Test
+    public void shouldNotAutoCommitTasksIfIntervalNotElapsed() throws 
Exception {
+        final StreamsConfig config = new 
StreamsConfig(configProps(Integer.MAX_VALUE));
+        final DefaultStateUpdater stateUpdater = new 
DefaultStateUpdater(config, changelogReader, offsetResetter, Time.SYSTEM);
+
+        try {
+            final StreamTask task1 = 
createActiveStatefulTaskInStateRestoring(TASK_0_0, 
Collections.singletonList(TOPIC_PARTITION_A_0));
+            final StreamTask task2 = 
createActiveStatefulTaskInStateRestoring(TASK_0_2, 
Collections.singletonList(TOPIC_PARTITION_B_0));
+            final StandbyTask task3 = 
createStandbyTaskInStateRunning(TASK_1_0, 
Collections.singletonList(TOPIC_PARTITION_C_0));
+            final StandbyTask task4 = 
createStandbyTaskInStateRunning(TASK_1_1, 
Collections.singletonList(TOPIC_PARTITION_D_0));
+            when(changelogReader.completedChangelogs())
+                    .thenReturn(Collections.emptySet());
+            when(changelogReader.allChangelogsCompleted())
+                    .thenReturn(false);
+
+            stateUpdater.add(task1);
+            stateUpdater.add(task2);
+            stateUpdater.add(task3);
+            stateUpdater.add(task4);
+
+            verifyNotCommitTasks(task1, task2, task3, task4);
+        } finally {
+            stateUpdater.shutdown(Duration.ofMinutes(1));
+        }
+    }
+
+    private void verifyCommitTasks(final boolean enforceCheckpoint, final 
Task... tasks) throws Exception {
+        waitForCondition(
+                () -> {
+                    for (final Task task : tasks) {
+                        verify(task, atLeast(1)).prepareCommit();
+                        verify(task, atLeast(1)).postCommit(enforceCheckpoint);
+                    }
+
+                    return true;
+                },
+                VERIFICATION_TIMEOUT,

Review Comment:
   I tried it a bit but since we are using a system test here using exact 
commit interval could cause flakiness; while using a larger value say `commit 
interval * 2` is safe enough I felt it would be similar to just using a longer 
value as `VERIFICATION_TIMEOUT` here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to