weida 0.1.0-alpha.10

QUIC-native messaging framework: runtime, native QUIC transport, Req/Rep, Push/Pull, Pub/Sub, PAIR, SURVEY and BUS
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
//! A simulated UDP network for tests: real QUIC and TLS over memory
//! ([decisions/0042](../../../docs/decisions/0042-simulated-network.md)).
//!
//! Give every runtime of one test the same [`SimNet`] through
//! `RuntimeConfig { network: Network::sim(&net), .. }`. Everything above the socket is the
//! production code: `bind_quic` binds a simulated address, `connect("weida://fp@127.0.0.1:port/..")`
//! dials it, the TLS handshake proves the same keys, mTLS refuses the same clients, and a redial
//! reports the same `PeerEvent`s. No socket is opened, so a test needs no network.
//!
//! **Addresses.** A port identifies one socket in a `SimNet`, whatever its IP. Port `0` takes
//! the next free port from 49152 upward. A port in use is `Error::Io` of kind `AddrInUse` until
//! its binding closes: `Binding::close`, or the runtime's `shutdown` or `drain`. A socket bound to
//! an unspecified IP is seen from `127.0.0.1` or `::1`, whichever family the receiver was
//! addressed in; a v4 source reaches a socket bound to a v6 address v4-mapped, as the OS presents
//! it. There is no name table: names go through `RuntimeConfig::resolver`, so a test dials IP
//! literals.
//!
//! **Faults** are keyed by port and apply to every datagram from or to it:
//! [`SimNet::set_delay`] holds datagrams for a one-way delay, [`SimNet::drop_next`] loses the next
//! few (QUIC retransmits them), and [`SimNet::partition`] loses everything until
//! [`SimNet::heal`]: an unannounced disconnect, which QUIC notices at `Limits::idle_timeout`.
//!
//! **Time.** Nothing here sleeps on its own: a delayed datagram is released by a Tokio timer, and
//! QUIC's own timers are Tokio's, so a test under `#[tokio::test(start_paused = true)]` spends
//! paused time, not wall time, on delays, idle timeouts and redial backoff. Reading the clock
//! and waiting on a paused clock cost no wall time and are fine in a unit test: a test under
//! `#[tokio::test(start_paused = true)]` that waits for a delay, an idle timeout or a redial
//! backoff is a unit test. A test that waits on a running clock is an integration test.
//!
//! **Bound.** A socket's receive buffer holds [`INBOX_DATAGRAMS`] datagrams; one arriving at a
//! full buffer is dropped and counted, as UDP drops past its receive buffer.
//!
//! **Local sockets** (on unix). A runtime on a `SimNet` binds and dials `weida+unix://` there
//! too: `bind_unix` takes the path on the network, not on disk (`AddrInUse` while a live
//! binding holds it, free again when it is dropped), and a dial is an in-memory pair with
//! 256 KiB each way, through the production unix transport, grouping and chunk framing. Both
//! ends report one simulated principal, so what the kernel proves (`SO_PEERCRED`, socket files
//! and their modes) still needs a real socket. [`SimNet::connect_unix`] is the raw dial for a
//! test that writes the local preamble by hand. A listener holds 128 connections it has not
//! accepted, and refuses the next, as a listen backlog does.
//!
//! `Binding::hand_over` needs an OS socket and panics on a simulated binding.

use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::io::{self, IoSliceMut};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
#[cfg(unix)]
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak};
use std::task::{Context, Poll, Waker};
use std::time::Duration;

use quinn::udp::{RecvMeta, Transmit};
use quinn::{AsyncUdpSocket, UdpPoller};
#[cfg(unix)]
use tokio::io::DuplexStream;
#[cfg(unix)]
use tokio::sync::mpsc;
use weida_core::Error;
#[cfg(unix)]
use weida_core::LocalPrincipal;

/// A simulated socket's receive buffer, in datagrams. A datagram that finds it full is dropped
/// and counted in [`SimStats::dropped`].
pub(crate) const INBOX_DATAGRAMS: usize = 4096;

/// The first port that port `0` hands out, and where the search wraps to.
const FIRST_EPHEMERAL: u16 = 49152;
/// Ports from [`FIRST_EPHEMERAL`] to 65535.
const EPHEMERAL_PORTS: u32 = 65536 - FIRST_EPHEMERAL as u32;

