You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/07/22 05:40:52 UTC
[shardingsphere-elasticjob] branch master updated: Add test case
for DaemonTaskScheduler.init and shutdown (#1230) (#1237)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 67a4339 Add test case for DaemonTaskScheduler.init and shutdown (#1230) (#1237)
67a4339 is described below
commit 67a43395d783f7225a246d97c474dcd4dd25adee
Author: Tboy <gu...@immomo.com>
AuthorDate: Wed Jul 22 13:40:44 2020 +0800
Add test case for DaemonTaskScheduler.init and shutdown (#1230) (#1237)
---
.../executor/prod/DaemonTaskSchedulerTest.java | 41 ++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/elasticjob-cloud/elasticjob-cloud-executor/src/test/java/org/apache/shardingsphere/elasticjob/cloud/executor/prod/DaemonTaskSchedulerTest.java b/elasticjob-cloud/elasticjob-cloud-executor/src/test/java/org/apache/shardingsphere/elasticjob/cloud/executor/prod/DaemonTaskSchedulerTest.java
index 0f8ac10..b54ed91 100755
--- a/elasticjob-cloud/elasticjob-cloud-executor/src/test/java/org/apache/shardingsphere/elasticjob/cloud/executor/prod/DaemonTaskSchedulerTest.java
+++ b/elasticjob-cloud/elasticjob-cloud-executor/src/test/java/org/apache/shardingsphere/elasticjob/cloud/executor/prod/DaemonTaskSchedulerTest.java
@@ -17,10 +17,12 @@
package org.apache.shardingsphere.elasticjob.cloud.executor.prod;
+import lombok.SneakyThrows;
import org.apache.mesos.ExecutorDriver;
import org.apache.mesos.Protos.TaskID;
import org.apache.mesos.Protos.TaskState;
import org.apache.mesos.Protos.TaskStatus;
+import org.apache.shardingsphere.elasticjob.api.ElasticJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.api.listener.ShardingContexts;
import org.apache.shardingsphere.elasticjob.cloud.executor.prod.DaemonTaskScheduler.DaemonJob;
@@ -34,6 +36,11 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.quartz.JobExecutionContext;
+import java.lang.reflect.Field;
+import java.util.concurrent.ConcurrentHashMap;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -52,6 +59,12 @@ public final class DaemonTaskSchedulerTest {
@Mock
private ShardingContexts shardingContexts;
+ @Mock
+ private JobConfiguration jobConfig;
+
+ @Mock
+ private ElasticJob elasticJob;
+
private TaskID taskId = TaskID.newBuilder().setValue(String.format("%s@-@0@-@%s@-@fake_slave_id@-@0", "test_job", ExecutionType.READY)).build();
private DaemonJob daemonJob;
@@ -95,4 +108,32 @@ public final class DaemonTaskSchedulerTest {
private JobConfiguration createJobConfiguration() {
return JobConfiguration.newBuilder("test_script_job", 3).cron("0/1 * * * * ?").jobErrorHandlerType("IGNORE").setProperty(ScriptJobProperties.SCRIPT_KEY, "echo test").build();
}
+
+ @Test
+ @SneakyThrows
+ public void assertInit() {
+ DaemonTaskScheduler scheduler = createScheduler();
+ scheduler.init();
+ Field field = DaemonTaskScheduler.class.getDeclaredField("RUNNING_SCHEDULERS");
+ field.setAccessible(true);
+ assertTrue(((ConcurrentHashMap) field.get(scheduler)).containsKey(taskId.getValue()));
+ }
+
+ @Test
+ @SneakyThrows
+ public void assertShutdown() {
+ DaemonTaskScheduler scheduler = createScheduler();
+ scheduler.init();
+ DaemonTaskScheduler.shutdown(taskId);
+ Field field = DaemonTaskScheduler.class.getDeclaredField("RUNNING_SCHEDULERS");
+ field.setAccessible(true);
+ assertFalse(((ConcurrentHashMap) field.get(scheduler)).containsKey(taskId.getValue()));
+ assertTrue(((ConcurrentHashMap) field.get(scheduler)).isEmpty());
+ }
+
+ private DaemonTaskScheduler createScheduler() {
+ when(jobConfig.getJobName()).thenReturn(taskId.getValue());
+ when(jobConfig.getCron()).thenReturn("0/1 * * * * ?");
+ return new DaemonTaskScheduler(elasticJob, "transient", jobConfig, jobFacade, executorDriver, taskId);
+ }
}