19
19
import java .util .Date ;
20
20
import java .util .concurrent .atomic .AtomicInteger ;
21
21
22
- import org .junit .Ignore ;
22
+ import org .junit .After ;
23
23
import org .junit .Rule ;
24
24
import org .junit .Test ;
25
25
import org .junit .rules .ExpectedException ;
36
36
import org .springframework .tests .Assume ;
37
37
import org .springframework .tests .TestGroup ;
38
38
39
- import static org .hamcrest .Matchers .both ;
40
- import static org .hamcrest .Matchers .greaterThan ;
41
- import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
42
- import static org .hamcrest .Matchers .lessThanOrEqualTo ;
43
- import static org .hamcrest .Matchers .startsWith ;
44
- import static org .junit .Assert .assertThat ;
39
+ import static org .hamcrest .Matchers .*;
40
+ import static org .junit .Assert .*;
45
41
46
42
/**
47
43
* Tests use of @EnableScheduling on @Configuration classes.
@@ -55,62 +51,151 @@ public class EnableSchedulingTests {
55
51
@ Rule
56
52
public final ExpectedException exception = ExpectedException .none ();
57
53
54
+ private AnnotationConfigApplicationContext ctx ;
55
+
56
+
57
+ @ After
58
+ public void tearDown () {
59
+ if (ctx != null ) {
60
+ ctx .close ();
61
+ }
62
+ }
63
+
58
64
59
65
@ Test
60
66
public void withFixedRateTask () throws InterruptedException {
61
67
Assume .group (TestGroup .PERFORMANCE );
62
68
63
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (FixedRateTaskConfig .class );
69
+ ctx = new AnnotationConfigApplicationContext (FixedRateTaskConfig .class );
64
70
65
71
Thread .sleep (100 );
66
72
assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThanOrEqualTo (10 ));
67
- ctx .close ();
68
73
}
69
74
75
+ @ Test
76
+ public void withSubclass () throws InterruptedException {
77
+ Assume .group (TestGroup .PERFORMANCE );
70
78
71
- @ Configuration
72
- @ EnableScheduling
73
- static class FixedRateTaskConfig {
79
+ ctx = new AnnotationConfigApplicationContext (FixedRateTaskConfigSubclass .class );
74
80
75
- @ Bean
76
- public AtomicInteger counter () {
77
- return new AtomicInteger ();
78
- }
81
+ Thread .sleep (100 );
82
+ assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThanOrEqualTo (10 ));
83
+ }
79
84
80
- @ Scheduled (fixedRate = 10 )
81
- public void task () {
82
- counter ().incrementAndGet ();
83
- }
85
+ @ Test
86
+ public void withExplicitScheduler () throws InterruptedException {
87
+ Assume .group (TestGroup .PERFORMANCE );
88
+
89
+ ctx = new AnnotationConfigApplicationContext (ExplicitSchedulerConfig .class );
90
+
91
+ Thread .sleep (100 );
92
+ assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThanOrEqualTo (10 ));
93
+ assertThat (ctx .getBean (ExplicitSchedulerConfig .class ).threadName , startsWith ("explicitScheduler-" ));
84
94
}
85
95
96
+ @ Test
97
+ public void withExplicitSchedulerAmbiguity_andSchedulingEnabled () {
98
+ // No exception raised as of 4.3, aligned with the behavior for @Async methods (SPR-14030)
99
+ ctx = new AnnotationConfigApplicationContext (AmbiguousExplicitSchedulerConfig .class );
100
+ }
86
101
87
102
@ Test
88
- public void withSubclass () throws InterruptedException {
103
+ public void withExplicitScheduledTaskRegistrar () throws InterruptedException {
89
104
Assume .group (TestGroup .PERFORMANCE );
90
105
91
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (FixedRateTaskConfigSubclass .class );
106
+ ctx = new AnnotationConfigApplicationContext (ExplicitScheduledTaskRegistrarConfig .class );
92
107
93
108
Thread .sleep (100 );
94
109
assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThanOrEqualTo (10 ));
95
- ctx .close ( );
110
+ assertThat ( ctx .getBean ( ExplicitScheduledTaskRegistrarConfig . class ). threadName , startsWith ( "explicitScheduler1" ) );
96
111
}
97
112
113
+ @ Test
114
+ public void withAmbiguousTaskSchedulers_butNoActualTasks () {
115
+ ctx = new AnnotationConfigApplicationContext (SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks .class );
116
+ }
98
117
99
- @ Configuration
100
- static class FixedRateTaskConfigSubclass extends FixedRateTaskConfig {
118
+ @ Test
119
+ public void withAmbiguousTaskSchedulers_andSingleTask () {
120
+ // No exception raised as of 4.3, aligned with the behavior for @Async methods (SPR-14030)
121
+ ctx = new AnnotationConfigApplicationContext (SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask .class );
101
122
}
102
123
124
+ @ Test
125
+ public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrarBean () throws InterruptedException {
126
+ Assume .group (TestGroup .PERFORMANCE );
127
+
128
+ ctx = new AnnotationConfigApplicationContext (
129
+ SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar .class );
130
+
131
+ Thread .sleep (100 );
132
+ assertThat (ctx .getBean (ThreadAwareWorker .class ).executedByThread , startsWith ("explicitScheduler2-" ));
133
+ }
103
134
104
135
@ Test
105
- public void withExplicitScheduler () throws InterruptedException {
136
+ public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute () throws InterruptedException {
106
137
Assume .group (TestGroup .PERFORMANCE );
107
138
108
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ExplicitSchedulerConfig .class );
139
+ ctx = new AnnotationConfigApplicationContext (
140
+ SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute .class );
109
141
110
142
Thread .sleep (100 );
111
- assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThanOrEqualTo (10 ));
112
- assertThat (ctx .getBean (ExplicitSchedulerConfig .class ).threadName , startsWith ("explicitScheduler-" ));
113
- ctx .close ();
143
+ assertThat (ctx .getBean (ThreadAwareWorker .class ).executedByThread , startsWith ("explicitScheduler2-" ));
144
+ }
145
+
146
+ @ Test
147
+ public void withTaskAddedVia_configureTasks () throws InterruptedException {
148
+ Assume .group (TestGroup .PERFORMANCE );
149
+
150
+ ctx = new AnnotationConfigApplicationContext (SchedulingEnabled_withTaskAddedVia_configureTasks .class );
151
+
152
+ Thread .sleep (100 );
153
+ assertThat (ctx .getBean (ThreadAwareWorker .class ).executedByThread , startsWith ("taskScheduler-" ));
154
+ }
155
+
156
+ @ Test
157
+ public void withTriggerTask () throws InterruptedException {
158
+ Assume .group (TestGroup .PERFORMANCE );
159
+
160
+ ctx = new AnnotationConfigApplicationContext (TriggerTaskConfig .class );
161
+
162
+ Thread .sleep (100 );
163
+ assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThan (1 ));
164
+ }
165
+
166
+ @ Test
167
+ public void withInitiallyDelayedFixedRateTask () throws InterruptedException {
168
+ Assume .group (TestGroup .PERFORMANCE );
169
+
170
+ ctx = new AnnotationConfigApplicationContext (FixedRateTaskConfig_withInitialDelay .class );
171
+
172
+ Thread .sleep (1950 );
173
+ AtomicInteger counter = ctx .getBean (AtomicInteger .class );
174
+
175
+ // The @Scheduled method should have been called at least once but
176
+ // not more times than the delay allows.
177
+ assertThat (counter .get (), both (greaterThan (0 )).and (lessThanOrEqualTo (10 )));
178
+ }
179
+
180
+
181
+ @ Configuration
182
+ @ EnableScheduling
183
+ static class FixedRateTaskConfig {
184
+
185
+ @ Bean
186
+ public AtomicInteger counter () {
187
+ return new AtomicInteger ();
188
+ }
189
+
190
+ @ Scheduled (fixedRate = 10 )
191
+ public void task () {
192
+ counter ().incrementAndGet ();
193
+ }
194
+ }
195
+
196
+
197
+ @ Configuration
198
+ static class FixedRateTaskConfigSubclass extends FixedRateTaskConfig {
114
199
}
115
200
116
201
@@ -140,16 +225,6 @@ public void task() {
140
225
}
141
226
142
227
143
- @ Ignore ("Disabled until SPR-14030 is resolved" )
144
- @ Test
145
- @ SuppressWarnings ("resource" )
146
- public void withExplicitSchedulerAmbiguity_andSchedulingEnabled () {
147
- exception .expect (IllegalStateException .class );
148
- exception .expectMessage (startsWith ("More than one TaskScheduler bean exists within the context" ));
149
- new AnnotationConfigApplicationContext (AmbiguousExplicitSchedulerConfig .class );
150
- }
151
-
152
-
153
228
@ Configuration
154
229
@ EnableScheduling
155
230
static class AmbiguousExplicitSchedulerConfig {
@@ -174,20 +249,6 @@ public void task() {
174
249
}
175
250
176
251
177
- @ Test
178
- public void withExplicitScheduledTaskRegistrar () throws InterruptedException {
179
- Assume .group (TestGroup .PERFORMANCE );
180
-
181
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
182
- ExplicitScheduledTaskRegistrarConfig .class );
183
-
184
- Thread .sleep (100 );
185
- assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThanOrEqualTo (10 ));
186
- assertThat (ctx .getBean (ExplicitScheduledTaskRegistrarConfig .class ).threadName , startsWith ("explicitScheduler1" ));
187
- ctx .close ();
188
- }
189
-
190
-
191
252
@ Configuration
192
253
@ EnableScheduling
193
254
static class ExplicitScheduledTaskRegistrarConfig implements SchedulingConfigurer {
@@ -230,14 +291,6 @@ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
230
291
}
231
292
232
293
233
- @ Test
234
- public void withAmbiguousTaskSchedulers_butNoActualTasks () {
235
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
236
- SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks .class );
237
- ctx .close ();
238
- }
239
-
240
-
241
294
@ Configuration
242
295
@ EnableScheduling
243
296
static class SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks {
@@ -258,16 +311,6 @@ public TaskScheduler taskScheduler2() {
258
311
}
259
312
260
313
261
- @ Ignore ("Disabled until SPR-14030 is resolved" )
262
- @ Test
263
- @ SuppressWarnings ("resource" )
264
- public void withAmbiguousTaskSchedulers_andSingleTask () {
265
- exception .expect (IllegalStateException .class );
266
- exception .expectMessage (startsWith ("More than one TaskScheduler bean exists within the context" ));
267
- new AnnotationConfigApplicationContext (SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask .class );
268
- }
269
-
270
-
271
314
@ Configuration
272
315
@ EnableScheduling
273
316
static class SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask {
@@ -292,19 +335,6 @@ public TaskScheduler taskScheduler2() {
292
335
}
293
336
294
337
295
- @ Test
296
- public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrarBean () throws InterruptedException {
297
- Assume .group (TestGroup .PERFORMANCE );
298
-
299
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
300
- SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar .class );
301
-
302
- Thread .sleep (20 );
303
- assertThat (ctx .getBean (ThreadAwareWorker .class ).executedByThread , startsWith ("explicitScheduler2-" ));
304
- ctx .close ();
305
- }
306
-
307
-
308
338
static class ThreadAwareWorker {
309
339
310
340
String executedByThread ;
@@ -346,19 +376,6 @@ public TaskScheduler taskScheduler2() {
346
376
}
347
377
348
378
349
- @ Test
350
- public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute () throws InterruptedException {
351
- Assume .group (TestGroup .PERFORMANCE );
352
-
353
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
354
- SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute .class );
355
-
356
- Thread .sleep (20 );
357
- assertThat (ctx .getBean (ThreadAwareWorker .class ).executedByThread , startsWith ("explicitScheduler2-" ));
358
- ctx .close ();
359
- }
360
-
361
-
362
379
@ Configuration
363
380
@ EnableScheduling
364
381
static class SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute implements SchedulingConfigurer {
@@ -394,19 +411,6 @@ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
394
411
}
395
412
396
413
397
- @ Test
398
- public void withTaskAddedVia_configureTasks () throws InterruptedException {
399
- Assume .group (TestGroup .PERFORMANCE );
400
-
401
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
402
- SchedulingEnabled_withTaskAddedVia_configureTasks .class );
403
-
404
- Thread .sleep (20 );
405
- assertThat (ctx .getBean (ThreadAwareWorker .class ).executedByThread , startsWith ("taskScheduler-" ));
406
- ctx .close ();
407
- }
408
-
409
-
410
414
@ Configuration
411
415
@ EnableScheduling
412
416
static class SchedulingEnabled_withTaskAddedVia_configureTasks implements SchedulingConfigurer {
@@ -436,18 +440,6 @@ public void run() {
436
440
}
437
441
438
442
439
- @ Test
440
- public void withTriggerTask () throws InterruptedException {
441
- Assume .group (TestGroup .PERFORMANCE );
442
-
443
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (TriggerTaskConfig .class );
444
-
445
- Thread .sleep (100 );
446
- assertThat (ctx .getBean (AtomicInteger .class ).get (), greaterThan (1 ));
447
- ctx .close ();
448
- }
449
-
450
-
451
443
@ Configuration
452
444
static class TriggerTaskConfig {
453
445
@@ -478,23 +470,6 @@ public Date nextExecutionTime(TriggerContext triggerContext) {
478
470
}
479
471
480
472
481
- @ Test
482
- public void withInitiallyDelayedFixedRateTask () throws InterruptedException {
483
- Assume .group (TestGroup .PERFORMANCE );
484
-
485
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
486
- FixedRateTaskConfig_withInitialDelay .class );
487
-
488
- Thread .sleep (1950 );
489
- AtomicInteger counter = ctx .getBean (AtomicInteger .class );
490
- ctx .close ();
491
-
492
- // The @Scheduled method should have been called at least once but
493
- // not more times than the delay allows.
494
- assertThat (counter .get (), both (greaterThan (0 )).and (lessThanOrEqualTo (10 )));
495
- }
496
-
497
-
498
473
@ Configuration
499
474
@ EnableScheduling
500
475
static class FixedRateTaskConfig_withInitialDelay {
0 commit comments