/// The connections a simulated `AF_UNIX` listener holds before it accepts them, as a kernel's
/// listen backlog does; a connection past it is refused.
#[cfg(unix)]
const LOCAL_BACKLOG: usize = 128;

/// What one simulated local connection holds in each direction before a writer waits: a
/// socket's send and receive buffers together.
#[cfg(unix)]
const LOCAL_BUFFER: usize = 256 * 1024;

/// The peer every simulated local connection reports, in both directions: the processes it
/// stands in for are this one, so a credential check passes as it does between two processes
/// of one user.
#[cfg(unix)]
pub(crate) const SIM_PRINCIPAL: LocalPrincipal = LocalPrincipal {
    uid: 1000,
    gid: 1000,
    pid: Some(1),
};

/// An in-memory UDP network: QUIC and TLS run over it unchanged, so identities, pinned
/// addresses, mTLS refusals, redials and their `PeerEvent`s are the real ones. Clone to share.
#[derive(Clone, Debug, Default)]
pub struct SimNet {
    inner: Arc<Net>,
}

/// What a [`SimNet`] did with the datagrams sent on it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SimStats {
    /// Datagrams put into a receiving socket's buffer.
    pub delivered: u64,
    /// Datagrams lost to a fault, to no socket at the destination, or to a full buffer.
    pub dropped: u64,
}

#[derive(Debug)]
struct Net {
    state: Mutex<NetState>,
    delivered: AtomicU64,
    dropped: AtomicU64,
}

impl Default for Net {
    fn default() -> Self {
        Net {
            state: Mutex::new(NetState {
                sockets: HashMap::new(),
                faults: HashMap::new(),
                next_port: FIRST_EPHEMERAL,
                #[cfg(unix)]
                locals: HashMap::new(),
                #[cfg(unix)]
                next_local: 0,
            }),
            delivered: AtomicU64::new(0),
            dropped: AtomicU64::new(0),
        }
    }
}

#[derive(Debug)]
struct NetState {
    sockets: HashMap<u16, Weak<SimSocket>>,
    faults: HashMap<u16, Fault>,
    next_port: u16,
    /// Simulated `AF_UNIX` listeners by path: the serial of the binding that
    /// holds the path, and where its connections arrive.
    #[cfg(unix)]
    locals: HashMap<PathBuf, (u64, mpsc::Sender<DuplexStream>)>,
    #[cfg(unix)]
    next_local: u64,
}

/// The faults on one port.
#[derive(Clone, Copy, Debug, Default)]
struct Fault {
    delay: Duration,
    drop_next: u64,
    partitioned: bool,
}

impl Net {
    fn lock(&self) -> MutexGuard<'_, NetState> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn count_dropped(&self) {
        self.dropped.fetch_add(1, Ordering::Relaxed);
    }
}

impl NetState {
    fn live(&self, port: u16) -> Option<Arc<SimSocket>> {
        self.sockets.get(&port).and_then(Weak::upgrade)
    }

    fn fault(&mut self, port: u16) -> &mut Fault {
        self.faults.entry(port).or_default()
    }
}

impl SimNet {
    /// A network with no sockets and no faults.
    pub fn new() -> SimNet {
        SimNet::default()
    }

    /// Holds every datagram to or from `addr` for `one_way`; `Duration::ZERO` clears it.
    ///
    /// A datagram between two delayed ports waits the larger delay, once. A delay applies to
    /// datagrams sent after the call.
    pub fn set_delay(&self, addr: SocketAddr, one_way: Duration) {
        self.inner.lock().fault(addr.port()).delay = one_way;
    }

    /// Drops the next `datagrams` datagrams to or from `addr` (added to any still pending).
    pub fn drop_next(&self, addr: SocketAddr, datagrams: u64) {
        let mut state = self.inner.lock();
        let fault = state.fault(addr.port());
        fault.drop_next = fault.drop_next.saturating_add(datagrams);
    }

    /// Drops every datagram to or from `addr` until [`SimNet::heal`]: an unannounced
    /// disconnect, which QUIC notices at `Limits::idle_timeout` (`LossCause::IdleTimeout`). For
    /// an announced one use `Binding::close` or `Binding::disconnect`.
    pub fn partition(&self, addr: SocketAddr) {
        self.inner.lock().fault(addr.port()).partitioned = true;
    }

    /// Ends a [`SimNet::partition`] of `addr`; its delay and drop count stay.
    pub fn heal(&self, addr: SocketAddr) {
        self.inner.lock().fault(addr.port()).partitioned = false;
    }

