pi-async-task-queue 0.1.5

任务队列接口及默认实现
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
//! 经公开空闲来源接口(idle source API)验证原任务(Task)的保管、有限清理和退出责任。
//! 仅驱动真实生产出口,不判断业务空闲,不写入来源私有状态(private state)。
use pi_async_task_queue::{
    IdleQueueManager, IdleQueueStatus, IdleTake, IdleTaskQueue, MpscIdleTaskQueue,
    MpscQueueOptions, QueueCleanupProgress,
};
use std::num::{NonZeroU8, NonZeroUsize};
use pi_async_task::{create, Runnable, Schedule, TaskOptions};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

/// 队列(queue)不得替业务轮询(poll)或调度(schedule)未绑定原任务(Task)。
struct NoSchedule;
impl Schedule for NoSchedule {
    fn schedule(&self, _: Runnable) { panic!("未绑定任务(Task)不得被来源调度(schedule)"); }
}

/// 记录原异步计算(Future)实际析构(drop),首次异常(panic)只由指定原值产生。
struct DropProbe { drops: Arc<AtomicUsize>, panic: bool }
impl std::future::Future for DropProbe {
    type Output = ();
    fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<()> {
        panic!("来源不得业务轮询(poll)");
    }
}
impl Drop for DropProbe {
    fn drop(&mut self) {
        self.drops.fetch_add(1, Ordering::SeqCst);
        if self.panic { std::panic::panic_any(971usize); }
    }
}

/// 使用真实创建出口(creation API),不登记、推进或清理来源内部状态(internal state)。
fn make_task(drops: &Arc<AtomicUsize>, panic: bool, options: TaskOptions) -> pi_async_task::TaskParts<()> {
    create(DropProbe { drops: Arc::clone(drops), panic }, NoSchedule, (), options)
        .unwrap_or_else(|_| panic!("真实任务创建(task creation)失败"))
}

/// 最小节点额度(node budget)使固定范围和复用边界可由公开结果确定性观察。
fn one_node_options() -> MpscQueueOptions {
    MpscQueueOptions { cleanup_batch_size: NonZeroUsize::new(1).unwrap(), push_attempts: NonZeroU8::new(3).unwrap() }
}

/// @api MpscIdleTaskQueue::new/handle/Clone/clear/close/Drop;空来源可重复关闭且句柄(handle)不重开。
#[test]
fn test_empty_idle_source_closes_while_shared_handles_survive() {
    let mut queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
    let handle = queue.handle();
    let clone = handle.clone();
    assert!(matches!(handle.status(), IdleQueueStatus::Accepting));
    assert_eq!(handle.len(), 0);
    assert!(clone.is_empty());
    assert!(matches!(handle.try_take_batch(NonZeroUsize::new(1).unwrap()), Ok(IdleTake::Empty)));
    assert!(matches!(queue.clear().unwrap(), QueueCleanupProgress::Complete));
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::Complete));
    assert!(matches!(queue.close().unwrap(), QueueCleanupProgress::Complete));
    assert!(matches!(clone.status(), IdleQueueStatus::Closed));
    assert!(matches!(queue.close().unwrap(), QueueCleanupProgress::Complete));
    drop(queue);
    assert!(matches!(handle.status(), IdleQueueStatus::Closed));
    assert!(matches!(handle.try_take_batch(NonZeroUsize::new(1).unwrap()), Ok(IdleTake::Closed)));
    assert_eq!(clone.len(), 0);
}

