You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "linghengqian (via GitHub)" <gi...@apache.org> on 2025/09/11 05:40:09 UTC

[PR] Refactor test changes for `#2475` [shardingsphere-elasticjob]

linghengqian opened a new pull request, #2477:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/2477

   For #2475 .
   
   Changes proposed in this pull request:
   - Refactor test changes for `#2475`. It seems strange to put the key unit tests in the related test module of the GraalVM Native Image, because the unit tests of that module are not always executed by CI.
   


-- 
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-unsubscribe@shardingsphere.apache.org

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


Re: [PR] Refactor test changes for `#2475` [shardingsphere-elasticjob]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2477:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/2477#discussion_r2343307386


##########
bootstrap/src/test/java/org/apache/shardingsphere/elasticjob/bootstrap/type/ScheduleJobBootstrapTest.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.elasticjob.bootstrap.type;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.curator.CuratorZookeeperClient;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.curator.test.TestingServer;
+import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
+import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
+import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
+import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
+import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.time.LocalTime;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+@Slf4j
+class ScheduleJobBootstrapTest {
+    
+    private TestingServer testingServer;
+    
+    private CoordinatorRegistryCenter regCenter;
+    
+    @BeforeEach
+    void beforeEach() throws Exception {
+        testingServer = new TestingServer();
+        try (
+                CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(),
+                        60000, 500, null,
+                        new ExponentialBackoffRetry(500, 3, 1500))) {
+            client.start();
+            Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(client::isConnected);
+        }
+        regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(testingServer.getConnectString(), "elasticjob-test"));
+        regCenter.init();
+    }
+    
+    @AfterEach
+    void afterEach() throws IOException {
+        regCenter.close();
+        testingServer.close();
+    }
+    
+    @Test
+    void testWhenShutdownThenTaskCanCaptureInterruptedException() {
+        testCaptureInterruptedException(1);
+        testCaptureInterruptedException(2);
+    }
+    
+    @SuppressWarnings({"InfiniteLoopStatement", "BusyWait"})
+    private void testCaptureInterruptedException(final int shardingTotalCount) {
+        String jobName = "testTaskCaptureInterruptedTask" + shardingTotalCount;
+        AtomicBoolean captured = new AtomicBoolean(false);
+        AtomicBoolean running = new AtomicBoolean(false);
+        LocalTime oneSecondLater = LocalTime.now().plusSeconds(2L);
+        String cronExpression = String.format("%d %d %d * * ?", oneSecondLater.getSecond(), oneSecondLater.getMinute(), oneSecondLater.getHour());
+        SimpleJob captureInterruptedTask = shardingContext -> {
+            try {
+                running.set(true);
+                while (true) {
+                    Thread.sleep(100L);
+                }
+            } catch (final InterruptedException ex) {
+                captured.set(true);
+                Thread.currentThread().interrupt();
+            }
+        };
+        ScheduleJobBootstrap job = new ScheduleJobBootstrap(regCenter, captureInterruptedTask, JobConfiguration.newBuilder(jobName, shardingTotalCount).cron(cronExpression).build());
+        job.schedule();
+        Awaitility.await().atMost(10L, TimeUnit.SECONDS).ignoreExceptions().until(running::get);
+        job.shutdown();
+        Awaitility.await().atMost(10L, TimeUnit.SECONDS).ignoreExceptions().until(captured::get);

Review Comment:
   [nitpick] The test logic is simplified compared to the original version but may be less robust. The original implementation checked for thread interruption status and had a proper loop termination condition, while this version relies solely on InterruptedException catching. Consider adding a timeout or interruption check in the while loop to ensure the test behaves predictably.



-- 
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-unsubscribe@shardingsphere.apache.org

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


Re: [PR] Refactor test changes for `#2475` [shardingsphere-elasticjob]

Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu merged PR #2477:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/2477


-- 
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-unsubscribe@shardingsphere.apache.org

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


Re: [PR] Refactor test changes for `#2475` [shardingsphere-elasticjob]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2477:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/2477#issuecomment-3283590587

   ## [Codecov](https://app.codecov.io/gh/apache/shardingsphere-elasticjob/pull/2477?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :white_check_mark: Project coverage is 79.47%. Comparing base ([`a8ba448`](https://app.codecov.io/gh/apache/shardingsphere-elasticjob/commit/a8ba448927d7e237ecece9516634a44d822a1b11?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`0677cca`](https://app.codecov.io/gh/apache/shardingsphere-elasticjob/commit/0677ccaf059ebc06adc2c6fb7e5faca1754635e8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 63 commits behind head on master.
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff              @@
   ##             master    #2477      +/-   ##
   ============================================
   - Coverage     80.10%   79.47%   -0.64%     
   - Complexity     1113     1127      +14     
   ============================================
     Files           204      208       +4     
     Lines          3821     3907      +86     
     Branches        445      465      +20     
   ============================================
   + Hits           3061     3105      +44     
   - Misses          570      602      +32     
   - Partials        190      200      +10     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/shardingsphere-elasticjob/pull/2477?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


-- 
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-unsubscribe@shardingsphere.apache.org

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


Re: [PR] Refactor test changes for `#2475` [shardingsphere-elasticjob]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2477:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/2477#discussion_r2343443365


##########
bootstrap/src/test/java/org/apache/shardingsphere/elasticjob/bootstrap/type/ScheduleJobBootstrapTest.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.elasticjob.bootstrap.type;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.curator.CuratorZookeeperClient;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.curator.test.TestingServer;
+import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
+import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
+import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
+import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
+import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.quartz.core.JobRunShell;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.time.LocalTime;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+
+@Slf4j
+class ScheduleJobBootstrapTest {
+    
+    private TestingServer testingServer;
+    
+    private CoordinatorRegistryCenter regCenter;
+    
+    @BeforeEach
+    void beforeEach() throws Exception {
+        testingServer = new TestingServer();
+        try (
+                CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(),
+                        60000, 500, null,
+                        new ExponentialBackoffRetry(500, 3, 1500))) {
+            client.start();
+            Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(client::isConnected);
+        }
+        regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(testingServer.getConnectString(), "elasticjob-test"));
+        regCenter.init();
+    }
+    
+    @AfterEach
+    void afterEach() throws IOException {
+        regCenter.close();
+        testingServer.close();
+    }
+    
+    @Test
+    void testWhenShutdownThenTaskCanCaptureInterruptedException() {
+        Logger jobRunShell = (Logger) LoggerFactory.getLogger(JobRunShell.class.getName());
+        Logger errorLogger = (Logger) LoggerFactory.getLogger("org.quartz.core.ErrorLogger");
+        Level originJobRunShellLevel = jobRunShell.getLevel();
+        Level originErrorLoggerLevel = errorLogger.getLevel();
+        try {
+            jobRunShell.setLevel(Level.OFF);
+            errorLogger.setLevel(Level.OFF);
+            testCaptureInterruptedException(1);
+            testCaptureInterruptedException(2);
+        } finally {
+            jobRunShell.setLevel(originJobRunShellLevel);
+            errorLogger.setLevel(originErrorLoggerLevel);
+        }
+    }
+    
+    @SuppressWarnings({"InfiniteLoopStatement", "BusyWait"})

Review Comment:
   The infinite loop and busy wait suppressions indicate potential code smell. Consider refactoring the test logic to avoid the infinite loop by using a controlled condition variable instead of 'while(true)'.



-- 
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-unsubscribe@shardingsphere.apache.org

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