    /// The datagrams delivered and dropped so far.
    pub fn stats(&self) -> SimStats {
        SimStats {
            delivered: self.inner.delivered.load(Ordering::Relaxed),
            dropped: self.inner.dropped.load(Ordering::Relaxed),
        }
    }

    /// A bare `quinn` endpoint at `addr` on this network, accepting connections when `server`
    /// is given: a peer that speaks the wire protocol by hand, for a test that sends what weida
    /// never would. Port `0` takes the next free port, as [`Network::sim`] bindings do. Call it
    /// inside a Tokio runtime.
    ///
    /// [`Network::sim`]: crate::Network::sim
    pub fn endpoint(
        &self,
        addr: SocketAddr,
        server: Option<quinn::ServerConfig>,
    ) -> Result<quinn::Endpoint, Error> {
        let socket = self.bind(addr)?;
        quinn::Endpoint::new_with_abstract_socket(
            quinn::EndpointConfig::default(),
            server,
            socket,
            Arc::new(quinn::TokioRuntime),
        )
        .map_err(Error::Io)
    }

    /// Binds a socket at `addr`; port `0` takes the next free port.
    pub(crate) fn bind(&self, addr: SocketAddr) -> Result<Arc<SimSocket>, Error> {
        let mut state = self.inner.lock();
        let port = if addr.port() == 0 {
            let mut candidate = state.next_port;
            let mut found = None;
            for _ in 0..EPHEMERAL_PORTS {
                if state.live(candidate).is_none() {
                    found = Some(candidate);
                    break;
                }
                candidate = next_ephemeral(candidate);
            }
            let Some(port) = found else {
                return Err(Error::Io(io::Error::new(
                    io::ErrorKind::AddrInUse,
                    "no free port on this simulated network",
                )));
            };
            state.next_port = next_ephemeral(port);
            port
        } else {
            if state.live(addr.port()).is_some() {
                return Err(Error::Io(io::Error::new(
                    io::ErrorKind::AddrInUse,
                    format!("{addr} is in use on this simulated network"),
                )));
            }
            addr.port()
        };
        let socket = Arc::new(SimSocket {
            net: Arc::clone(&self.inner),
            addr: SocketAddr::new(addr.ip(), port),
            inbox: Mutex::new(Inbox::default()),
            released: AtomicBool::new(false),
        });
        state.sockets.insert(port, Arc::downgrade(&socket));
        Ok(socket)
    }

    /// Listens on the `AF_UNIX` path `path`: `AddrInUse` while a live listener holds it, as
    /// the socket's live-listener probe finds; a path whose listener is gone is taken over,
    /// as a stale socket file is replaced.
    #[cfg(unix)]
    pub(crate) fn bind_local(&self, path: &Path) -> Result<LocalListener, Error> {
        let mut state = self.inner.lock();
        if let Some((_, live)) = state.locals.get(path)
            && !live.is_closed()
        {
            return Err(Error::Io(io::Error::new(
                io::ErrorKind::AddrInUse,
                format!("{} is in use on this simulated network", path.display()),
            )));
        }
        let serial = state.next_local;
        state.next_local += 1;
        let (tx, incoming) = mpsc::channel(LOCAL_BACKLOG);
        state.locals.insert(path.to_owned(), (serial, tx));
        Ok(LocalListener {
            incoming,
            lease: LocalLease {
                net: Arc::clone(&self.inner),
                path: path.to_owned(),
                serial,
            },
        })
    }

    /// A bare connection to the `weida+unix://` binding on `path` of a runtime on this network:
    /// what `UnixStream::connect` is to an OS binding, and what the unix transport dials under
    /// `Network::sim`; a test that writes the local preamble and frames by hand uses it too.
    /// `ConnectionLost(PeerClosed)` where nothing listens or its backlog of 128 is full, as a
    /// refused `connect` is.
    #[cfg(unix)]
    pub fn connect_unix(&self, path: impl AsRef<Path>) -> Result<DuplexStream, Error> {
        let path = path.as_ref();
        let refused = || Error::ConnectionLost(weida_core::LossCause::PeerClosed);
        let listener = self
            .inner
            .lock()
            .locals
            .get(path)
            .map(|(_, tx)| tx.clone())
            .ok_or_else(refused)?;
        let (ours, theirs) = tokio::io::duplex(LOCAL_BUFFER);
        listener.try_send(theirs).map_err(|_| refused())?;
        Ok(ours)
    }
}

