nexio 0.2.6

A thread-per-core async runtime with owned-buffer I/O: io_uring on Linux (cancellation layer checked by Miri and Kani), a portable epoll/kqueue/IOCP backend elsewhere
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! Unit tests for executor, covering wake ordering, drop behavior, JoinHandle,
//! cancellation, panic handling, and reactor integration.

use std::{
    cell::RefCell,
    future::Future,
    pin::Pin,
    rc::Rc,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
    task::{Context, Poll, Waker},
};

use super::{
    block_on, channel::mpsc, channel::oneshot, join2, select2, spawn, yield_now, Either, JoinError,
    JoinSet,
};

/// Helper struct that increments a counter when dropped.
#[derive(Clone)]
struct DropTracker {
    counter: Arc<AtomicUsize>,
}

impl DropTracker {
    fn new(counter: Arc<AtomicUsize>) -> Self {
        Self { counter }
    }
}

impl Drop for DropTracker {
    fn drop(&mut self) {
        self.counter.fetch_add(1, Ordering::SeqCst);
    }
}

/// A future that records when it was polled and allows external waking.
struct SteppedFuture {
    id: usize,
    poll_log: Rc<RefCell<Vec<usize>>>,
    waker_slot: Rc<RefCell<Option<Waker>>>,
    polls_remaining: usize,
}

impl Future for SteppedFuture {
    type Output = usize;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.poll_log.borrow_mut().push(self.id);
        if self.polls_remaining == 0 {
            Poll::Ready(self.id)
        } else {
            self.polls_remaining -= 1;
            *self.waker_slot.borrow_mut() = Some(cx.waker().clone());
            Poll::Pending
        }
    }
}

// ── Test: Basic spawn and join ───────────────────────────────────────────────

#[test]
fn test_spawn_and_join() {
    let result = block_on(async {
        let handle = spawn(async { 40 + 2 });
        handle.await.unwrap()
    });
    assert_eq!(result, 42);
}

#[test]
fn test_spawn_multiple_and_join() {
    let sum = block_on(async {
        let mut handles = Vec::new();
        for i in 1..=5 {
            handles.push(spawn(async move { i * 10 }));
        }
        let mut total = 0;
        for h in handles {
            total += h.await.unwrap();
        }
        total
    });
    assert_eq!(sum, 150);
}

// ── Test: Wake ordering ──────────────────────────────────────────────────────

#[test]
fn test_wake_ordering_fifo() {
    // We spawn 3 tasks that pause on first poll.
    // Then we wake them in order: Task 3, Task 1, Task 2.
    // We assert that the executor polls them in that exact FIFO order.
    let log = Rc::new(RefCell::new(Vec::new()));
    let w1 = Rc::new(RefCell::new(None));
    let w2 = Rc::new(RefCell::new(None));
    let w3 = Rc::new(RefCell::new(None));

    let log_clone = Rc::clone(&log);
    let (w1_c, w2_c, w3_c) = (Rc::clone(&w1), Rc::clone(&w2), Rc::clone(&w3));

    block_on(async move {
        let h1 = spawn(SteppedFuture {
            id: 1,
            poll_log: Rc::clone(&log_clone),
            waker_slot: w1_c,
            polls_remaining: 1,
        });
        let h2 = spawn(SteppedFuture {
            id: 2,
            poll_log: Rc::clone(&log_clone),
            waker_slot: w2_c,
            polls_remaining: 1,
        });
        let h3 = spawn(SteppedFuture {
            id: 3,
            poll_log: Rc::clone(&log_clone),
            waker_slot: w3_c,
            polls_remaining: 1,
        });

        // Let all three tasks run their first poll (log: [1, 2, 3]).
        yield_now().await;

        assert_eq!(*log_clone.borrow(), vec![1, 2, 3]);

        // Clear log for testing the wake order.
        log_clone.borrow_mut().clear();

        // Wake in specific order: 3, then 1, then 2.
        w3.borrow().as_ref().unwrap().wake_by_ref();
        w1.borrow().as_ref().unwrap().wake_by_ref();
        w2.borrow().as_ref().unwrap().wake_by_ref();

        // Await all.
        assert_eq!(h1.await.unwrap(), 1);
        assert_eq!(h2.await.unwrap(), 2);
        assert_eq!(h3.await.unwrap(), 3);

        // The second polls MUST be in exact wake order: [3, 1, 2]!
        assert_eq!(*log_clone.borrow(), vec![3, 1, 2]);
    });
}

