marmot-z commented on code in PR #2475:
URL: 
https://github.com/apache/shardingsphere-elasticjob/pull/2475#discussion_r2333723853


##########
test/native/src/test/java/org/apache/shardingsphere/elasticjob/test/natived/it/operation/JavaTest.java:
##########
@@ -305,4 +308,44 @@ void testShardingStatisticsAPI() {
         });
         job.shutdown();
     }
+
+    @Test
+    void 
testGivenOneShardingCountWhenShutdownThenTaskCanCaptureInterruptedException() 
throws Exception {
+        testCaptureInterruptedException(1);
+    }
+
+    @Test
+    void 
testGivenOneMoreShardingCountWhenShutdownThenTaskCanCaptureInterruptedException()
 throws Exception {
+        testCaptureInterruptedException(2);
+    }
+
+    private void testCaptureInterruptedException(int shardingTotalCount) 
throws Exception {
+        String jobName = "testTaskCaptureInterruptedTask";
+        final AtomicBoolean captured = new AtomicBoolean(false);
+        LocalTime tenSecondsLater = LocalTime.now().plusSeconds(2);
+        String cronExpression = String.format("%d %d %d * * ?", 
tenSecondsLater.getSecond(), tenSecondsLater.getMinute(), 
tenSecondsLater.getHour());
+        SimpleJob captureInterruptedTask = shardingContext -> {
+            try {
+                while (true) {
+                    if (Thread.currentThread().isInterrupted()) {
+                        captured.set(true);
+                        break;
+                    }
+                    System.out.println("Running...");
+                    Thread.sleep(100);
+                }
+            } catch (InterruptedException e) {
+                captured.set(true);
+                Thread.currentThread().interrupt();
+            }
+        };
+        ScheduleJobBootstrap job = new ScheduleJobBootstrap(firstRegCenter, 
captureInterruptedTask,
+                JobConfiguration.newBuilder(jobName, shardingTotalCount)
+                        .cron(cronExpression)
+                        .build());
+        job.schedule();
+        Awaitility.await().pollDelay(4L, TimeUnit.SECONDS).until(() -> true);
+        job.shutdown();
+        Awaitility.await().pollDelay(1L, TimeUnit.SECONDS).untilAsserted(() -> 
assertThat(captured.get(), is(true)));

Review Comment:
   I think declaring `captured` as an AtomicBoolean field is sufficient. As 
long as it becomes true, it means that the business code can capture the 
interrupt, which is in line with expectations. 
   
   However, I am still not sure what the problem is here. Is it that the 
`Awaitility` API is used incorrectly, the waiting time is set too long, or the 
logic of this test code is not clear? 
   1. If the `Awaitility` API is used incorrectly, please give me some tips. 
   2. If the waiting time is set too long, I will try to shorten it. 
   3. If the test code logic is unclear, please point out the unclear part.



-- 
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: notifications-unsubscr...@shardingsphere.apache.org

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

Reply via email to