/// A simulated `AF_UNIX` listener: the connections dialled to its path, and the path itself,
/// which it frees when it is dropped.
#[cfg(unix)]
pub(crate) struct LocalListener {
    pub(crate) incoming: mpsc::Receiver<DuplexStream>,
    pub(crate) lease: LocalLease,
}

/// A path a simulated `AF_UNIX` listener holds; dropped, the path is free again, unless a newer
/// listener took it over.
#[cfg(unix)]
#[derive(Debug)]
pub(crate) struct LocalLease {
    net: Arc<Net>,
    path: PathBuf,
    serial: u64,
}

#[cfg(unix)]
impl LocalLease {
    pub(crate) fn path(&self) -> &Path {
        &self.path
    }
}

#[cfg(unix)]
impl Drop for LocalLease {
    fn drop(&mut self) {
        let mut state = self.net.lock();
        if state
            .locals
            .get(&self.path)
            .is_some_and(|(serial, _)| *serial == self.serial)
        {
            state.locals.remove(&self.path);
        }
    }
}

fn next_ephemeral(port: u16) -> u16 {
    if port == u16::MAX {
        FIRST_EPHEMERAL
    } else {
        port + 1
    }
}

/// A v4-mapped v6 address as the v4 address it names; anything else as it is.
fn canonical_ip(ip: IpAddr) -> IpAddr {
    match ip {
        IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(IpAddr::V6(v6), IpAddr::V4),
        v4 => v4,
    }
}

fn canonical(addr: SocketAddr) -> SocketAddr {
    SocketAddr::new(canonical_ip(addr.ip()), addr.port())
}

/// Whether a socket bound to `bound` receives a datagram addressed to `dst` (canonical).
fn accepts(bound: IpAddr, dst: IpAddr) -> bool {
    match bound {
        IpAddr::V6(v6) if v6.is_unspecified() => true,
        IpAddr::V4(v4) if v4.is_unspecified() => dst.is_ipv4(),
        specific => canonical_ip(specific) == dst,
    }
}

/// The source a receiver bound to `receiver` sees for a datagram from a socket bound to
/// `sender`, addressed to `dst` (canonical).
fn source_seen(sender: SocketAddr, dst: SocketAddr, receiver: IpAddr) -> SocketAddr {
    let ip = if sender.ip().is_unspecified() {
        match dst.ip() {
            IpAddr::V4(_) => IpAddr::V4(Ipv4Addr::LOCALHOST),
            IpAddr::V6(_) => IpAddr::V6(Ipv6Addr::LOCALHOST),
        }
    } else {
        canonical_ip(sender.ip())
    };
    let ip = match (receiver, ip) {
        (IpAddr::V6(_), IpAddr::V4(v4)) => IpAddr::V6(v4.to_ipv6_mapped()),
        (_, ip) => ip,
    };
    SocketAddr::new(ip, sender.port())
}

/// One socket on a [`SimNet`]: what a simulated binding or client endpoint reads and sends on.
pub(crate) struct SimSocket {
    net: Arc<Net>,
    addr: SocketAddr,
    inbox: Mutex<Inbox>,
    released: AtomicBool,
}

#[derive(Default)]
struct Inbox {
    queue: VecDeque<Datagram>,
    waker: Option<Waker>,
    /// Armed while the oldest datagram is still delayed.
    timer: Option<Pin<Box<tokio::time::Sleep>>>,
}

struct Datagram {
    from: SocketAddr,
    release: tokio::time::Instant,
    contents: Vec<u8>,
}

impl SimSocket {
    /// Frees this socket's port and stops it reading and sending: the end of its binding.
    /// Idempotent; a port already taken by a newer socket stays that socket's.
    pub(crate) fn release(&self) {
        self.released.store(true, Ordering::Release);
        self.unregister();
        let mut inbox = self.inbox();
        inbox.queue.clear();
        inbox.timer = None;
        inbox.waker = None;
    }

    fn unregister(&self) {
        let mut state = self.net.lock();
        let ours = state
            .sockets
            .get(&self.addr.port())
            .is_some_and(|entry| std::ptr::eq(entry.as_ptr(), self));
        if ours {
            state.sockets.remove(&self.addr.port());
        }
    }

