windows-threadpool-sys 0.1.1

Memory-safe access to the Windows thread pool APIs.
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
// Copyright (c) 2026 Mike Grier
//! Unit tests for periodic thread-pool timers.
//!
//! The important assertions here are about repetition and teardown ordering.
//! Cadence itself is only loosely checked, because the pool coalesces timers and
//! a loaded machine can delay any tick.

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant, SystemTime};

use crate::callback_env::CallbackEnviron;
use crate::pool::ThreadpoolPool;
use crate::timer::ThreadpoolPeriodicTimer;
use crate::timer::tests::Fires;

fn counting_timer(period: Duration) -> (ThreadpoolPeriodicTimer, Arc<Fires>) {
    let fires = Fires::new();
    let recorder = Arc::clone(&fires);
    let timer = ThreadpoolPeriodicTimer::new(period, move |_tick| recorder.record(), None)
        .expect("create timer");
    (timer, fires)
}

// --- creation ---

#[test]
fn new_periodic_timer_succeeds() {
    assert!(ThreadpoolPeriodicTimer::new(Duration::from_millis(1), |_| {}, None).is_ok());
}

#[test]
fn new_with_env_succeeds() {
    let mut env = CallbackEnviron::new();
    assert!(ThreadpoolPeriodicTimer::new(Duration::from_millis(1), |_| {}, Some(&mut env)).is_ok());
}

/// A zero period would describe a timer that never repeats, which is what
/// `ThreadpoolTimer` is for. Accepting it would recreate exactly the ambiguity that
/// splitting the types removed.
#[test]
fn a_zero_period_is_rejected() {
    let error = ThreadpoolPeriodicTimer::new(Duration::ZERO, |_| {}, None)
        .expect_err("a zero period must be rejected");
    assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
}

/// The boundary is at one millisecond, because the pool takes the period as
/// whole milliseconds and anything shorter rounds down to zero -- which the pool
/// reads as "do not repeat".
#[test]
fn a_sub_millisecond_period_is_rejected() {
    for micros in [1u64, 100, 500, 999] {
        let error = ThreadpoolPeriodicTimer::new(Duration::from_micros(micros), |_| {}, None)
            .expect_err("a sub-millisecond period must be rejected");
        assert_eq!(
            error.kind(),
            std::io::ErrorKind::InvalidInput,
            "period of {micros}us"
        );
    }
}

/// A fractional period would be truncated by the pool, so the timer would tick
/// sooner than `period()` reports. Rejecting keeps the two in agreement.
#[test]
fn a_fractional_millisecond_period_is_rejected() {
    for micros in [1_500u64, 2_001, 10_500, 999_999] {
        let error = ThreadpoolPeriodicTimer::new(Duration::from_micros(micros), |_| {}, None)
            .expect_err("a fractional-millisecond period must be rejected");
        assert_eq!(
            error.kind(),
            std::io::ErrorKind::InvalidInput,
            "period of {micros}us"
        );
    }
    // A sub-millisecond remainder in nanoseconds is the same defect.
    let error = ThreadpoolPeriodicTimer::new(
        Duration::from_millis(5) + Duration::from_nanos(1),
        |_| {},
        None,
    )
    .expect_err("a period with a nanosecond remainder must be rejected");
    assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
}

/// Beyond `u32::MAX` milliseconds the period would be capped, making the timer
/// tick far more often than asked.
#[test]
fn a_period_beyond_the_maximum_is_rejected() {
    let error = ThreadpoolPeriodicTimer::new(
        ThreadpoolPeriodicTimer::MAX_PERIOD + Duration::from_millis(1),
        |_| {},
        None,
    )
    .expect_err("a period beyond the maximum must be rejected");
    assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);

    assert!(
        ThreadpoolPeriodicTimer::new(ThreadpoolPeriodicTimer::MAX_PERIOD, |_| {}, None).is_ok(),
        "the advertised maximum period must be accepted"
    );
}