/// @api IdleTaskQueue::try_push;缺链接(link)和关闭拒绝均返原身份,调用方继续承担原值。
#[test]
fn test_rejected_idle_submission_returns_original_and_owner_drop_cleans_accepted_task() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
    let handle = queue.handle();
    let plain = make_task(&drops, false, TaskOptions::plain());
    let id = plain.runnable.id();
    let error = handle.try_push(plain.runnable).err().expect("必须拒绝缺少链接(link)的原值");
    match error.cause {
        pi_async_task_queue::IdleQueueError::Backend(report) => {
            assert_eq!(report.current_context(), &pi_result::ErrorKind::InvalidInput);
        }
        _ => panic!("缺链接(link)须保留Backend分类"),
    }
    assert_eq!(error.task.id(), id);
    assert_eq!(handle.len(), 0);
    assert_eq!(drops.load(Ordering::SeqCst), 0);
    drop(error.task);
    let accepted = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    handle.try_push(accepted.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    assert_eq!(handle.len(), 1);
    drop(queue);
    assert!(matches!(handle.status(), IdleQueueStatus::Closed));
    assert_eq!(handle.len(), 0);
    assert_eq!(drops.load(Ordering::SeqCst), 2);
    let late = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let id = late.runnable.id();
    let error = handle.try_push(late.runnable).err().expect("关闭后必须拒绝");
    assert!(matches!(error.cause, pi_async_task_queue::IdleQueueError::Closed));
    assert_eq!(error.task.id(), id);
    assert_eq!(drops.load(Ordering::SeqCst), 2);
    drop(error.task);
    assert_eq!(drops.load(Ordering::SeqCst), 3);
}

/// @api IdleTaskQueue::try_take_batch/len;交付受节点额度(node budget)和调用上限约束,结果独立保活原值。
#[test]
fn test_idle_batch_is_bounded_and_keeps_original_order_and_ownership() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(one_node_options()).unwrap();
    let handle = queue.handle();
    let first = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let second = make_task(&drops, false, TaskOptions::intrusive_local_and_mpsc());
    let ids = [first.runnable.id(), second.runnable.id()];
    handle.try_push(first.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    handle.try_push(second.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    let first_batch = match handle.try_take_batch(NonZeroUsize::new(8).unwrap()) {
        Ok(IdleTake::Ready(batch)) => batch,
        _ => panic!("必须交付首个原值"),
    };
    assert_eq!(first_batch.len(), 1);
    assert_eq!(first_batch[0].id(), ids[0]);
    assert_eq!(handle.len(), 1);
    let second_batch = match handle.try_take_batch(NonZeroUsize::new(1).unwrap()) {
        Ok(IdleTake::Ready(batch)) => batch,
        _ => panic!("必须交付第二个原值"),
    };
    assert_eq!(second_batch.len(), 1);
    assert_eq!(second_batch[0].id(), ids[1]);
    assert!(handle.is_empty());
    drop(queue);
    assert_eq!(drops.load(Ordering::SeqCst), 0);
    drop(first_batch);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    drop(second_batch);
    assert_eq!(drops.load(Ordering::SeqCst), 2);
}

/// @api IdleTaskQueue::process_cleanup;扫描(scan)旋转未取消原值且不减数,只清理真实取消目标。
#[test]
fn test_cancellation_scan_keeps_live_tasks_and_retires_cancelled_task() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(one_node_options()).unwrap();
    let handle = queue.handle();
    let first = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let cancelled = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let first_id = first.runnable.id();
    handle.try_push(first.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    handle.try_push(cancelled.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    cancelled.control.cancel();
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::MoreWork));
    assert_eq!(handle.len(), 2);
    assert_eq!(drops.load(Ordering::SeqCst), 0);
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::MoreWork));
    assert_eq!(handle.len(), 1);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::MoreWork));
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::Complete));
    assert_eq!(handle.len(), 1);
    let batch = match handle.try_take_batch(NonZeroUsize::new(1).unwrap()) {
        Ok(IdleTake::Ready(batch)) => batch,
        _ => panic!("未取消原任务(Task)仍须可取"),
    };
    assert_eq!(batch[0].id(), first_id);
    drop(batch);
    assert_eq!(drops.load(Ordering::SeqCst), 2);
}

/// 本轮清理已归还自己的欠账,下一轮可以立即确认同一扫描终点。
#[test]
fn test_idle_cleanup_can_confirm_its_endpoint_after_releasing_the_original() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
    let handle = queue.handle();
    let task = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    handle.try_push(task.runnable).unwrap_or_else(|_| panic!("应接收原任务"));
    task.control.cancel();

    let (progress, ready) = handle.process_cleanup_with_readiness().unwrap();
    assert!(matches!(progress, QueueCleanupProgress::MoreWork));
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    assert_eq!(handle.len(), 0);
    assert!(ready, "本轮已结清欠账,未确认的终点可立即处理");

    let (progress, ready) = handle.process_cleanup_with_readiness().unwrap();
    assert!(matches!(progress, QueueCleanupProgress::Complete));
    assert!(!ready);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
}