#[test]
fn test_wake_ordering_yield_interleaved() {
    let trace = Rc::new(RefCell::new(Vec::new()));
    let t1 = Rc::clone(&trace);
    let t2 = Rc::clone(&trace);

    block_on(async move {
        let h1 = spawn(async move {
            for _ in 0..3 {
                t1.borrow_mut().push("A");
                yield_now().await;
            }
        });

        let h2 = spawn(async move {
            for _ in 0..3 {
                t2.borrow_mut().push("B");
                yield_now().await;
            }
        });

        h1.await.unwrap();
        h2.await.unwrap();
    });

    assert_eq!(*trace.borrow(), vec!["A", "B", "A", "B", "A", "B"]);
}

// ── Test: Drop semantics ─────────────────────────────────────────────────────

#[test]
fn test_drop_on_normal_completion() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        let handle = spawn(async move {
            let _captured = tracker;
            yield_now().await;
            100
        });

        assert_eq!(handle.await.unwrap(), 100);
    });

    // Captured variables in the task's future must be dropped upon completion.
    assert_eq!(drop_count.load(Ordering::SeqCst), 1);
}

#[test]
fn test_drop_join_handle_detaches_task_and_drops_output() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));
    let done_flag = Rc::new(RefCell::new(false));
    let done_clone = Rc::clone(&done_flag);

    block_on(async move {
        let handle = spawn(async move {
            yield_now().await;
            *done_clone.borrow_mut() = true;
            tracker
        });

        // Drop the JoinHandle immediately before the task completes!
        drop(handle);

        // Yield until the detached task finishes.
        while !*done_flag.borrow() {
            yield_now().await;
        }
    });

    // The output value must have been dropped when the task completed!
    assert_eq!(drop_count.load(Ordering::SeqCst), 1);
}

#[test]
fn test_drop_join_handle_after_completion_drops_output() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        let handle = spawn(async move { tracker });

        // Let the task run to completion without awaiting the handle.
        yield_now().await;
        assert!(handle.is_finished());

        // Output is still held inside Completed(...) in the shared state.
        assert_eq!(drop_count.load(Ordering::SeqCst), 0);

        // Now drop the JoinHandle.
        drop(handle);

        // Output MUST be dropped now!
        assert_eq!(drop_count.load(Ordering::SeqCst), 1);
    });
}

#[test]
fn test_join_handle_consumes_output() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        let handle = spawn(async move { tracker });

        let received = handle.await.unwrap();
        assert_eq!(drop_count.load(Ordering::SeqCst), 0);

        drop(received);
        assert_eq!(drop_count.load(Ordering::SeqCst), 1);
    });
}

#[test]
fn test_cancel_task_drops_future_and_resources() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        let handle = spawn(async move {
            let _held = tracker;
            loop {
                yield_now().await;
            }
        });

        // Let task start.
        yield_now().await;
        assert_eq!(drop_count.load(Ordering::SeqCst), 0);

        // Cancel the task.
        handle.cancel();

        // The future and its captured variables must be dropped immediately!
        assert_eq!(drop_count.load(Ordering::SeqCst), 1);

        // Awaiting the cancelled task must return Err(JoinError::Cancelled).
        let res = handle.await;
        assert_eq!(res, Err(JoinError::Cancelled));
    });
}

#[test]
fn test_executor_cleanup_drops_unfinished_tasks() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        drop(spawn(async move {
            let _held = tracker;
            loop {
                yield_now().await;
            }
        }));
        // Root future finishes immediately.
    });

    // When block_on exits, executor cleanup must drop the unfinished task!
    assert_eq!(drop_count.load(Ordering::SeqCst), 1);
}

