浏览代码

优化测试

zhouhao 7 年之前
父节点
当前提交
d8fa3a0067

+ 7 - 5
hsweb-system/hsweb-system-schedule/hsweb-system-schedule-starter/src/test/java/org/hswebframework/web/schedule/test/DynamicScheduleTests.java

@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicLong;
 
 
 /**
 /**
@@ -25,12 +26,12 @@ public class DynamicScheduleTests extends SimpleWebApplicationTests {
     private ScheduleJobService scheduleJobService;
     private ScheduleJobService scheduleJobService;
 
 
 
 
-    public static final AtomicLong counter=new AtomicLong();
+    public static final CountDownLatch counter=new CountDownLatch(1);
+    public static final AtomicLong value=new AtomicLong();
     private String id;
     private String id;
 
 
     @Before
     @Before
     public void initJob() throws InterruptedException {
     public void initJob() throws InterruptedException {
-        Thread.sleep(5000);
         id = scheduleJobService.insert(createJob());
         id = scheduleJobService.insert(createJob());
         scheduleJobService.enable(id);
         scheduleJobService.enable(id);
     }
     }
@@ -41,7 +42,8 @@ public class DynamicScheduleTests extends SimpleWebApplicationTests {
         entity.setType("test");
         entity.setType("test");
         entity.setLanguage("javascript");
         entity.setLanguage("javascript");
         entity.setScript("" +
         entity.setScript("" +
-                "org.hswebframework.web.schedule.test.DynamicScheduleTests.counter.incrementAndGet()\n" +
+                "org.hswebframework.web.schedule.test.DynamicScheduleTests.value.incrementAndGet();\n" +
+                "org.hswebframework.web.schedule.test.DynamicScheduleTests.counter.countDown();\n" +
                 "java.lang.System.out.println('job running...')");
                 "java.lang.System.out.println('job running...')");
         entity.setQuartzConfig("{\"type\":\"cron\",\"config\":\"0/1 * * * * ?\"}");
         entity.setQuartzConfig("{\"type\":\"cron\",\"config\":\"0/1 * * * * ?\"}");
         return entity;
         return entity;
@@ -49,7 +51,7 @@ public class DynamicScheduleTests extends SimpleWebApplicationTests {
 
 
     @Test
     @Test
     public void testCreateJob() throws InterruptedException {
     public void testCreateJob() throws InterruptedException {
-        Thread.sleep(20000);
-        Assert.assertTrue(counter.get()>0);
+        counter.await();
+        Assert.assertTrue(value.get()>0);
     }
     }
 }
 }

+ 5 - 3
hsweb-system/hsweb-system-schedule/hsweb-system-schedule-starter/src/test/java/org/hswebframework/web/schedule/test/ScheduleTests.java

@@ -7,6 +7,7 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.scheduling.annotation.Scheduled;
 
 
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicInteger;
 
 
 /**
 /**
@@ -20,16 +21,17 @@ public class ScheduleTests extends SimpleWebApplicationTests {
 
 
     AtomicInteger counter = new AtomicInteger();
     AtomicInteger counter = new AtomicInteger();
 
 
+    CountDownLatch countDownLatch = new CountDownLatch(1);
+
     @Scheduled(cron = "0/1 * * * * ?")
     @Scheduled(cron = "0/1 * * * * ?")
     public void quartzTest() {
     public void quartzTest() {
-        System.out.println(1234);
         counter.incrementAndGet();
         counter.incrementAndGet();
+        countDownLatch.countDown();
     }
     }
 
 
     @Test
     @Test
     public void test() throws InterruptedException {
     public void test() throws InterruptedException {
-        Thread.sleep(10000);
+        countDownLatch.await();
         Assert.assertTrue(counter.get() > 0);
         Assert.assertTrue(counter.get() > 0);
-        System.out.println(counter.get());
     }
     }
 }
 }