qubit-thread-pool 0.1.1

Dynamic and fixed thread pool executor services for Qubit Rust libraries
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
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026 Haixing Hu.
 *
 *    SPDX-License-Identifier: Apache-2.0
 *
 *    Licensed under the Apache License, Version 2.0.
 *
 ******************************************************************************/
//! Tests for [`FixedThreadPool`](qubit_thread_pool::service::FixedThreadPool).

use std::{
    io,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
        mpsc,
    },
    time::Duration,
};

use qubit_thread_pool::{
    TaskExecutionError,
    service::{ExecutorService, FixedThreadPool, RejectedExecution},
};

use super::{create_runtime, wait_started, wait_until};

fn ok_unit_task() -> Result<(), io::Error> {
    Ok(())
}

fn ok_usize_task() -> Result<usize, io::Error> {
    Ok(42)
}

fn create_single_worker_pool() -> FixedThreadPool {
    FixedThreadPool::new(1).expect("fixed thread pool should be created")
}

#[test]
fn test_fixed_thread_pool_submit_acceptance_is_not_task_success() {
    let pool = FixedThreadPool::new(2).expect("fixed thread pool should be created");

    pool.submit(ok_unit_task as fn() -> Result<(), io::Error>)
        .expect("fixed thread pool should accept shared runnable")
        .get()
        .expect("shared runnable should complete successfully");

    let handle = pool
        .submit(|| Err::<(), _>(io::Error::other("task failed")))
        .expect("fixed thread pool should accept runnable");

    let err = handle
        .get()
        .expect_err("accepted runnable should report task failure through handle");
    assert!(matches!(err, TaskExecutionError::Failed(_)));
    pool.shutdown();
    create_runtime().block_on(pool.await_termination());
}

#[test]
fn test_fixed_thread_pool_submit_callable_returns_value() {
    let pool = FixedThreadPool::new(2).expect("fixed thread pool should be created");

    let handle = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("fixed thread pool should accept callable");

    assert_eq!(
        handle.get().expect("callable should complete successfully"),
        42,
    );
    pool.shutdown();
    create_runtime().block_on(pool.await_termination());
}

#[tokio::test]
async fn test_fixed_thread_pool_handle_can_be_awaited() {
    let pool = FixedThreadPool::new(2).expect("fixed thread pool should be created");

    let handle = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("fixed thread pool should accept callable");

    assert_eq!(handle.await.expect("handle should await result"), 42);
    pool.shutdown();
    pool.await_termination().await;
}

#[test]
fn test_fixed_thread_pool_shutdown_rejects_new_tasks() {
    let pool = FixedThreadPool::new(1).expect("fixed thread pool should be created");

    pool.shutdown();
    let result = pool.submit(ok_unit_task as fn() -> Result<(), io::Error>);

    assert!(matches!(result, Err(RejectedExecution::Shutdown)));
    create_runtime().block_on(pool.await_termination());
    assert!(pool.is_shutdown());
    assert!(pool.is_terminated());
}