/// @api IdleQueueManager::clear及共享process_cleanup;已有扫描(scan)不能冒充新全清理范围,后来提交留到下一轮。
#[test]
fn test_clear_uses_its_own_endpoint_and_helpers_leave_acknowledgement_to_manager() {
    let drops = Arc::new(AtomicUsize::new(0));
    let mut queue = MpscIdleTaskQueue::new(one_node_options()).unwrap();
    let handle = queue.handle();
    let first = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let second = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    handle.try_push(first.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    handle.try_push(second.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::MoreWork));
    let third = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    handle.try_push(third.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    assert!(matches!(queue.clear().unwrap(), QueueCleanupProgress::MoreWork));
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    let late = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let late_id = late.runnable.id();
    handle.try_push(late.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    assert!(matches!(handle.try_take_batch(NonZeroUsize::new(1).unwrap()), Ok(IdleTake::WouldBlock)));
    for _ in 0..4 {
        assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::MoreWork));
    }
    assert_eq!(drops.load(Ordering::SeqCst), 3);
    assert_eq!(handle.len(), 1);
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::Complete));
    assert!(matches!(handle.try_take_batch(NonZeroUsize::new(1).unwrap()), Ok(IdleTake::WouldBlock)));
    assert!(matches!(queue.clear().unwrap(), QueueCleanupProgress::Complete));
    let remaining = match handle.try_take_batch(NonZeroUsize::new(1).unwrap()) {
        Ok(IdleTake::Ready(batch)) => batch,
        _ => panic!("原固定范围后的任务(Task)必须保留"),
    };
    assert_eq!(remaining.len(), 1);
    assert_eq!(remaining[0].id(), late_id);
    assert_eq!(drops.load(Ordering::SeqCst), 3);
    drop(remaining);
    assert_eq!(drops.load(Ordering::SeqCst), 4);
}

/// @api IdleTaskQueue::try_take_batch;取消目标在普通取出时内部清理,不交给业务或提前归还数量。
#[test]
fn test_ordinary_take_discards_cancelled_task_without_polling_it() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(one_node_options()).unwrap();
    let handle = queue.handle();
    let cancelled = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let live = make_task(&drops, false, TaskOptions::intrusive_mpsc());
    let live_id = live.runnable.id();
    handle.try_push(cancelled.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    handle.try_push(live.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    cancelled.control.cancel();
    assert!(matches!(handle.try_take_batch(NonZeroUsize::new(1).unwrap()), Ok(IdleTake::WouldBlock)));
    assert_eq!(handle.len(), 1);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    let batch = match handle.try_take_batch(NonZeroUsize::new(1).unwrap()) {
        Ok(IdleTake::Ready(batch)) => batch,
        _ => panic!("须交付未取消原值"),
    };
    assert_eq!(batch[0].id(), live_id);
    assert_eq!(handle.len(), 0);
    drop(batch);
    assert_eq!(drops.load(Ordering::SeqCst), 2);
}

/// @api IdleQueueManager::clear;首个或中间原值析构(drop)异常保持原载荷(payload),余项仍恰好处理一次。
#[test]
fn test_clear_panic_preserves_original_payload_and_settles_every_pending_task() {
    for panic_index in [0, 1] {
        let drops = Arc::new(AtomicUsize::new(0));
        let mut queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
        let handle = queue.handle();
        for index in 0..3 {
            let task = make_task(&drops, index == panic_index, TaskOptions::intrusive_mpsc());
            handle.try_push(task.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
        }
        let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| queue.clear()))
            .err().expect("原析构(drop)异常必须传播");
        assert_eq!(panic.downcast_ref::<usize>(), Some(&971));
        assert_eq!(drops.load(Ordering::SeqCst), 3);
        assert_eq!(handle.len(), 0);
        assert!(matches!(handle.status(), IdleQueueStatus::Accepting));
        assert!(matches!(queue.clear().unwrap(), QueueCleanupProgress::Complete));
        assert!(matches!(queue.close().unwrap(), QueueCleanupProgress::Complete));
        drop(queue);
        assert_eq!(drops.load(Ordering::SeqCst), 3);
    }
}