/// Whole-millisecond periods across the accepted range are reported exactly as
/// given, which is the property the rejections above exist to preserve.
#[test]
fn an_accepted_period_is_reported_exactly() {
    for millis in [1u64, 2, 15, 1_000, 60_000, u64::from(u32::MAX)] {
        let period = Duration::from_millis(millis);
        let timer =
            ThreadpoolPeriodicTimer::new(period, |_| {}, None).expect("a whole-millisecond period");
        assert_eq!(timer.period(), period, "period of {millis}ms");
    }
}

#[test]
fn the_shortest_accepted_period_is_one_millisecond() {
    assert_eq!(
        ThreadpoolPeriodicTimer::MIN_PERIOD,
        Duration::from_millis(1)
    );
    assert!(
        ThreadpoolPeriodicTimer::new(ThreadpoolPeriodicTimer::MIN_PERIOD, |_| {}, None).is_ok(),
        "the advertised minimum period must be accepted"
    );
}

/// The guard exists to keep a periodic timer from silently being a one-shot, so
/// this checks the behaviour rather than the guard: the shortest accepted period
/// must actually repeat.
#[test]
fn the_shortest_accepted_period_actually_repeats() {
    let fires = Fires::new();
    let counter = Arc::clone(&fires);
    let timer = ThreadpoolPeriodicTimer::new(
        ThreadpoolPeriodicTimer::MIN_PERIOD,
        move |_| {
            counter.record();
        },
        None,
    )
    .expect("create timer");

    timer.start_after(Duration::ZERO);
    // Fails rather than hangs if the timer only ever fires once, which is what
    // a period that rounded down to zero would do.
    fires.wait_for(3);
    timer.stop_and_drain();

    assert!(
        fires.count() >= 3,
        "a timer at the minimum period is not repeating"
    );
}

#[test]
fn new_timer_is_stopped() {
    let timer =
        ThreadpoolPeriodicTimer::new(Duration::from_millis(1), |_| {}, None).expect("create timer");
    assert!(!timer.is_running(), "a fresh timer must not be running");
}

#[test]
fn the_period_is_reported() {
    let timer = ThreadpoolPeriodicTimer::new(Duration::from_millis(25), |_| {}, None)
        .expect("create timer");
    assert_eq!(timer.period(), Duration::from_millis(25));
}

#[test]
fn drop_without_starting_is_clean() {
    let _timer =
        ThreadpoolPeriodicTimer::new(Duration::from_millis(1), |_| {}, None).expect("create timer");
}

// --- repetition ---

#[test]
fn start_ticks_repeatedly() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.start();
    fires.wait_for(5);
    timer.stop_and_drain();
    assert!(
        fires.count() >= 5,
        "a periodic timer must keep ticking; saw {}",
        fires.count()
    );
}

#[test]
fn start_after_controls_only_the_first_tick() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    let started = Instant::now();
    timer.start_after(Duration::from_millis(80));
    fires.wait_for(1);
    let first = started.elapsed();
    // The first tick honours the explicit delay, not the period.
    assert!(
        first >= Duration::from_millis(60),
        "the first tick came after only {first:?}, well before the 80ms delay"
    );
    fires.wait_for(3);
    timer.stop_and_drain();
}

#[test]
fn a_running_timer_reports_running() {
    let (timer, fires) = counting_timer(Duration::from_millis(50));
    timer.start_after(Duration::from_millis(1));
    fires.wait_for(1);
    assert!(timer.is_running(), "ticking must not clear the schedule");
    timer.stop_and_drain();
    assert!(!timer.is_running());
}

#[test]
fn start_at_accepts_an_absolute_first_tick() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.start_at(SystemTime::now() + Duration::from_millis(10));
    fires.wait_for(3);
    timer.stop_and_drain();
}

#[test]
fn start_with_window_still_ticks() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.start_with_window(Duration::from_millis(1), Duration::from_millis(10));
    fires.wait_for(3);
    timer.stop_and_drain();
}

