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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