#[test]
fn test_fixed_thread_pool_bounded_queue_rejects_when_saturated() {
    let pool = FixedThreadPool::builder()
        .pool_size(1)
        .queue_capacity(1)
        .build()
        .expect("fixed thread pool should be created");
    let (started_tx, started_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();

    let first = pool
        .submit(move || {
            started_tx
                .send(())
                .expect("test should receive task start signal");
            release_rx
                .recv()
                .map_err(|err| io::Error::other(err.to_string()))?;
            Ok::<(), io::Error>(())
        })
        .expect("first task should be accepted");
    wait_started(started_rx);
    let queued = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("queued task should be accepted");

    let saturated = pool.submit(ok_unit_task as fn() -> Result<(), io::Error>);

    assert!(matches!(saturated, Err(RejectedExecution::Saturated)));
    release_tx
        .send(())
        .expect("blocking task should receive release signal");
    first.get().expect("running task should complete normally");
    assert_eq!(queued.get().expect("queued task should run"), 42);
    pool.shutdown();
    create_runtime().block_on(pool.await_termination());
}

#[test]
fn test_fixed_thread_pool_shutdown_drains_queued_tasks() {
    let pool = create_single_worker_pool();
    let (started_tx, started_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();

    let first = pool
        .submit(move || {
            started_tx
                .send(())
                .expect("test should receive task start signal");
            release_rx
                .recv()
                .map_err(|err| io::Error::other(err.to_string()))?;
            Ok::<(), io::Error>(())
        })
        .expect("first task should be accepted");
    wait_started(started_rx);
    let second = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("queued task should be accepted");

    pool.shutdown();
    let rejected = pool.submit(ok_unit_task as fn() -> Result<(), io::Error>);
    release_tx
        .send(())
        .expect("blocking task should receive release signal");
    first
        .get()
        .expect("first task should complete successfully");

    assert!(matches!(rejected, Err(RejectedExecution::Shutdown)));
    assert_eq!(second.get().expect("queued task should still run"), 42);
    create_runtime().block_on(pool.await_termination());
    assert!(pool.is_terminated());
}

#[test]
fn test_fixed_thread_pool_shutdown_now_cancels_queued_tasks() {
    let pool = create_single_worker_pool();
    let (started_tx, started_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();

    let first = pool
        .submit(move || {
            started_tx
                .send(())
                .expect("test should receive task start signal");
            release_rx
                .recv()
                .map_err(|err| io::Error::other(err.to_string()))?;
            Ok::<(), io::Error>(())
        })
        .expect("first task should be accepted");
    wait_started(started_rx);
    let queued = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("queued task should be accepted");

    let report = pool.shutdown_now();

    assert_eq!(report.queued, 1);
    assert_eq!(report.running, 1);
    assert_eq!(report.cancelled, 1);
    assert!(matches!(queued.get(), Err(TaskExecutionError::Cancelled),));
    release_tx
        .send(())
        .expect("blocking task should receive release signal");
    first.get().expect("running task should complete normally");
    create_runtime().block_on(pool.await_termination());
    assert!(pool.is_terminated());
}

#[test]
fn test_fixed_thread_pool_cancel_before_start_reports_cancelled() {
    let pool = create_single_worker_pool();
    let (started_tx, started_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();

    let first = pool
        .submit(move || {
            started_tx
                .send(())
                .expect("test should receive task start signal");
            release_rx
                .recv()
                .map_err(|err| io::Error::other(err.to_string()))?;
            Ok::<(), io::Error>(())
        })
        .expect("first task should be accepted");
    wait_started(started_rx);
    let queued = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("queued task should be accepted");

    assert!(queued.cancel());
    assert!(queued.is_done());
    assert!(matches!(queued.get(), Err(TaskExecutionError::Cancelled),));
    pool.shutdown();
    release_tx
        .send(())
        .expect("blocking task should receive release signal");
    first.get().expect("running task should complete normally");
    create_runtime().block_on(pool.await_termination());
}

#[test]
fn test_fixed_thread_pool_await_termination_waits_for_running_task() {
    let pool = create_single_worker_pool();
    let completed = Arc::new(AtomicBool::new(false));
    let completed_for_task = Arc::clone(&completed);

    pool.submit(move || {
        std::thread::sleep(Duration::from_millis(80));
        completed_for_task.store(true, Ordering::Release);
        Ok::<(), io::Error>(())
    })
    .expect("fixed thread pool should accept task");

    pool.shutdown();
    create_runtime().block_on(pool.await_termination());

    assert!(pool.is_terminated());
    assert!(completed.load(Ordering::Acquire));
}

#[test]
fn test_fixed_thread_pool_multiple_workers_drain_local_queues() {
    let pool = FixedThreadPool::new(2).expect("fixed thread pool should be created");
    let counter = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let mut handles = Vec::new();

    for _ in 0..16 {
        let counter_for_task = Arc::clone(&counter);
        handles.push(
            pool.submit(move || {
                counter_for_task.fetch_add(1, Ordering::AcqRel);
                Ok::<(), io::Error>(())
            })
            .expect("fixed thread pool should accept task"),
        );
    }

    for handle in handles {
        handle.get().expect("task should complete");
    }
    wait_until(|| counter.load(Ordering::Acquire) == 16);
    pool.shutdown();
    create_runtime().block_on(pool.await_termination());
}

#[test]
fn test_fixed_thread_pool_large_pool_uses_global_queue_shutdown_now() {
    let pool = FixedThreadPool::new(5).expect("fixed thread pool should be created");
    let release = Arc::new(AtomicBool::new(false));
    let (started_tx, started_rx) = mpsc::channel();
    let mut running = Vec::new();

    for _ in 0..5 {
        let release_for_task = Arc::clone(&release);
        let started_tx = started_tx.clone();
        running.push(
            pool.submit(move || {
                started_tx
                    .send(())
                    .expect("test should receive task start signal");
                while !release_for_task.load(Ordering::Acquire) {
                    std::thread::sleep(Duration::from_millis(5));
                }
                Ok::<(), io::Error>(())
            })
            .expect("fixed thread pool should accept blocking task"),
        );
    }
    drop(started_tx);
    for _ in 0..5 {
        started_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("task should start within timeout");
    }

    let queued = pool
        .submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
        .expect("queued task should be accepted");
    wait_until(|| pool.queued_count() == 1);

    let report = pool.shutdown_now();

    assert_eq!(report.queued, 1);
    assert_eq!(report.cancelled, 1);
    assert!(matches!(queued.get(), Err(TaskExecutionError::Cancelled)));
    release.store(true, Ordering::Release);
    for handle in running {
        handle.get().expect("running task should complete");
    }
    create_runtime().block_on(pool.await_termination());
}

#[test]
fn test_fixed_thread_pool_large_pool_runs_global_queue_tasks() {
    let pool = FixedThreadPool::new(5).expect("fixed thread pool should be created");
    let mut handles = Vec::new();

    for value in 0..10usize {
        handles.push(
            pool.submit_callable(move || Ok::<usize, io::Error>(value))
                .expect("fixed thread pool should accept callable"),
        );
    }

    let mut values = handles
        .into_iter()
        .map(|handle| handle.get().expect("callable should complete"))
        .collect::<Vec<_>>();
    values.sort_unstable();
    assert_eq!(values, (0..10usize).collect::<Vec<_>>());
    pool.shutdown();
    create_runtime().block_on(pool.await_termination());
}

#[test]
fn test_fixed_thread_pool_shutdown_now_cancels_worker_local_batch() {
    let pool = create_single_worker_pool();
    let release = Arc::new(AtomicBool::new(false));
    let (started_tx, started_rx) = mpsc::channel();

    let first = {
        let release_for_task = Arc::clone(&release);
        pool.submit(move || {
            started_tx
                .send(())
                .expect("test should receive task start signal");
            while !release_for_task.load(Ordering::Acquire) {
                std::thread::sleep(Duration::from_millis(5));
            }
            Ok::<(), io::Error>(())
        })
        .expect("first task should be accepted")
    };
    wait_started(started_rx);

    let mut queued = Vec::new();
    for _ in 0..8 {
        queued.push(
            pool.submit_callable(ok_usize_task as fn() -> Result<usize, io::Error>)
                .expect("queued task should be accepted"),
        );
    }
    wait_until(|| pool.queued_count() == 8);

    let report = pool.shutdown_now();

    assert_eq!(report.running, 1);
    assert!(report.queued <= 8);
    release.store(true, Ordering::Release);
    first.get().expect("running task should complete");
    create_runtime().block_on(pool.await_termination());

    let mut cancelled = 0usize;
    for handle in queued {
        if matches!(handle.get(), Err(TaskExecutionError::Cancelled)) {
            cancelled += 1;
        }
    }
    assert_eq!(cancelled, 8);
}