linghengqian commented on code in PR #2475: URL: https://github.com/apache/shardingsphere-elasticjob/pull/2475#discussion_r2330262786
########## 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) { Review Comment: - How about changing it to `} catch (final InterruptedException ex) {` ? - I'm surprised that Elasticjob's checkstyle configuration doesn't check for `final` keywords and variable names like ShardingSphere does. ########## 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: - If you're just verifying the asynchronous result of `job.shutdown();`, why use `pollDelay` to slow down the execution of the unit test? Why not use `Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() -> captured.get());`? - Or, do you need additional assertions for exceptions? I don't see any such assertions in your commit. ########## 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); Review Comment: - Why do local variables within functions need to be declared `final`? Is this a suggestion from GitHub Copilot? ########## 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); + } Review Comment: - Why can't these two unit tests be merged into one? Will merging them into the same unit test cause conflicts? Elasticjob unit tests are currently executed serially. What am I missing? ```java { testCaptureInterruptedException(1); testCaptureInterruptedException(2); } ``` -- 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