// ── Test: Panic handling ─────────────────────────────────────────────────────

#[test]
fn test_task_panic_is_caught_and_reported() {
    block_on(async {
        let handle = spawn(async {
            panic!("test panic inside spawned task");
        });

        let res = handle.await;
        assert!(res.is_err());
        assert!(res.unwrap_err().is_panic());
    });
}

// ── Test: Nested spawns ──────────────────────────────────────────────────────

#[test]
fn test_nested_spawns() {
    let sum = block_on(async {
        let h1 = spawn(async {
            let h2 = spawn(async {
                let h3 = spawn(async { 7 });
                h3.await.unwrap() * 2
            });
            h2.await.unwrap() + 1
        });
        h1.await.unwrap()
    });
    assert_eq!(sum, 15);
}

// ── Test: Nested block_on panics ─────────────────────────────────────────────

#[test]
#[should_panic(expected = "cannot call block_on recursively on the same thread")]
fn test_nested_block_on_panics() {
    block_on(async {
        block_on(async {});
    });
}

// ── Test: Reactor integration via submit_and_poll ────────────────────────────

#[test]
fn test_reactor_integration_poll() {
    let result = block_on(async {
        let count = crate::reactor::REACTOR.with(|r| r.borrow_mut().submit_and_poll().unwrap());
        assert_eq!(count, 0);
        42
    });
    assert_eq!(result, 42);
}

// ── Test: join2 ─────────────────────────────────────────────────────────────

#[test]
fn test_join2_basic() {
    let result = block_on(async { join2(async { 10 }, async { 20 }).await });
    assert_eq!(result, (10, 20));
}

#[test]
fn test_join2_async_interleaved() {
    let result = block_on(async {
        let f1 = async {
            yield_now().await;
            yield_now().await;
            100
        };
        let f2 = async {
            yield_now().await;
            200
        };
        join2(f1, f2).await
    });
    assert_eq!(result, (100, 200));
}

#[test]
fn test_join2_drop_semantics() {
    let drop1 = Arc::new(AtomicUsize::new(0));
    let drop2 = Arc::new(AtomicUsize::new(0));
    let t1 = DropTracker::new(Arc::clone(&drop1));
    let t2 = DropTracker::new(Arc::clone(&drop2));

    block_on(async move {
        let (r1, r2) = join2(
            async move {
                let _x = t1;
                yield_now().await;
                1
            },
            async move {
                let _y = t2;
                2
            },
        )
        .await;
        assert_eq!((r1, r2), (1, 2));
    });

    assert_eq!(drop1.load(Ordering::SeqCst), 1);
    assert_eq!(drop2.load(Ordering::SeqCst), 1);
}

// ── Test: select2 ────────────────────────────────────────────────────────────

#[test]
fn test_select2_left_wins() {
    let result = block_on(async {
        let fast = async { 42 };
        let slow = async {
            yield_now().await;
            99
        };
        select2(fast, slow).await
    });
    assert_eq!(result, Either::Left(42));
    assert!(result.is_left());
    assert_eq!(result.left(), Some(42));
    assert_eq!(result.right(), None);
}

#[test]
fn test_select2_right_wins() {
    let result = block_on(async {
        let slow = async {
            yield_now().await;
            10
        };
        let fast = async { 20 };
        select2(slow, fast).await
    });
    assert_eq!(result, Either::Right(20));
    assert!(result.is_right());
    assert_eq!(result.right(), Some(20));
    assert_eq!(result.left(), None);
}

#[test]
fn test_select2_drop_cancellation_of_losing_future() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        let fast = async {
            yield_now().await;
            "fast"
        };
        let slow = async move {
            let _captured = tracker;
            loop {
                yield_now().await;
            }
            #[allow(unreachable_code)]
            "slow"
        };

        let result = select2(fast, slow).await;
        assert_eq!(result, Either::Left("fast"));
    });

    // The losing future must have been dropped and cancelled immediately!
    assert_eq!(drop_count.load(Ordering::SeqCst), 1);
}

