15
15
*/
16
16
package org .quartz ;
17
17
18
+ import org .junit .Assert ;
18
19
import org .junit .Assume ;
19
20
import org .junit .BeforeClass ;
20
- import org .junit .runner . RunWith ;
21
+ import org .junit .Test ;
21
22
import org .quartz .impl .DirectSchedulerFactory ;
22
23
import org .quartz .impl .SchedulerRepository ;
23
- import org .quartz .impl .jdbcjobstore .JdbcQuartzTestUtilities ;
24
- import org .quartz .impl .jdbcjobstore .JobStoreTX ;
25
24
import org .quartz .simpl .RAMJobStore ;
26
- import org .quartz .simpl .SimpleThreadPool ;
27
25
import org .quartz .simpl .SimpleVirtualThreadPool ;
28
- import org .quartz .spi .JobStore ;
29
26
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 ;
31
37
32
38
public class VirtualThreadedSchedulerTest extends AbstractSchedulerTest {
33
39
@@ -42,4 +48,74 @@ protected Scheduler createScheduler(String name, int threadPoolSize) throws Sche
42
48
DirectSchedulerFactory .getInstance ().createScheduler (schedulerName , "AUTO" , new SimpleVirtualThreadPool (), new RAMJobStore ());
43
49
return SchedulerRepository .getInstance ().lookup (schedulerName );
44
50
}
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
+ }
45
121
}
0 commit comments