    fn inbox(&self) -> MutexGuard<'_, Inbox> {
        self.inbox.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Sends one datagram to `destination`, or counts why it was lost.
    fn send_one(&self, destination: SocketAddr, contents: &[u8]) {
        let dst = canonical(destination);
        let (receiver, delay) = {
            let mut state = self.net.lock();
            let source = state
                .faults
                .get(&self.addr.port())
                .copied()
                .unwrap_or_default();
            let target = state.faults.get(&dst.port()).copied().unwrap_or_default();
            if source.partitioned || target.partitioned {
                drop(state);
                self.net.count_dropped();
                return;
            }
            if source.drop_next > 0 {
                state.fault(self.addr.port()).drop_next -= 1;
                drop(state);
                self.net.count_dropped();
                return;
            }
            if target.drop_next > 0 {
                state.fault(dst.port()).drop_next -= 1;
                drop(state);
                self.net.count_dropped();
                return;
            }
            let receiver = state
                .live(dst.port())
                .filter(|socket| accepts(socket.addr.ip(), dst.ip()));
            (receiver, source.delay.max(target.delay))
        };
        let Some(receiver) = receiver else {
            self.net.count_dropped();
            return;
        };
        let from = source_seen(self.addr, dst, receiver.addr.ip());
        receiver.deliver(Datagram {
            from,
            release: tokio::time::Instant::now() + delay,
            contents: contents.to_vec(),
        });
    }

    fn deliver(&self, datagram: Datagram) {
        let mut inbox = self.inbox();
        if self.released.load(Ordering::Acquire) || inbox.queue.len() >= INBOX_DATAGRAMS {
            drop(inbox);
            self.net.count_dropped();
            return;
        }
        inbox.queue.push_back(datagram);
        let waker = inbox.waker.take();
        drop(inbox);
        self.net.delivered.fetch_add(1, Ordering::Relaxed);
        if let Some(waker) = waker {
            waker.wake();
        }
    }
}

impl Drop for SimSocket {
    fn drop(&mut self) {
        self.unregister();
    }
}

impl AsyncUdpSocket for SimSocket {
    fn create_io_poller(self: Arc<Self>) -> Pin<Box<dyn UdpPoller>> {
        Box::pin(SimPoller)
    }

    fn try_send(&self, transmit: &Transmit) -> io::Result<()> {
        // A closed binding's socket is gone: what its endpoint still sends goes nowhere.
        if self.released.load(Ordering::Acquire) {
            self.net.count_dropped();
            return Ok(());
        }
        // A GSO batch is several datagrams of `segment_size` bytes, the last one possibly
        // shorter; each travels on its own.
        let size = transmit.segment_size.unwrap_or(transmit.contents.len());
        for contents in transmit.contents.chunks(size.max(1)) {
            self.send_one(transmit.destination, contents);
        }
        // Memory is never full the way a socket buffer is: a send never blocks.
        Ok(())
    }