#[test]
fn restarting_replaces_the_previous_schedule() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.start_after(Duration::from_secs(3_600));
    timer.start_after(Duration::from_millis(1));
    fires.wait_for(3);
    timer.stop_and_drain();
}

// --- the overlap contract ---

/// The property that distinguishes this type: the pool queues ticks on schedule
/// regardless of whether the previous tick has finished, so a slow callback runs
/// concurrently with itself.
///
/// This is asserted as a capability rather than a guarantee -- the pool is free
/// to schedule as it sees fit -- so the test tolerates not observing overlap
/// while still failing if the timer stops ticking altogether.
#[test]
fn a_slow_callback_may_overlap_itself() {
    let concurrent = Arc::new(AtomicUsize::new(0));
    let peak = Arc::new(AtomicUsize::new(0));
    let fires = Fires::new();

    let in_flight = Arc::clone(&concurrent);
    let high_water = Arc::clone(&peak);
    let recorder = Arc::clone(&fires);

    // Ticks are scheduled far faster than the callback can complete.
    let timer = ThreadpoolPeriodicTimer::new(
        Duration::from_millis(2),
        move |_tick| {
            let now = in_flight.fetch_add(1, Ordering::SeqCst) + 1;
            high_water.fetch_max(now, Ordering::SeqCst);
            std::thread::sleep(Duration::from_millis(25));
            in_flight.fetch_sub(1, Ordering::SeqCst);
            recorder.record();
        },
        None,
    )
    .expect("create timer");

    timer.start_after(Duration::from_millis(1));
    fires.wait_for(4);
    timer.stop_and_drain();

    let observed = peak.load(Ordering::SeqCst);
    assert!(observed >= 1, "the timer must have ticked at all");
    // Document what actually happened; overlap is expected but not mandated.
    if observed == 1 {
        eprintln!(
            "note: the pool did not overlap ticks in this run, though the callback \
             outlasts the period and may do so"
        );
    }
}

// --- stopping ---

#[test]
fn stop_prevents_further_ticks() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.start_after(Duration::from_millis(1));
    fires.wait_for(3);
    timer.stop_and_drain();

    let settled = fires.count();
    std::thread::sleep(Duration::from_millis(60));
    assert_eq!(
        fires.count(),
        settled,
        "no tick may run after stop and drain"
    );
}

#[test]
fn stopping_a_stopped_timer_is_a_no_op() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.stop();
    timer.stop();
    assert!(!timer.is_running());
    assert_eq!(fires.count(), 0);
}

#[test]
fn a_timer_can_be_restarted_after_stopping() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    timer.start_after(Duration::from_millis(1));
    fires.wait_for(2);
    timer.stop_and_drain();
    let after_stop = fires.count();

    timer.start_after(Duration::from_millis(1));
    fires.wait_for(after_stop + 2);
    timer.stop_and_drain();
    assert!(fires.count() > after_stop);
}

/// A tick can stop its own timer, which is how "tick until done" is written.
#[test]
fn a_tick_can_stop_the_timer() {
    let ticks = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&ticks);
    let fires = Fires::new();
    let recorder = Arc::clone(&fires);

    let timer = ThreadpoolPeriodicTimer::new(
        Duration::from_millis(2),
        move |tick| {
            recorder.record();
            if counter.fetch_add(1, Ordering::SeqCst) >= 2 {
                tick.stop();
            }
        },
        None,
    )
    .expect("create timer");

    timer.start_after(Duration::from_millis(1));
    // The timer stops itself; wait for that rather than for a tick count.
    let deadline = Instant::now() + Duration::from_secs(30);
    while timer.is_running() && Instant::now() < deadline {
        std::thread::yield_now();
    }
    assert!(
        !timer.is_running(),
        "the tick should have stopped the timer"
    );

    timer.stop_and_drain();
    let settled = fires.count();
    std::thread::sleep(Duration::from_millis(50));
    assert_eq!(fires.count(), settled);
    assert!(settled >= 3);
}

