Skip to content

Commit 9385b83

Browse files
committed
Wrote tests and ensuring things run on JDK-21. But some other test fails in JDK-21.
Signed-off-by: noga <[email protected]>
1 parent dab4f52 commit 9385b83

File tree

1 file changed

+82
-6
lines changed

1 file changed

+82
-6
lines changed

quartz/src/test/java/org/quartz/VirtualThreadedSchedulerTest.java

+82-6
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@
1515
*/
1616
package org.quartz;
1717

18+
import org.junit.Assert;
1819
import org.junit.Assume;
1920
import org.junit.BeforeClass;
20-
import org.junit.runner.RunWith;
21+
import org.junit.Test;
2122
import org.quartz.impl.DirectSchedulerFactory;
2223
import org.quartz.impl.SchedulerRepository;
23-
import org.quartz.impl.jdbcjobstore.JdbcQuartzTestUtilities;
24-
import org.quartz.impl.jdbcjobstore.JobStoreTX;
2524
import org.quartz.simpl.RAMJobStore;
26-
import org.quartz.simpl.SimpleThreadPool;
2725
import org.quartz.simpl.SimpleVirtualThreadPool;
28-
import org.quartz.spi.JobStore;
2926

30-
import java.sql.SQLException;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.concurrent.CyclicBarrier;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.atomic.AtomicBoolean;
33+
34+
import static org.junit.Assert.assertFalse;
35+
import static org.quartz.JobBuilder.newJob;
36+
import static org.quartz.TriggerBuilder.newTrigger;
3137

3238
public class VirtualThreadedSchedulerTest extends AbstractSchedulerTest {
3339

@@ -42,4 +48,74 @@ protected Scheduler createScheduler(String name, int threadPoolSize) throws Sche
4248
DirectSchedulerFactory.getInstance().createScheduler(schedulerName, "AUTO", new SimpleVirtualThreadPool(), new RAMJobStore());
4349
return SchedulerRepository.getInstance().lookup(schedulerName);
4450
}
51+
52+
public static class TimeAdjustedJob implements Job {
53+
54+
Throwable err = null ;
55+
56+
@Override
57+
public void execute(JobExecutionContext context) {
58+
59+
try {
60+
SchedulerContext schedulerContext = context.getScheduler().getContext();
61+
schedulerContext.put("JOB_THREAD", Thread.currentThread());
62+
schedulerContext.put("JOB_INSTANCE", this);
63+
long wait = (long) schedulerContext.get("WAIT_TIME");
64+
Thread.sleep(wait); // job keeps on waiting
65+
} catch (Throwable e) {
66+
err = e;
67+
}
68+
}
69+
}
70+
@Override
71+
@Test
72+
public void testShutdownWithoutWaitIsUnclean() throws Exception {
73+
Scheduler scheduler = createScheduler("testShutdownWithoutWaitIsUnclean", 8);
74+
75+
scheduler.getContext().put("WAIT_TIME", Long.MAX_VALUE);
76+
scheduler.start();
77+
scheduler.addJob(newJob().ofType(TimeAdjustedJob.class).withIdentity("job").storeDurably().build(), false);
78+
scheduler.scheduleJob(newTrigger().forJob("job").startNow().build());
79+
while (scheduler.getCurrentlyExecutingJobs().isEmpty()) {
80+
Thread.sleep(50);
81+
}
82+
scheduler.shutdown(false); // try doing it here..
83+
//At this point, we still would have the job running.. like forever...
84+
Thread taskThread = (Thread)scheduler.getContext().get("JOB_THREAD");
85+
Assert.assertTrue(taskThread.isAlive()); // this is what we test
86+
taskThread.interrupt(); // now we close it out
87+
Thread.sleep(50);
88+
TimeAdjustedJob job = (TimeAdjustedJob)scheduler.getContext().get("JOB_INSTANCE");
89+
Assert.assertTrue( job.err instanceof InterruptedException ); // this implies we have succeeded
90+
}
91+
92+
@Override
93+
@Test
94+
public void testShutdownWithWaitIsClean() throws Exception {
95+
final AtomicBoolean shutdown = new AtomicBoolean(false);
96+
final Scheduler scheduler = createScheduler("testShutdownWithWaitIsClean", 8);
97+
scheduler.getContext().put("WAIT_TIME", 1500L);
98+
scheduler.start();
99+
scheduler.addJob(newJob().ofType(TimeAdjustedJob.class).withIdentity("job").storeDurably().build(), false);
100+
scheduler.scheduleJob(newTrigger().forJob("job").startNow().build());
101+
while (scheduler.getCurrentlyExecutingJobs().isEmpty()) {
102+
Thread.sleep(50);
103+
}
104+
105+
Thread t = new Thread(() -> {
106+
try {
107+
scheduler.shutdown(true);
108+
shutdown.set(true);
109+
//At this point, we still would not have the job running..
110+
Thread taskThread = (Thread)scheduler.getContext().get("JOB_THREAD");
111+
assertFalse(taskThread.isAlive());
112+
113+
} catch (SchedulerException ex) {
114+
throw new RuntimeException(ex);
115+
}
116+
});
117+
t.start();
118+
t.join();
119+
Assert.assertTrue( shutdown.get() );
120+
}
45121
}

0 commit comments

Comments
 (0)