    fn poll_recv(
        &self,
        cx: &mut Context,
        bufs: &mut [IoSliceMut<'_>],
        meta: &mut [RecvMeta],
    ) -> Poll<io::Result<usize>> {
        if self.released.load(Ordering::Acquire) {
            return Poll::Pending;
        }
        let mut inbox = self.inbox();
        loop {
            let now = tokio::time::Instant::now();
            let mut filled = 0;
            while filled < bufs.len().min(meta.len()) {
                let Some(head) = inbox.queue.front() else {
                    break;
                };
                if head.release > now {
                    break;
                }
                let datagram = inbox.queue.pop_front().expect("the head was just read");
                let len = datagram.contents.len().min(bufs[filled].len());
                bufs[filled][..len].copy_from_slice(&datagram.contents[..len]);
                meta[filled] = RecvMeta {
                    addr: datagram.from,
                    len,
                    stride: len,
                    ecn: None,
                    dst_ip: None,
                };
                filled += 1;
            }
            if filled > 0 {
                inbox.timer = None;
                return Poll::Ready(Ok(filled));
            }
            let Some(release) = inbox.queue.front().map(|head| head.release) else {
                break;
            };
            let timer = match &mut inbox.timer {
                Some(timer) => {
                    if timer.deadline() != release {
                        timer.as_mut().reset(release);
                    }
                    timer
                }
                none => none.insert(Box::pin(tokio::time::sleep_until(release))),
            };
            if timer.as_mut().poll(cx).is_pending() {
                break;
            }
        }
        inbox.waker = Some(cx.waker().clone());
        Poll::Pending
    }

    fn local_addr(&self) -> io::Result<SocketAddr> {
        Ok(self.addr)
    }
}

impl fmt::Debug for SimSocket {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SimSocket")
            .field("addr", &self.addr)
            .field("released", &self.released.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

/// A simulated socket is always writable.
#[derive(Debug)]
struct SimPoller;

impl UdpPoller for SimPoller {
    fn poll_writable(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn addr(s: &str) -> SocketAddr {
        s.parse().expect("address")
    }

    fn send(socket: &SimSocket, to: SocketAddr, contents: &[u8]) {
        socket
            .try_send(&Transmit {
                destination: to,
                ecn: None,
                contents,
                segment_size: None,
                src_ip: None,
            })
            .expect("a simulated send never fails");
    }

    /// What one poll finds: the sender and the bytes of the first datagram, or nothing.
    fn recv(socket: &SimSocket) -> Option<(SocketAddr, Vec<u8>)> {
        let mut buf = [0u8; 64];
        let mut bufs = [IoSliceMut::new(&mut buf)];
        let mut meta = [RecvMeta::default()];
        let mut cx = Context::from_waker(Waker::noop());
        match socket.poll_recv(&mut cx, &mut bufs, &mut meta) {
            Poll::Ready(Ok(1)) => Some((meta[0].addr, buf[..meta[0].len].to_vec())),
            Poll::Pending => None,
            other => panic!("unexpected poll result {other:?}"),
        }
    }

    #[test]
    fn a_datagram_reaches_the_socket_of_its_port_in_the_receivers_family() {
        let net = SimNet::new();
        let server = net.bind(addr("127.0.0.1:0")).expect("server");
        let client = net.bind(addr("[::]:0")).expect("client");
        let (server_addr, client_port) = (server.addr, client.addr.port());
        assert_ne!(server_addr.port(), client_port);

        // A v6 socket addresses a v4 peer v4-mapped, as quinn does.
        send(
            &client,
            SocketAddr::new(
                IpAddr::V6(Ipv4Addr::LOCALHOST.to_ipv6_mapped()),
                server_addr.port(),
            ),
            b"hello",
        );
        let (from, bytes) = recv(&server).expect("the server received it");
        assert_eq!(
            from,
            SocketAddr::new(Ipv4Addr::LOCALHOST.into(), client_port)
        );
        assert_eq!(bytes, b"hello");

        send(&server, from, b"back");
        let (from, bytes) = recv(&client).expect("the client received the reply");
        assert_eq!(
            from,
            SocketAddr::new(
                IpAddr::V6(Ipv4Addr::LOCALHOST.to_ipv6_mapped()),
                server_addr.port()
            )
        );
        assert_eq!(bytes, b"back");
        assert_eq!(
            net.stats(),
            SimStats {
                delivered: 2,
                dropped: 0
            }
        );
    }

    #[test]
    fn a_partition_and_a_drop_count_lose_datagrams_and_heal_restores_them() {
        let net = SimNet::new();
        let server = net.bind(addr("127.0.0.1:0")).expect("server");
        let client = net.bind(addr("127.0.0.1:0")).expect("client");
        let to = server.addr;

        net.drop_next(to, 1);
        send(&client, to, b"one");
        assert_eq!(recv(&server), None, "the first send is dropped");
        send(&client, to, b"two");
        assert_eq!(recv(&server).expect("delivered").1, b"two");

        net.partition(to);
        send(&client, to, b"three");
        assert_eq!(recv(&server), None, "a partition loses everything");
        net.heal(to);
        send(&client, to, b"four");
        assert_eq!(recv(&server).expect("delivered after heal").1, b"four");

        assert_eq!(net.stats().dropped, 2);
    }

    #[test]
    fn a_port_is_taken_until_its_socket_is_released() {
        let net = SimNet::new();
        let first = net.bind(addr("127.0.0.1:0")).expect("first");
        let taken = first.addr;
        match net.bind(taken) {
            Err(Error::Io(e)) => assert_eq!(e.kind(), io::ErrorKind::AddrInUse),
            other => panic!("expected AddrInUse, got {other:?}"),
        }
        first.release();
        let second = net.bind(taken).expect("the port is free after release");
        assert_eq!(second.addr, taken);
        // The old socket's release does not free the new one's port.
        first.release();
        assert!(net.bind(taken).is_err(), "the new socket still holds it");
    }
}