#[test]
fn test_select2_drop_cancellation_right_loser() {
    let drop_count = Arc::new(AtomicUsize::new(0));
    let tracker = DropTracker::new(Arc::clone(&drop_count));

    block_on(async move {
        let slow = async move {
            let _captured = tracker;
            loop {
                yield_now().await;
            }
            #[allow(unreachable_code)]
            1
        };
        let fast = async {
            yield_now().await;
            2
        };

        let result = select2(slow, fast).await;
        assert_eq!(result, Either::Right(2));
    });

    // Slow future on the left was dropped upon the right future winning!
    assert_eq!(drop_count.load(Ordering::SeqCst), 1);
}

// ── Test: JoinSet ───────────────────────────────────────────────────────────

#[test]
fn test_join_set_basic() {
    block_on(async {
        let mut set = JoinSet::new();
        assert!(set.is_empty());
        assert_eq!(set.len(), 0);

        for i in 1..=4 {
            set.spawn(async move { i * 10 });
        }
        assert_eq!(set.len(), 4);
        assert!(!set.is_empty());

        let mut results = Vec::new();
        while let Some(res) = set.join_next().await {
            results.push(res.unwrap());
        }

        assert_eq!(set.len(), 0);
        assert!(set.is_empty());
        assert_eq!(set.join_next().await, None);

        results.sort();
        assert_eq!(results, vec![10, 20, 30, 40]);
    });
}

#[test]
fn test_join_set_completion_order() {
    // Task A takes 3 yields
    // Task B takes 1 yield
    // Task C takes 2 yields
    // Expected join_next order: B, C, A!
    block_on(async {
        let mut set = JoinSet::new();

        set.spawn(async {
            yield_now().await;
            yield_now().await;
            yield_now().await;
            "A"
        });

        set.spawn(async {
            yield_now().await;
            "B"
        });

        set.spawn(async {
            yield_now().await;
            yield_now().await;
            "C"
        });

        let mut order = Vec::new();
        while let Some(res) = set.join_next().await {
            order.push(res.unwrap());
        }

        assert_eq!(order, vec!["B", "C", "A"]);
    });
}

#[test]
fn test_join_set_abort_all() {
    block_on(async {
        let mut set = JoinSet::new();

        for _ in 0..3 {
            set.spawn(async {
                loop {
                    yield_now().await;
                }
            });
        }

        assert_eq!(set.len(), 3);
        set.abort_all();
        assert_eq!(set.len(), 3);

        let mut cancelled_count = 0;
        while let Some(res) = set.join_next().await {
            assert_eq!(res, Err(JoinError::Cancelled));
            cancelled_count += 1;
        }

        assert_eq!(cancelled_count, 3);
        assert_eq!(set.len(), 0);
        assert!(set.is_empty());
    });
}

#[test]
fn test_join_set_drop_cleanup() {
    let drop_count = Arc::new(AtomicUsize::new(0));

    block_on(async {
        let mut set = JoinSet::new();

        for _ in 0..3 {
            let tracker = DropTracker::new(Arc::clone(&drop_count));
            set.spawn(async move {
                let _captured = tracker;
                loop {
                    yield_now().await;
                }
            });
        }

        // Allow tasks to run their first step
        yield_now().await;
        assert_eq!(drop_count.load(Ordering::SeqCst), 0);

        // Dropping JoinSet must cancel and drop all 3 tasks immediately!
        drop(set);
    });

    assert_eq!(drop_count.load(Ordering::SeqCst), 3);
}

// ── Test: oneshot channel ───────────────────────────────────────────────────

#[test]
fn test_oneshot_send_recv() {
    block_on(async {
        let (tx, rx) = oneshot::channel();
        tx.send("hello").unwrap();
        let val = rx.await.unwrap();
        assert_eq!(val, "hello");
    });
}

#[test]
fn test_oneshot_sender_drop() {
    block_on(async {
        let (tx, rx) = oneshot::channel::<i32>();
        drop(tx);
        let res = rx.await;
        assert_eq!(res, Err(oneshot::RecvError));
    });
}