/// @api MpscIdleTaskQueue::Drop;首个原值异常后无分配备援(allocation-free fallback)接续尚在来源中的尾部。
#[test]
fn test_owner_drop_panic_finishes_tail_without_waiting_for_shared_handles() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(one_node_options()).unwrap();
    let handle = queue.handle();
    for index in 0..3 {
        let task = make_task(&drops, index == 0, TaskOptions::intrusive_mpsc());
        handle.try_push(task.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    }
    let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(queue)))
        .err().expect("管理者析构(manager drop)须传播原异常");
    assert_eq!(panic.downcast_ref::<usize>(), Some(&971));
    assert_eq!(drops.load(Ordering::SeqCst), 3);
    assert_eq!(handle.len(), 0);
    assert!(matches!(handle.status(), IdleQueueStatus::Closed));
    assert!(matches!(handle.process_cleanup().unwrap(), QueueCleanupProgress::Complete));
}

/// 原值析构(drop)内只通过公开入口(public API)重入(reenter)来源,证明权限先于用户代码归还。
struct ReentrantDrop {
    handle: pi_async_task_queue::MpscIdleTaskQueueHandle,
    drops: Arc<AtomicUsize>,
    observed: Arc<AtomicUsize>,
}
impl std::future::Future for ReentrantDrop {
    type Output = ();
    fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<()> {
        panic!("来源不得业务轮询(poll)");
    }
}
impl Drop for ReentrantDrop {
    fn drop(&mut self) {
        assert_eq!(self.handle.len(), 1, "原值清理结束前不得归还数量");
        assert!(matches!(self.handle.process_cleanup(), Ok(QueueCleanupProgress::MoreWork)),
            "权限外清理尚欠账(pending cleanup)时不能Busy或Complete");
        assert_eq!(self.handle.len(), 1);
        self.observed.fetch_add(1, Ordering::SeqCst);
        self.drops.fetch_add(1, Ordering::SeqCst);
    }
}

