timer-lib 0.4.0

A feature-rich Rust library for creating and managing timers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex as StdMutex;
use tokio::task::yield_now;
use tokio::time::{advance, Instant};

struct CountingCallback {
    executions: Arc<AtomicUsize>,
    fail: bool,
}

#[async_trait]
impl TimerCallback for CountingCallback {
    async fn execute(&self) -> Result<(), TimerError> {
        self.executions.fetch_add(1, Ordering::SeqCst);
        if self.fail {
            Err(TimerError::callback_failed("forced failure"))
        } else {
            Ok(())
        }
    }
}

async fn settle() {
    for _ in 0..5 {
        yield_now().await;
    }
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn timer_starts_stopped() {
    let timer = Timer::new();

    assert_eq!(timer.get_state().await, TimerState::Stopped);
    assert_eq!(timer.get_interval().await, Duration::ZERO);
    assert_eq!(timer.get_expiration_count().await, None);
    assert_eq!(timer.get_statistics().await, TimerStatistics::default());
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn one_shot_timer_returns_completed_outcome() {
    let executions = Arc::new(AtomicUsize::new(0));
    let timer = Timer::new();

    let run_id = timer
        .start_once(
            Duration::from_secs(5),
            CountingCallback {
                executions: Arc::clone(&executions),
                fail: false,
            },
        )
        .await
        .unwrap();

    advance(Duration::from_secs(5)).await;
    settle().await;

    let outcome = timer.join().await.unwrap();
    assert_eq!(outcome.run_id, run_id);
    assert_eq!(outcome.reason, TimerFinishReason::Completed);
    assert_eq!(executions.load(Ordering::SeqCst), 1);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn stop_is_graceful_and_cancel_is_immediate() {
    let executions = Arc::new(AtomicUsize::new(0));
    let timer = Timer::new();

    timer
        .start_recurring(
            RecurringSchedule::new(Duration::from_secs(1)),
            CountingCallback {
                executions: Arc::clone(&executions),
                fail: false,
            },
        )
        .await
        .unwrap();

    yield_now().await;
    advance(Duration::from_secs(1)).await;
    settle().await;
    let stopped = timer.stop().await.unwrap();
    assert_eq!(stopped.reason, TimerFinishReason::Stopped);
    assert_eq!(executions.load(Ordering::SeqCst), 1);

    timer
        .start_recurring(RecurringSchedule::new(Duration::from_secs(10)), || async {
            Ok(())
        })
        .await
        .unwrap();
    let cancelled = timer.cancel().await.unwrap();
    assert_eq!(cancelled.reason, TimerFinishReason::Cancelled);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn replacing_a_run_records_replaced_outcome() {
    let timer = Timer::new();

    let first_run = timer
        .start_recurring(RecurringSchedule::new(Duration::from_secs(10)), || async {
            Ok(())
        })
        .await
        .unwrap();

    let second_run = timer
        .start_once(Duration::from_secs(1), || async { Ok(()) })
        .await
        .unwrap();

    assert_ne!(first_run, second_run);
    let outcome = timer.join().await.unwrap();
    assert_eq!(outcome.run_id, second_run);
    assert_eq!(outcome.reason, TimerFinishReason::Completed);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn interval_adjustments_apply_to_future_ticks() {
    let executions = Arc::new(AtomicUsize::new(0));
    let timer = Timer::new();

    timer
        .start_recurring(
            RecurringSchedule::new(Duration::from_secs(5)),
            CountingCallback {
                executions: Arc::clone(&executions),
                fail: false,
            },
        )
        .await
        .unwrap();

    yield_now().await;
    advance(Duration::from_secs(5)).await;
    settle().await;
    timer
        .adjust_interval(Duration::from_secs(30))
        .await
        .unwrap();
    settle().await;
    advance(Duration::from_secs(29)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 1);
    advance(Duration::from_secs(1)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 2);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn events_are_emitted_for_key_lifecycle_changes() {
    let timer = Timer::new();
    let mut events = timer.subscribe();

    let run_id = timer
        .start_recurring(
            RecurringSchedule::new(Duration::from_secs(1)).with_expiration_count(1),
            || async { Ok(()) },
        )
        .await
        .unwrap();

    assert_eq!(
        events.wait_started().await,
        Some(TimerEvent::Started {
            run_id,
            interval: Duration::from_secs(1),
            recurring: true,
            expiration_count: Some(1),
            metadata: TimerMetadata::default(),
        })
    );

    advance(Duration::from_secs(1)).await;
    settle().await;

    assert!(matches!(
        events.wait_tick().await,
        Some(TimerEvent::Tick { run_id: seen, .. }) if seen == run_id
    ));
    let finished = events.wait_finished().await.unwrap();
    assert_eq!(finished.run_id, run_id);
    assert_eq!(finished.reason, TimerFinishReason::Completed);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn builder_starts_recurring_timers_with_less_boilerplate() {
    let executions = Arc::new(AtomicUsize::new(0));
    let timer =
        Timer::recurring(RecurringSchedule::new(Duration::from_secs(1)).with_expiration_count(2))
            .start(CountingCallback {
                executions: Arc::clone(&executions),
                fail: false,
            })
            .await
            .unwrap();

    advance(Duration::from_secs(2)).await;
    settle().await;

    let outcome = timer.join().await.unwrap();
    assert_eq!(outcome.reason, TimerFinishReason::Completed);
    assert_eq!(executions.load(Ordering::SeqCst), 2);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn recurring_timers_can_delay_the_first_tick() {
    let executions = Arc::new(AtomicUsize::new(0));
    let timer = Timer::new();

    timer
        .start_recurring(
            RecurringSchedule::new(Duration::from_secs(5))
                .with_initial_delay(Duration::from_secs(2))
                .with_expiration_count(2),
            CountingCallback {
                executions: Arc::clone(&executions),
                fail: false,
            },
        )
        .await
        .unwrap();
    settle().await;

    advance(Duration::from_secs(1)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 0);

    advance(Duration::from_secs(1)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 1);

    advance(Duration::from_secs(4)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 1);

    advance(Duration::from_secs(1)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 2);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn completion_subscription_is_lossless() {
    let timer = Timer::new();
    let mut completion = timer.completion();

    let run_id = timer
        .start_once(Duration::from_secs(2), || async { Ok(()) })
        .await
        .unwrap();

    advance(Duration::from_secs(2)).await;
    settle().await;

    let outcome = completion.wait_for_run(run_id).await.unwrap();
    assert_eq!(outcome.run_id, run_id);
    assert_eq!(outcome.reason, TimerFinishReason::Completed);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn completion_wait_advances_to_the_next_unseen_outcome() {
    let timer = Timer::new();
    let mut completion = timer.completion();

    let first_run = timer
        .start_once(Duration::from_secs(1), || async { Ok(()) })
        .await
        .unwrap();

    advance(Duration::from_secs(1)).await;
    settle().await;

    let first_outcome = completion.wait().await.unwrap();
    assert_eq!(first_outcome.run_id, first_run);

    let second_wait = tokio::spawn(async move { completion.wait().await });
    let second_run = timer
        .start_once(Duration::from_secs(1), || async { Ok(()) })
        .await
        .unwrap();

    advance(Duration::from_secs(1)).await;
    settle().await;

    let second_outcome = second_wait.await.unwrap().unwrap();
    assert_eq!(second_outcome.run_id, second_run);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn paused_builder_start_waits_for_resume() {
    let timer =
        Timer::recurring(RecurringSchedule::new(Duration::from_secs(1)).with_expiration_count(1))
            .paused_start()
            .start(|| async { Ok(()) })
            .await
            .unwrap();

    advance(Duration::from_secs(5)).await;
    settle().await;
    assert_eq!(timer.get_statistics().await.execution_count, 0);

    timer.resume().await.unwrap();
    advance(Duration::from_secs(1)).await;
    settle().await;

    assert_eq!(
        timer.join().await.unwrap().reason,
        TimerFinishReason::Completed
    );
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn builder_initial_delay_controls_the_first_recurring_tick() {
    let executions = Arc::new(AtomicUsize::new(0));
    let timer = Timer::recurring(
        RecurringSchedule::new(Duration::from_secs(3))
            .with_initial_delay(Duration::from_secs(1))
            .with_expiration_count(2),
    )
    .start(CountingCallback {
        executions: Arc::clone(&executions),
        fail: false,
    })
    .await
    .unwrap();
    settle().await;

    advance(Duration::from_secs(1)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 1);

    advance(Duration::from_secs(2)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 1);

    advance(Duration::from_secs(1)).await;
    settle().await;
    assert_eq!(executions.load(Ordering::SeqCst), 2);
    assert_eq!(
        timer.join().await.unwrap().reason,
        TimerFinishReason::Completed
    );
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn callback_timeout_counts_as_a_failed_execution() {
    let timer = Timer::once(Duration::from_secs(1))
        .callback_timeout(Duration::from_secs(2))
        .start(|| async {
            tokio::time::sleep(Duration::from_secs(10)).await;
            Ok::<(), TimerError>(())
        })
        .await
        .unwrap();
    settle().await;

    advance(Duration::from_secs(1)).await;
    settle().await;
    advance(Duration::from_secs(2)).await;
    settle().await;

    let outcome = timer.join().await.unwrap();
    assert_eq!(outcome.reason, TimerFinishReason::Completed);
    assert_eq!(outcome.statistics.execution_count, 1);
    assert_eq!(outcome.statistics.failed_executions, 1);
    assert_eq!(outcome.statistics.successful_executions, 0);
    assert!(outcome
        .statistics
        .last_error
        .as_ref()
        .is_some_and(TimerError::is_callback_timed_out));
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn retry_policy_retries_failed_callbacks_before_succeeding() {
    let attempts = Arc::new(AtomicUsize::new(0));
    let attempts_for_callback = Arc::clone(&attempts);
    let timer = Timer::once(Duration::from_secs(1))
        .max_retries(2)
        .start(move || {
            let attempts = Arc::clone(&attempts_for_callback);
            async move {
                if attempts.fetch_add(1, Ordering::SeqCst) < 2 {
                    Err(TimerError::callback_failed("try again"))
                } else {
                    Ok(())
                }
            }
        })
        .await
        .unwrap();
    settle().await;

    advance(Duration::from_secs(1)).await;
    settle().await;

    let outcome = timer.join().await.unwrap();
    assert_eq!(attempts.load(Ordering::SeqCst), 3);
    assert_eq!(outcome.statistics.execution_count, 1);
    assert_eq!(outcome.statistics.failed_executions, 2);
    assert_eq!(outcome.statistics.successful_executions, 1);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn event_suppression_can_be_enabled_from_the_builder() {
    let timer = Timer::once(Duration::from_secs(1))
        .with_events_disabled()
        .start(|| async { Ok(()) })
        .await
        .unwrap();
    let mut events = timer.subscribe();

    advance(Duration::from_secs(1)).await;
    settle().await;

    assert!(events.try_recv().is_none());
    assert_eq!(
        timer.join().await.unwrap().reason,
        TimerFinishReason::Completed
    );
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn event_helpers_wait_for_pause_resume_and_stop() {
    let timer = Timer::new();
    let mut events = timer.subscribe();

    timer
        .start_recurring(RecurringSchedule::new(Duration::from_secs(2)), || async {
            Ok(())
        })
        .await
        .unwrap();
    settle().await;

    timer.pause().await.unwrap();
    assert!(matches!(
        events.wait_paused().await,
        Some(TimerEvent::Paused { .. })
    ));

    timer.resume().await.unwrap();
    assert!(matches!(
        events.wait_resumed().await,
        Some(TimerEvent::Resumed { .. })
    ));

    let stopped = timer.stop().await.unwrap();
    let seen = events.wait_stopped().await.unwrap();
    assert_eq!(seen, stopped);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn event_helpers_wait_for_cancelled_outcomes() {
    let timer = Timer::new();
    let mut events = timer.subscribe();

    timer
        .start_recurring(RecurringSchedule::new(Duration::from_secs(5)), || async {
            Ok(())
        })
        .await
        .unwrap();
    settle().await;

    let cancelled = timer.cancel().await.unwrap();
    let seen = events.wait_cancelled().await.unwrap();
    assert_eq!(seen, cancelled);
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn fixed_rate_and_fixed_delay_schedules_diverge_under_slow_callbacks() {
    let fixed_delay_starts = Arc::new(StdMutex::new(Vec::new()));
    let fixed_rate_starts = Arc::new(StdMutex::new(Vec::new()));
    let fixed_delay_base = Instant::now();
    let fixed_rate_base = fixed_delay_base;

    let fixed_delay_timer = Timer::new();
    let fixed_delay_starts_for_callback = Arc::clone(&fixed_delay_starts);
    fixed_delay_timer
        .start_recurring(
            RecurringSchedule::new(Duration::from_secs(5))
                .fixed_delay()
                .with_expiration_count(2),
            move || {
                let starts = Arc::clone(&fixed_delay_starts_for_callback);
                async move {
                    starts
                        .lock()
                        .unwrap()
                        .push((Instant::now() - fixed_delay_base).as_secs());
                    tokio::time::sleep(Duration::from_secs(2)).await;
                    Ok::<(), TimerError>(())
                }
            },
        )
        .await
        .unwrap();

    let fixed_rate_timer = Timer::new();
    let fixed_rate_starts_for_callback = Arc::clone(&fixed_rate_starts);
    fixed_rate_timer
        .start_recurring(
            RecurringSchedule::new(Duration::from_secs(5))
                .fixed_rate()
                .with_expiration_count(2),
            move || {
                let starts = Arc::clone(&fixed_rate_starts_for_callback);
                async move {
                    starts
                        .lock()
                        .unwrap()
                        .push((Instant::now() - fixed_rate_base).as_secs());
                    tokio::time::sleep(Duration::from_secs(2)).await;
                    Ok::<(), TimerError>(())
                }
            },
        )
        .await
        .unwrap();
    settle().await;

    advance(Duration::from_secs(5)).await;
    settle().await;

    assert_eq!(*fixed_delay_starts.lock().unwrap(), vec![5]);
    assert_eq!(*fixed_rate_starts.lock().unwrap(), vec![5]);

    advance(Duration::from_secs(2)).await;
    settle().await;

    advance(Duration::from_secs(3)).await;
    settle().await;

    assert_eq!(*fixed_rate_starts.lock().unwrap(), vec![5, 10]);
    assert_eq!(*fixed_delay_starts.lock().unwrap(), vec![5]);

    advance(Duration::from_secs(2)).await;
    settle().await;

    assert_eq!(*fixed_delay_starts.lock().unwrap(), vec![5, 12]);
    assert_eq!(
        fixed_delay_timer.join().await.unwrap().reason,
        TimerFinishReason::Completed
    );
    assert_eq!(
        fixed_rate_timer.join().await.unwrap().reason,
        TimerFinishReason::Completed
    );
}