// --- destruction ---

/// Dropping a running timer must terminate: `Drop` stops before draining, so the
/// timer cannot requeue itself forever.
#[test]
fn drop_of_a_running_timer_terminates() {
    let started = Instant::now();
    {
        let (timer, fires) = counting_timer(Duration::from_millis(1));
        timer.start_after(Duration::from_millis(1));
        fires.wait_for(3);
        // Drop here, with the timer still running.
    }
    assert!(
        started.elapsed() < Duration::from_secs(10),
        "dropping a running periodic timer appears to have hung"
    );
}

/// Drop must wait for an executing tick before freeing the context.
#[test]
fn drop_waits_for_an_executing_tick() {
    let done = Arc::new(AtomicUsize::new(0));
    let flag = Arc::clone(&done);
    let started = Fires::new();
    let entered = Arc::clone(&started);

    {
        let timer = ThreadpoolPeriodicTimer::new(
            Duration::from_secs(3_600),
            move |_tick| {
                entered.record();
                std::thread::sleep(Duration::from_millis(30));
                flag.fetch_add(1, Ordering::SeqCst);
            },
            None,
        )
        .expect("create timer");
        // First tick almost immediately, then not again for an hour, so exactly
        // one tick is in flight when Drop runs.
        timer.start_after(Duration::from_millis(1));
        started.wait_for(1);
        // Drop here.
    }

    assert_eq!(
        done.load(Ordering::SeqCst),
        1,
        "Drop returned while a tick was still executing"
    );
}

#[test]
fn drop_while_started_but_not_yet_ticked_is_clean() {
    let (timer, fires) = counting_timer(Duration::from_secs(3_600));
    timer.start();
    drop(timer);
    assert_eq!(fires.count(), 0);
}

// --- callback state, environment, and thread-safety ---

#[test]
fn the_callback_may_own_heap_state() {
    let data = Arc::new(vec![2_u64, 4, 6]);
    let sum = Arc::new(AtomicUsize::new(0));
    let total = Arc::clone(&sum);
    let fires = Fires::new();
    let recorder = Arc::clone(&fires);

    let timer = ThreadpoolPeriodicTimer::new(
        Duration::from_millis(2),
        move |tick| {
            total.fetch_add(data.iter().sum::<u64>() as usize, Ordering::SeqCst);
            recorder.record();
            tick.stop();
        },
        None,
    )
    .expect("create timer");

    timer.start_after(Duration::from_millis(1));
    fires.wait_for(1);
    timer.stop_and_drain();
    assert!(sum.load(Ordering::SeqCst) >= 12);
}

#[test]
fn a_timer_runs_on_a_private_pool() {
    // Declared before the timer so it outlives it.
    let pool = ThreadpoolPool::new().expect("create pool");
    let mut env = CallbackEnviron::new();
    env.set_pool(&pool);

    let fires = Fires::new();
    let recorder = Arc::clone(&fires);
    let timer = ThreadpoolPeriodicTimer::new(
        Duration::from_millis(2),
        move |_tick| recorder.record(),
        Some(&mut env),
    )
    .expect("create timer");

    timer.start_after(Duration::from_millis(1));
    fires.wait_for(3);
    timer.stop_and_drain();
}

#[test]
fn a_periodic_timer_is_send_and_sync() {
    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}
    assert_send::<ThreadpoolPeriodicTimer>();
    assert_sync::<ThreadpoolPeriodicTimer>();
}

#[test]
fn a_timer_can_be_started_from_another_thread() {
    let (timer, fires) = counting_timer(Duration::from_millis(2));
    std::thread::scope(|scope| {
        let timer = &timer;
        scope.spawn(move || timer.start_after(Duration::from_millis(1)));
    });
    fires.wait_for(3);
    timer.stop_and_drain();
}