3
3
import static com .github .kagkarlsson .scheduler .ScheduledExecutionsFilter .all ;
4
4
import static com .github .kagkarlsson .scheduler .ScheduledExecutionsFilter .onlyResolved ;
5
5
import static com .github .kagkarlsson .scheduler .jdbc .JdbcTaskRepository .DEFAULT_TABLE_NAME ;
6
+ import static java .time .Duration .ofDays ;
7
+ import static java .time .Duration .ofMinutes ;
6
8
import static org .hamcrest .MatcherAssert .assertThat ;
7
9
import static org .hamcrest .Matchers .contains ;
8
10
import static org .hamcrest .Matchers .empty ;
@@ -175,13 +177,13 @@ public void get_due_should_be_sorted_by_priority_when_enabled() {
175
177
Instant now = TimeHelper .truncatedInstantNow ();
176
178
SchedulableTaskInstance <Void > id1 =
177
179
new SchedulableTaskInstance <>(
178
- oneTimeTask .instanceBuilder ("id1" ).priority (1 ).build (), now .minus (Duration . ofDays (1 )));
180
+ oneTimeTask .instanceBuilder ("id1" ).priority (1 ).build (), now .minus (ofDays (1 )));
179
181
SchedulableTaskInstance <Void > id2 =
180
182
new SchedulableTaskInstance <>(
181
- oneTimeTask .instanceBuilder ("id2" ).priority (10 ).build (), now .minus (Duration . ofDays (2 )));
183
+ oneTimeTask .instanceBuilder ("id2" ).priority (10 ).build (), now .minus (ofDays (2 )));
182
184
SchedulableTaskInstance <Void > id3 =
183
185
new SchedulableTaskInstance <>(
184
- oneTimeTask .instanceBuilder ("id3" ).priority (5 ).build (), now .minus (Duration . ofDays (3 )));
186
+ oneTimeTask .instanceBuilder ("id3" ).priority (5 ).build (), now .minus (ofDays (3 )));
185
187
186
188
Stream .of (id1 , id2 , id3 ).forEach (taskRespositoryWithPriority ::createIfNotExists );
187
189
@@ -295,7 +297,7 @@ public void reschedule_should_move_execution_in_time() {
295
297
296
298
Execution execution = due .get (0 );
297
299
final Optional <Execution > pickedExecution = taskRepository .pick (execution , now );
298
- final Instant nextExecutionTime = now .plus (Duration . ofMinutes (1 ));
300
+ final Instant nextExecutionTime = now .plus (ofMinutes (1 ));
299
301
taskRepository .reschedule (pickedExecution .get (), nextExecutionTime , now , null , 0 );
300
302
301
303
assertThat (taskRepository .getDue (now , POLLING_LIMIT ), hasSize (0 ));
@@ -318,7 +320,7 @@ public void reschedule_should_persist_consecutive_failures() {
318
320
319
321
Execution execution = due .get (0 );
320
322
final Optional <Execution > pickedExecution = taskRepository .pick (execution , now );
321
- final Instant nextExecutionTime = now .plus (Duration . ofMinutes (1 ));
323
+ final Instant nextExecutionTime = now .plus (ofMinutes (1 ));
322
324
taskRepository .reschedule (
323
325
pickedExecution .get (), nextExecutionTime , now .minusSeconds (1 ), now , 1 );
324
326
@@ -336,7 +338,7 @@ public void reschedule_should_update_data_if_specified() {
336
338
Execution created = taskRepository .getExecution (instance ).get ();
337
339
assertEquals (created .taskInstance .getData (), 1 );
338
340
339
- final Instant nextExecutionTime = now .plus (Duration . ofMinutes (1 ));
341
+ final Instant nextExecutionTime = now .plus (ofMinutes (1 ));
340
342
taskRepository .reschedule (created , nextExecutionTime , 2 , now , null , 0 );
341
343
342
344
final Execution rescheduled = taskRepository .getExecution (instance ).get ();
@@ -359,16 +361,43 @@ public void test_get_failing_executions() {
359
361
360
362
taskRepository .reschedule (getSingleDueExecution (), now , null , now , 1 );
361
363
assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ZERO ), hasSize (1 ));
362
- assertThat (taskRepository .getExecutionsFailingLongerThan (Duration . ofMinutes (1 )), hasSize (1 ));
363
- assertThat (taskRepository .getExecutionsFailingLongerThan (Duration . ofDays (1 )), hasSize (1 ));
364
+ assertThat (taskRepository .getExecutionsFailingLongerThan (ofMinutes (1 )), hasSize (1 ));
365
+ assertThat (taskRepository .getExecutionsFailingLongerThan (ofDays (1 )), hasSize (1 ));
364
366
365
- taskRepository .reschedule (
366
- getSingleDueExecution (), now , now .minus (Duration .ofMinutes (1 )), now , 1 );
367
+ taskRepository .reschedule (getSingleDueExecution (), now , now .minus (ofMinutes (1 )), now , 1 );
367
368
assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ZERO ), hasSize (1 ));
368
369
assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ofSeconds (1 )), hasSize (1 ));
369
370
assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ofHours (1 )), hasSize (0 ));
370
371
}
371
372
373
+ @ Test
374
+ public void
375
+ get_failing_executions_should_not_return_previously_failed_but_currently_successful () {
376
+ Instant third = TimeHelper .truncatedInstantNow ();
377
+ Instant second = third .minus (ofMinutes (1 ));
378
+ Instant first = second .minus (ofMinutes (1 ));
379
+
380
+ Instant timeToRun = first ;
381
+
382
+ final TaskInstance <Void > instance = oneTimeTask .instance ("id1" );
383
+ taskRepository .createIfNotExists (new SchedulableTaskInstance <>(instance , timeToRun ));
384
+
385
+ assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ZERO ), hasSize (0 ));
386
+
387
+ // simulate success
388
+ taskRepository .reschedule (getSingleDueExecution (), timeToRun , first , null , 0 );
389
+ assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ZERO ), hasSize (0 ));
390
+
391
+ // simulate failure
392
+ taskRepository .reschedule (getSingleDueExecution (), timeToRun , first , second , 1 );
393
+ assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ZERO ), hasSize (1 ));
394
+ assertThat (taskRepository .getExecutionsFailingLongerThan (ofMinutes (5 )), hasSize (0 ));
395
+
396
+ // simulate success
397
+ taskRepository .reschedule (getSingleDueExecution (), timeToRun , third , second , 0 );
398
+ assertThat (taskRepository .getExecutionsFailingLongerThan (Duration .ZERO ), hasSize (0 ));
399
+ }
400
+
372
401
@ Test
373
402
public void get_scheduled_executions () {
374
403
Instant now = TimeHelper .truncatedInstantNow ();
@@ -426,7 +455,7 @@ public void get_dead_executions_should_not_include_previously_unresolved() {
426
455
Instant now = TimeHelper .truncatedInstantNow ();
427
456
428
457
// 1
429
- final Instant timeDied = now .minus (Duration . ofDays (5 ));
458
+ final Instant timeDied = now .minus (ofDays (5 ));
430
459
createDeadExecution (oneTimeTask .instance ("id1" ), timeDied );
431
460
432
461
TaskResolver taskResolverMissingTask =
0 commit comments