#[test]
fn test_oneshot_receiver_drop() {
    let (tx, rx) = oneshot::channel::<i32>();
    drop(rx);
    assert!(tx.is_closed());
    let err = tx.send(42);
    assert_eq!(err, Err(42));
}

#[test]
fn test_oneshot_try_recv() {
    let (tx, mut rx) = oneshot::channel::<i32>();
    assert_eq!(rx.try_recv(), Err(oneshot::TryRecvError::Empty));
    tx.send(123).unwrap();
    assert_eq!(rx.try_recv(), Ok(123));
}

// ── Test: mpsc channel ──────────────────────────────────────────────────────

#[test]
fn test_mpsc_bounded_basic() {
    block_on(async {
        let (tx, mut rx) = mpsc::channel(4);
        tx.send(1).await.unwrap();
        tx.send(2).await.unwrap();
        tx.send(3).await.unwrap();

        assert_eq!(rx.recv().await, Some(1));
        assert_eq!(rx.recv().await, Some(2));
        assert_eq!(rx.recv().await, Some(3));
    });
}

#[test]
fn test_mpsc_backpressure() {
    block_on(async {
        // Channel with capacity 2
        let (tx, mut rx) = mpsc::channel(2);

        // Fill buffer to capacity
        tx.send(10).await.unwrap();
        tx.send(20).await.unwrap();

        let tx_clone = tx.clone();
        let sent_flag = Rc::new(RefCell::new(false));
        let sent_clone = Rc::clone(&sent_flag);

        // Spawning task that attempts to send a 3rd item (must be blocked by backpressure)
        let handle = spawn(async move {
            tx_clone.send(30).await.unwrap();
            *sent_clone.borrow_mut() = true;
        });

        // Yield: task runs, finds buffer full, registers waker, stays blocked
        yield_now().await;
        assert!(!*sent_flag.borrow());

        // Receive one item: frees a slot in buffer
        assert_eq!(rx.recv().await, Some(10));

        // Waiting for the blocked task ensures it is woken and completes sending
        handle.await.unwrap();
        assert!(*sent_flag.borrow());

        // Drain remaining items
        assert_eq!(rx.recv().await, Some(20));
        assert_eq!(rx.recv().await, Some(30));
    });
}

#[test]
fn test_mpsc_sender_drop() {
    block_on(async {
        let (tx, mut rx) = mpsc::channel(2);
        tx.send(1).await.unwrap();
        drop(tx);

        assert_eq!(rx.recv().await, Some(1));
        assert_eq!(rx.recv().await, None);
    });
}

#[test]
fn test_mpsc_receiver_drop_wakes_blocked_sender() {
    block_on(async {
        let (tx, rx) = mpsc::channel(1);
        tx.send(1).await.unwrap();

        let tx_clone = tx.clone();
        let handle = spawn(async move { tx_clone.send(2).await });

        yield_now().await;
        // Drop the receiver while task is blocked waiting for space
        drop(rx);

        // The blocked sender must be woken up and return SendError
        let res = handle.await.unwrap();
        assert_eq!(res, Err(mpsc::SendError(2)));
    });
}

#[test]
fn test_mpsc_waker_cleanup_on_send_drop() {
    block_on(async {
        let (tx, _rx) = mpsc::channel(1);
        tx.send(1).await.unwrap();

        // Attempt send(2) in a select with a faster future:
        let result = select2(tx.send(2), async {
            yield_now().await;
            "winner"
        })
        .await;

        assert_eq!(result, Either::Right("winner"));
    });
}

#[test]
fn test_mpsc_unbounded() {
    block_on(async {
        let (tx, mut rx) = mpsc::unbounded_channel();
        for i in 1..=5 {
            tx.send(i).unwrap();
        }
        drop(tx);

        let mut received = Vec::new();
        while let Some(val) = rx.recv().await {
            received.push(val);
        }
        assert_eq!(received, vec![1, 2, 3, 4, 5]);
    });
}