/// @api clear/process_cleanup;分类欠账(classified pending cleanup)阻止共享帮助者过早确认范围。
#[test]
fn test_clear_reentrant_cleanup_waits_for_original_drop_to_finish() {
    let drops = Arc::new(AtomicUsize::new(0));
    let observed = Arc::new(AtomicUsize::new(0));
    let mut queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
    let handle = queue.handle();
    let task = create(ReentrantDrop { handle: handle.clone(), drops: Arc::clone(&drops), observed: Arc::clone(&observed) },
        NoSchedule, (), TaskOptions::intrusive_mpsc()).unwrap_or_else(|_| panic!("创建(creation)失败"));
    handle.try_push(task.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
    assert!(matches!(queue.clear().unwrap(), QueueCleanupProgress::MoreWork));
    assert_eq!(observed.load(Ordering::SeqCst), 1);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    assert_eq!(handle.len(), 0);
    assert!(matches!(queue.clear().unwrap(), QueueCleanupProgress::Complete));
}

/// @api try_push/try_take_batch;真实生产线程(producer thread)发布三项后立即join,原身份与次序保持。
#[test]
fn test_cross_thread_idle_submission_keeps_each_original_and_order() {
    let drops = Arc::new(AtomicUsize::new(0));
    let queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
    let handle = queue.handle();
    let producer = handle.clone();
    let producer_drops = Arc::clone(&drops);
    let ids = std::thread::spawn(move || {
        let mut ids = Vec::new();
        for _ in 0..3 {
            let task = make_task(&producer_drops, false, TaskOptions::intrusive_mpsc());
            ids.push(task.runnable.id());
            producer.try_push(task.runnable).unwrap_or_else(|_| panic!("跨线程提交(cross-thread submission)失败"));
        }
        ids
    }).join().expect("生产线程(producer thread)必须正常结束");
    assert_eq!(handle.len(), 3);
    let batch = match handle.try_take_batch(NonZeroUsize::new(2).unwrap()) {
        Ok(IdleTake::Ready(batch)) => batch,
        _ => panic!("跨线程原值须可取"),
    };
    assert_eq!(batch.iter().map(Runnable::id).collect::<Vec<_>>(), ids[..2]);
    assert_eq!(handle.len(), 1);
    drop(queue);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    assert!(matches!(handle.status(), IdleQueueStatus::Closed));
    drop(batch);
    assert_eq!(drops.load(Ordering::SeqCst), 3);
}

/// 原异步计算(Future)的析构(drop)在业务边界有界暂停,未代补来源进展或清理。
struct PausedDrop { entered: Arc<AtomicUsize>, release: Arc<AtomicUsize>, drops: Arc<AtomicUsize> }
impl std::future::Future for PausedDrop {
    type Output = ();
    fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<()> {
        panic!("来源不得业务轮询(poll)");
    }
}
impl Drop for PausedDrop {
    fn drop(&mut self) {
        self.entered.store(1, Ordering::Release);
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while self.release.load(Ordering::Acquire) == 0 {
            assert!(std::time::Instant::now() < deadline, "业务析构(drop)暂停超时");
            std::thread::yield_now();
        }
        self.drops.fetch_add(1, Ordering::SeqCst);
    }
}

/// 断言(assertion)异常时也释放业务暂停条件,不修改生产来源状态(production source state)。
struct ReleasePausedDrop(Arc<AtomicUsize>);
impl Drop for ReleasePausedDrop {
    fn drop(&mut self) { self.0.store(1, Ordering::Release); }
}

/// @api process_cleanup/try_take_batch及MpscIdleTaskQueue::Drop;最后权限外清理者退出自动接续关闭。
/// 两条真实路径分别保留Scan和Normal欠账(pending cleanup),管理者退出不等最后共享句柄(shared handle)。
#[test]
fn test_last_outside_cleanup_continues_owner_exit_for_scan_and_normal_take() {
    for normal_take in [false, true] {
        let entered = Arc::new(AtomicUsize::new(0));
        let release = Arc::new(AtomicUsize::new(0));
        let drops = Arc::new(AtomicUsize::new(0));
        let queue = MpscIdleTaskQueue::new(MpscQueueOptions::default()).unwrap();
        let handle = queue.handle();
        let task = create(PausedDrop { entered: Arc::clone(&entered), release: Arc::clone(&release),
            drops: Arc::clone(&drops) }, NoSchedule, (), TaskOptions::intrusive_mpsc())
            .unwrap_or_else(|_| panic!("创建(creation)失败"));
        handle.try_push(task.runnable).unwrap_or_else(|_| panic!("提交(submission)失败"));
        task.control.cancel();
        let cleaner = handle.clone();
        std::thread::scope(|scope| {
            let worker = scope.spawn(move || {
                if normal_take {
                    assert!(matches!(cleaner.try_take_batch(NonZeroUsize::new(1).unwrap()), Ok(IdleTake::WouldBlock)));
                } else {
                    assert!(matches!(cleaner.process_cleanup(), Ok(QueueCleanupProgress::MoreWork)));
                }
            });
            let release_on_exit = ReleasePausedDrop(Arc::clone(&release));
            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
            while entered.load(Ordering::Acquire) == 0 {
                assert!(std::time::Instant::now() < deadline, "真实清理者未进入析构(drop)");
                std::thread::yield_now();
            }
            assert_eq!(drops.load(Ordering::SeqCst), 0);
            assert_eq!(handle.len(), 1);
            drop(queue);
            assert!(matches!(handle.status(), IdleQueueStatus::Closing));
            assert_eq!(handle.len(), 1);
            drop(release_on_exit);
            worker.join().expect("最后清理者(last cleanup participant)必须正常退出");
        });
        assert_eq!(drops.load(Ordering::SeqCst), 1);
        assert_eq!(handle.len(), 0);
        assert!(matches!(handle.status(), IdleQueueStatus::Closed),
            "不得依赖再次调用公开清理(cleanup)或最后Arc释放来完成关闭");
    }
}