hick-reactor 0.2.0

Async mDNS driver layered on `mdns-proto` + `hick-udp`, runtime-agnostic via `agnostic-net` (tokio & smol).
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
use super::*;
use crate::service::{SERVICE_UPDATE_CAPACITY, ServiceMailbox};

/// Drain one [`ServiceUpdate`] from a shared mailbox (the handle side), used by
/// the service-update tests to assert delivery without awaiting the async
/// [`crate::Service::next`].
fn lock_mailbox_for_test(
  mailbox: &std::sync::Arc<std::sync::Mutex<ServiceMailbox>>,
) -> Option<ServiceUpdate> {
  mailbox
    .lock()
    .unwrap_or_else(|e| e.into_inner())
    .drain_for_test()
}

#[test]
fn on_link_check_rejects_non_255_ttl() {
  // only TTL/Hop-Limit exactly 255 (or an absent value, where we
  // can't enforce) is treated as on-link.
  assert!(is_on_link(Some(255)));
  assert!(is_on_link(None)); // degraded: platform didn't report it
  assert!(!is_on_link(Some(254)));
  assert!(!is_on_link(Some(1)));
  assert!(!is_on_link(Some(0)));
}

/// regression: a PRESENT (bound) family's `send_to` failure
/// must map to `Retry` (keep the debt, retry until the 2 s ceiling), NOT
/// `WriteOff`. A bound UDP socket can return transient errors whose kind is
/// NOT `WouldBlock`/`Interrupted` (e.g. `ENOBUFS`, route/interface churn);
/// writing that family off would free the route once the OTHER family drained
/// and strand this family's peers on stale positive-TTL records. `WriteOff` is
/// reserved for an ABSENT socket (the caller's `let mut … = WriteOff` default),
/// never produced by this present-socket classifier.
#[test]
fn present_socket_send_error_is_retry_not_writeoff() {
  // Ok → Sent.
  assert_eq!(
    present_socket_send_outcome::<usize>(&Ok(42)),
    WithdrawalSend::Sent,
  );
  // Every non-WouldBlock/Interrupted error kind a bound socket might surface
  // must still be Retry (NEVER WriteOff).
  for kind in [
    std::io::ErrorKind::WouldBlock,
    std::io::ErrorKind::Interrupted,
    std::io::ErrorKind::OutOfMemory, // stands in for ENOBUFS buffer pressure
    std::io::ErrorKind::AddrNotAvailable, // transient interface/route churn
    std::io::ErrorKind::PermissionDenied,
    std::io::ErrorKind::Other,
  ] {
    let res: std::io::Result<usize> = Err(std::io::Error::from(kind));
    assert_eq!(
      present_socket_send_outcome(&res),
      WithdrawalSend::Retry,
      "a present (bound) socket error ({kind:?}) must be Retry, not WriteOff"
    );
  }
}

#[test]
fn packet_is_response_reads_qr_bit() {
  // QR bit is the MSB of header byte 2.
  assert!(packet_is_response(&[0, 0, 0x84, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  assert!(!packet_is_response(&[
    0, 0, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0
  ])); // query
  assert!(!packet_is_response(&[0, 0])); // too short to be a response
  assert!(!packet_is_response(&[]));
}

// an untrusted response (QR=1 from a non-5353 source port) must
// be dropped BEFORE it can consume the take-once self-send credit, so our
// genuine port-5353 loopback still matches.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn untrusted_response_does_not_burn_self_send_credit() {
  use std::{
    net::{IpAddr, Ipv4Addr},
    time::SystemTime,
  };

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);

  // A QR=1 response body (header byte 2 = 0x84) we "recently sent".
  let body = vec![0u8, 0, 0x84, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  record_self_send(&mut state.recent_sends, &body, SystemTime::now());
  assert_eq!(state.recent_sends.len(), 1);

  // Same bytes arriving from an EPHEMERAL port (on-link TTL 255): untrusted
  // response — must be dropped before `take_self_send`.
  let untrusted = Packet {
    src: "192.0.2.9:40000".parse().unwrap(),
    data: body.clone(),
    local_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
    interface_index: 0,
    kernel_rx_time: Some(SystemTime::now()),
    read_time: SystemTime::now(),
    hop_limit: Some(255),
  };
  state.handle_packet(untrusted);
  assert_eq!(
    state.recent_sends.len(),
    1,
    "untrusted response must not consume the self-send credit"
  );

  // The genuine loopback from port 5353 passes the gate and consumes it.
  let loopback = Packet {
    src: "192.0.2.9:5353".parse().unwrap(),
    data: body,
    local_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
    interface_index: 0,
    kernel_rx_time: Some(SystemTime::now()),
    read_time: SystemTime::now(),
    hop_limit: Some(255),
  };
  state.handle_packet(loopback);
  assert_eq!(
    state.recent_sends.len(),
    0,
    "the trusted port-5353 loopback consumes the credit"
  );
}

/// A short datagram (just enough to set QR=1 but not a full DNS message) from
/// a non-5353 source bumps packets_rx + bytes_rx exactly once, and exactly
/// one reject counter (packets_dropped). No double-count: proto's handle() is
/// never reached so proto cannot bump these counters.
///
/// The test drives `handle_packet` directly — no socket bind needed — and uses
/// `#[cfg(feature = "tokio")]` only to access `DriverState::new`.
#[cfg(all(feature = "stats", feature = "tokio"))]
#[test]
fn pre_drop_short_qr1_counts_rx_and_dropped_exactly_once() {
  use std::{
    net::{IpAddr, Ipv4Addr},
    time::SystemTime,
  };

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);

  // 3-byte body: only byte 2 matters (QR=1 → 0x80). Too short for a valid DNS
  // message — proto would reject it on parse, but we drop before proto.
  let body: Vec<u8> = vec![0x00, 0x00, 0x80];
  let len = body.len() as u64;

  // Source port ≠ 5353 → untrusted-response pre-drop path; on-link (TTL=255).
  let pkt = Packet {
    src: "192.0.2.7:40000".parse().unwrap(),
    data: body,
    local_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
    interface_index: 0,
    kernel_rx_time: Some(SystemTime::now()),
    read_time: SystemTime::now(),
    hop_limit: Some(255),
  };
  state.handle_packet(pkt);

  let snap = state.stats.snapshot();
  assert_eq!(
    snap.packets_rx, 1,
    "packets_rx must be 1 (datagram was received)"
  );
  assert_eq!(
    snap.bytes_rx, len,
    "bytes_rx must equal the datagram length"
  );
  assert_eq!(snap.packets_dropped, 1, "exactly one reject counter");
  // Confirm no double-count: only the driver-side bump runs (proto handle() was
  // not called), so no extra packets_rx from the proto path.
  assert_eq!(
    snap.packets_rx, 1,
    "no double-count: proto handle() was not reached"
  );
}

/// A well-formed untrusted QR=1 response from a non-5353 source (12-byte DNS
/// header with QR=1 set, all fields zero otherwise) must trigger the
/// untrusted-response pre-drop: packets_rx +1, bytes_rx +len, packets_dropped
/// +1. Self-send credit ring must be unchanged.
#[cfg(all(feature = "stats", feature = "tokio"))]
#[test]
fn pre_drop_untrusted_qr1_response_counts_rx_and_dropped_exactly_once() {
  use std::{
    net::{IpAddr, Ipv4Addr},
    time::SystemTime,
  };

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);

  // Minimal 12-byte DNS response header: QR=1 (byte 2 = 0x84 for AA+Response).
  let body: Vec<u8> = vec![
    0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  ];
  let len = body.len() as u64;

  // No prior self-send credit recorded — if the drop were to incorrectly call
  // take_self_send the tracker would stay at zero (no match), but the correct
  // behaviour is that it is never called at all.
  assert_eq!(state.recent_sends.len(), 0);

  let pkt = Packet {
    src: "192.0.2.8:54321".parse().unwrap(), // non-5353 → untrusted
    data: body,
    local_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
    interface_index: 0,
    kernel_rx_time: Some(SystemTime::now()),
    read_time: SystemTime::now(),
    hop_limit: Some(255), // on-link
  };
  state.handle_packet(pkt);

  // Self-send tracker unchanged (never reached).
  assert_eq!(
    state.recent_sends.len(),
    0,
    "self-send credit ring must be untouched"
  );

  let snap = state.stats.snapshot();
  assert_eq!(snap.packets_rx, 1, "packets_rx +1 (datagram was received)");
  assert_eq!(snap.bytes_rx, len, "bytes_rx == datagram length");
  assert_eq!(snap.packets_dropped, 1, "exactly one reject counter");
}

/// Off-link datagrams (TTL ≠ 255) must also count packets_rx + bytes_rx once
/// (received from the wire) and packets_dropped once (rejected).
#[cfg(all(feature = "stats", feature = "tokio"))]
#[test]
fn pre_drop_off_link_datagram_counts_rx_and_dropped_exactly_once() {
  use std::{
    net::{IpAddr, Ipv4Addr},
    time::SystemTime,
  };

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);

  // A datagram with TTL < 255 → off-link gate fires before the untrusted-
  // response check. Use a query (QR=0) so only the §11 path is exercised.
  let body: Vec<u8> = vec![
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  ];
  let len = body.len() as u64;

  let pkt = Packet {
    src: "203.0.113.5:5353".parse().unwrap(),
    data: body,
    local_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
    interface_index: 0,
    kernel_rx_time: Some(SystemTime::now()),
    read_time: SystemTime::now(),
    hop_limit: Some(64), // off-link: TTL != 255
  };
  state.handle_packet(pkt);

  let snap = state.stats.snapshot();
  assert_eq!(snap.packets_rx, 1, "packets_rx +1 (datagram was received)");
  assert_eq!(snap.bytes_rx, len, "bytes_rx == datagram length");
  assert_eq!(snap.packets_dropped, 1, "exactly one reject counter");
}

// NOTE: the same-host sibling-address RETENTION tests
// (`unregister_shared_host_preserves_sibling_addresses`,
// `unregister_with_unannounced_same_host_sibling_withdraws_addresses`,
// `unregister_disjoint_host_addrs_withdraws_only_own`) and their
// `goodbye_v4_addrs` / `goodbye_withdraws_addr` helpers were REMOVED in the
// endpoint-owned-withdrawal migration. They inspected the encoded bytes of the
// deleted driver-side goodbye queue (`state.goodbyes[0].data`), produced by the
// deleted `retained_host_addrs` sibling scan in `remove_service`. Sibling
// retention now lives in the endpoint (`Endpoint::sibling_retained_addrs`,
// recomputed FRESH each round in `poll_withdrawal_transmit` from the route
// table) and is covered by the proto-level
// `poll_withdrawal_transmit ... sibling retention` test.
// NOTE: the non-terminal coalescing + bound + drop-oldest semantics for
// service updates (one Established, latest Renamed, bounded ring, reserved
// terminal) moved out of the driver's per-ctx overflow deque into the
// handle-owned `ServiceMailbox` and are unit-tested at that seam in
// `crate::service::tests` (`mailbox_coalesces_established_and_renamed_by_kind`,
// `mailbox_hard_cap_drops_oldest`,
// `mailbox_terminal_reserved_under_non_terminal_pressure`, …). The driver-level
// tests below assert the END-TO-END contract through `deliver_service_update` +
// the live `Service` handle.

/// a non-draining caller cannot grow memory without bound — a flood of service
/// updates is bounded + coalesced by the handle-owned mailbox (one Established,
/// latest Renamed, reserved terminal), never an unbounded backlog.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn service_update_delivery_is_bounded_for_non_draining_caller() {
  use mdns_proto::{ServiceUpdate, event::ServiceRenamed};

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let mut r = mdns_proto::ServiceRecords::new(
    mdns_proto::Name::try_from_str("_ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("svc._ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("host.local.").unwrap(),
    631,
    120,
  );
  r.add_a(std::net::Ipv4Addr::new(192, 168, 1, 10));
  // `reg` (the mailbox `Arc` + the doorbell receiver) is kept alive but NEVER
  // drained — a non-draining caller. The driver ctx shares the same mailbox.
  let reg = state
    .register_service(mdns_proto::ServiceSpec::new(r), now)
    .unwrap();
  let handle = reg.handle;

  // Push a churn of Established + distinct Renamed far past the cap.
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    for i in 0..1000u32 {
      deliver_service_update(ctx, ServiceUpdate::Established);
      deliver_service_update(
        ctx,
        ServiceUpdate::Renamed(ServiceRenamed::new(
          mdns_proto::Name::try_from_str(&format!("svc-{i}._ipp._tcp.local.")).unwrap(),
        )),
      );
    }
    // The mailbox coalesces to one Established + the latest Renamed — at most
    // the cap, regardless of how much the peer churns.
    let mb = ctx.mailbox.lock().unwrap_or_else(|e| e.into_inner());
    assert!(
      mb.non_terminal_len() <= SERVICE_UPDATE_CAPACITY,
      "the mailbox must stay within capacity under churn; got {}",
      mb.non_terminal_len()
    );
    // Established + Renamed coalesce by kind, so exactly two non-terminal
    // updates survive.
    assert_eq!(
      mb.non_terminal_len(),
      2,
      "Established and the latest Renamed coalesce to two pending updates"
    );
  }
  drop(reg);
}

#[test]
fn addr_in_subnet_masks_correctly() {
  use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
  let net = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 10));
  assert!(addr_in_subnet(
    net,
    24,
    IpAddr::V4(Ipv4Addr::new(192, 168, 1, 200))
  ));
  assert!(!addr_in_subnet(
    net,
    24,
    IpAddr::V4(Ipv4Addr::new(192, 168, 2, 1))
  ));
  // prefix 0 matches everything; family mismatch never matches.
  assert!(addr_in_subnet(
    IpAddr::V4(Ipv4Addr::UNSPECIFIED),
    0,
    IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))
  ));
  assert!(!addr_in_subnet(net, 24, IpAddr::V6(Ipv6Addr::LOCALHOST)));
  // IPv6 /64.
  let n6 = IpAddr::V6("2001:db8:0:1::".parse().unwrap());
  assert!(addr_in_subnet(
    n6,
    64,
    IpAddr::V6("2001:db8:0:1::ff".parse().unwrap())
  ));
  assert!(!addr_in_subnet(
    n6,
    64,
    IpAddr::V6("2001:db8:0:2::ff".parse().unwrap())
  ));
}

#[test]
fn src_on_local_link_fallback() {
  use std::net::{IpAddr, Ipv4Addr};
  let subnets = vec![(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 10)), 24u8)];
  const BOUND: u32 = 3;
  // In-subnet peer is on-link; an off-subnet global address is not
  // (interface index is irrelevant for non-link-local sources).
  assert!(src_on_local_link(
    &subnets,
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::new(192, 168, 1, 55))
  ));
  assert!(!src_on_local_link(
    &subnets,
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))
  ));
  // Loopback is always on-link.
  assert!(src_on_local_link(
    &subnets,
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::LOCALHOST)
  ));
  // §11 fail-closed: a global source with no enumerated subnets has no
  // on-link evidence and is dropped (was previously fail-open).
  assert!(!src_on_local_link(
    &[],
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))
  ));
  assert!(!src_on_local_link(
    &[],
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1))
  ));
  // A global source inside a cached /8 is on-link; outside it is dropped.
  let wide = vec![(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 0)), 8u8)];
  assert!(src_on_local_link(
    &wide,
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3))
  ));
  assert!(!src_on_local_link(
    &wide,
    BOUND,
    BOUND,
    IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1))
  ));
}

#[test]
fn src_on_local_link_scopes_link_local_to_bound_interface() {
  // a link-local source is on-link ONLY when it arrived on the
  // interface we're bound to — a link-local address from a different NIC is
  // not our link and must not pass the §11 fallback.
  use std::net::{IpAddr, Ipv4Addr};
  let subnets = vec![(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 10)), 24u8)];
  const BOUND: u32 = 3;
  const OTHER: u32 = 7;
  let v4_ll = IpAddr::V4(Ipv4Addr::new(169, 254, 1, 1));
  let v6_ll = IpAddr::V6("fe80::1".parse().unwrap());
  // Arrived on the bound interface → on-link.
  assert!(src_on_local_link(&subnets, BOUND, BOUND, v4_ll));
  assert!(src_on_local_link(&subnets, BOUND, BOUND, v6_ll));
  // Arrived on a DIFFERENT interface → NOT on-link.
  assert!(!src_on_local_link(&subnets, BOUND, OTHER, v4_ll));
  assert!(!src_on_local_link(&subnets, BOUND, OTHER, v6_ll));
  // Receive interface unknown (0) → degraded accept (can't scope).
  assert!(src_on_local_link(&subnets, BOUND, 0, v4_ll));
  assert!(src_on_local_link(&subnets, BOUND, 0, v6_ll));
}

#[test]
fn collect_local_subnets_rejects_zero_index() {
  // the fallback is scoped to the BOUND interface. Index 0 is
  // "no interface" — it must NOT enumerate every NIC, so the result is
  // empty (which makes src_on_local_link fail closed for a global source
  // rather than treat another NIC's subnet as on-link).
  assert!(collect_local_subnets(0).is_empty());
}

#[test]
fn self_send_consume_once() {
  // one recorded send suppresses exactly one loopback.
  let t = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"hello", t);
  // The loopback arrives at-or-after our send -> matched and consumed.
  assert!(take_self_send(
    &mut tracker,
    b"hello",
    t,
    MatchMode::Ordered
  ));
  // A second byte-identical packet finds no entry -> treated as a peer.
  assert!(!take_self_send(
    &mut tracker,
    b"hello",
    t,
    MatchMode::Ordered
  ));
  assert!(tracker.is_empty());
}

#[test]
fn self_send_distinct_payloads_do_not_match() {
  let t = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"alpha", t);
  assert!(!take_self_send(
    &mut tracker,
    b"beta",
    t,
    MatchMode::Ordered
  ));
  // The unrelated entry is left intact for its own loopback.
  assert!(take_self_send(
    &mut tracker,
    b"alpha",
    t,
    MatchMode::Ordered
  ));
}

#[test]
fn self_send_expires_after_ttl() {
  // a packet arriving more than SELF_SEND_TTL after the send
  // is no longer our loopback, and the stale entry is swept on the next
  // record so the tracker can't grow without bound.
  let t = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"hello", t);
  let too_late = t + SELF_SEND_TTL + Duration::from_millis(1);
  assert!(!take_self_send(
    &mut tracker,
    b"hello",
    too_late,
    MatchMode::Ordered
  ));
  record_self_send(&mut tracker, b"other", too_late);
  assert_eq!(tracker.len(), 1);
  assert!(take_self_send(
    &mut tracker,
    b"other",
    too_late,
    MatchMode::Ordered
  ));
}

#[test]
fn self_send_peer_before_our_send_cannot_steal_credit() {
  // a byte-identical peer datagram the kernel stamped BEFORE
  // our send must not consume the credit even though its content hash
  // matches; otherwise the genuine loopback is later misclassified as a
  // peer (self-rename / dropped answers).
  let sent = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"probe", sent);
  let peer_rx = sent - Duration::from_millis(500);
  assert!(!take_self_send(
    &mut tracker,
    b"probe",
    peer_rx,
    MatchMode::Ordered
  ));
  // Our genuine loopback arrives at-or-after the send and is matched.
  let loop_rx = sent + Duration::from_millis(1);
  assert!(take_self_send(
    &mut tracker,
    b"probe",
    loop_rx,
    MatchMode::Ordered
  ));
}

// on microsecond `timeval` sources (Apple/BSD)
// RX_TIMESTAMP_GRAIN is 1µs, so a loopback whose kernel timestamp was
// truncated to a slightly-earlier microsecond than our nanosecond send
// time still counts as ours — but anything earlier than the grain is a
// genuine pre-send (peer) datagram and must not match.
#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[test]
fn self_send_ordered_tolerates_microsecond_truncation() {
  assert_eq!(hick_udp::RX_TIMESTAMP_GRAIN, Duration::from_micros(1));
  let sent = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"trunc", sent);
  let truncated_rx = sent - (hick_udp::RX_TIMESTAMP_GRAIN - Duration::from_nanos(1));
  assert!(take_self_send(
    &mut tracker,
    b"trunc",
    truncated_rx,
    MatchMode::Ordered
  ));

  record_self_send(&mut tracker, b"trunc", sent);
  let too_early = sent - (hick_udp::RX_TIMESTAMP_GRAIN + Duration::from_micros(4));
  assert!(!take_self_send(
    &mut tracker,
    b"trunc",
    too_early,
    MatchMode::Ordered
  ));
}

// on nanosecond `SO_TIMESTAMPNS` (Linux/Android) the kernel
// timestamp is exact, so RX_TIMESTAMP_GRAIN is zero and there is NO
// pre-send tolerance: a byte-identical peer datagram stamped even 500ns
// before our send must not steal the take-once credit.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn self_send_ordered_nanosecond_rejects_pre_send() {
  assert_eq!(hick_udp::RX_TIMESTAMP_GRAIN, Duration::ZERO);
  let sent = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"probe", sent);
  let pre_send = sent - Duration::from_nanos(500);
  assert!(!take_self_send(
    &mut tracker,
    b"probe",
    pre_send,
    MatchMode::Ordered
  ));
  // The entry survives the non-match; our genuine loopback (at-or-after
  // the send) is still matched.
  assert!(take_self_send(
    &mut tracker,
    b"probe",
    sent,
    MatchMode::Ordered
  ));
}

#[test]
fn self_send_degraded_matches_take_once_within_ttl() {
  // with no kernel timestamp the reference is a userspace
  // READ time (always at-or-after the send). Degraded mode matches on
  // content hash alone within TTL, take-once. This is what keeps normal
  // single-host operation correct on Windows / timestamp-less kernels.
  let sent = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"win", sent);
  let read = sent + Duration::from_millis(10);
  assert!(take_self_send(
    &mut tracker,
    b"win",
    read,
    MatchMode::Degraded
  ));
  // Take-once: the credit is gone. (A byte-identical PEER datagram read
  // next would now be treated as a peer — and, conversely, a pre-buffered
  // peer datagram read first could consume this credit. That credit-theft
  // exposure is the documented degradation when no kernel rx timestamp is
  // available; ordered mode is what closes it.)
  assert!(!take_self_send(
    &mut tracker,
    b"win",
    read,
    MatchMode::Degraded
  ));
}

#[test]
fn self_send_degraded_expires_after_ttl() {
  let sent = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"win", sent);
  let too_late = sent + SELF_SEND_TTL + Duration::from_millis(1);
  assert!(!take_self_send(
    &mut tracker,
    b"win",
    too_late,
    MatchMode::Degraded
  ));
}

#[test]
fn self_send_dual_stack_records_two_entries() {
  // dual-stack fan-out records one entry per real send, so
  // BOTH loopback copies are suppressed.
  let t = SystemTime::now();
  let mut tracker = Vec::new();
  record_self_send(&mut tracker, b"resp", t);
  record_self_send(&mut tracker, b"resp", t);
  assert!(take_self_send(&mut tracker, b"resp", t, MatchMode::Ordered));
  assert!(take_self_send(&mut tracker, b"resp", t, MatchMode::Ordered));
  assert!(!take_self_send(
    &mut tracker,
    b"resp",
    t,
    MatchMode::Ordered
  ));
}

#[test]
fn self_send_cap_declines_without_evicting_live_entries() {
  // at capacity, record_self_send declines a new entry rather
  // than evicting a still-live one (which would unmask a real loopback).
  let t = SystemTime::now();
  let mut tracker = vec![(fnv1a(b"live"), t); MAX_SELF_SEND_ENTRIES];
  record_self_send(&mut tracker, b"overflow", t);
  assert_eq!(tracker.len(), MAX_SELF_SEND_ENTRIES);
  // The would-be new entry was never added.
  assert!(!take_self_send(
    &mut tracker,
    b"overflow",
    t,
    MatchMode::Ordered
  ));
  // A pre-existing live entry is still matchable.
  assert!(take_self_send(&mut tracker, b"live", t, MatchMode::Ordered));
}

// NOTE: the deleted driver-goodbye-queue seam tests
// (`flush_goodbyes_completes_the_burst`,
// `live_goodbye_round_with_no_send_keeps_budget_and_backs_off`,
// `live_drain_force_clears_expired_barrier`) asserted the removed per-driver
// `goodbyes` queue + `sent_once` transmit barrier (`drain_goodbyes` Part A
// re-arm, the `expires_at` anti-pin force-clear, and `has_pending_barrier`).
// The endpoint now owns the resend schedule, the spend/re-arm bookkeeping, and
// the 2 s anti-pin ceiling — covered by the proto-level withdrawal tests
// (`note_withdrawal_result` spend/backoff, `drain_completed_withdrawals`
// ceiling). The replacement-survival test below is the driver-seam observation
// that a withdrawal HOLDS the name and frees it on completion.

/// Endpoint-owned-withdrawal replacement survival (supersedes the old free-name
/// goodbye BARRIER test). Under `with_probe_unique_names(false)` a same-name
/// replacement would announce a positive TTL directly (no §8.1 probe) — exactly
/// the configuration in which a stale TTL=0 goodbye could be overtaken. The old
/// driver enforced ordering with a transmit barrier; the endpoint now enforces
/// it structurally — it KEEPS the route (holding the name) for the whole §10.1
/// withdrawal, so a same-name `register_service` is REJECTED until the goodbye
/// completes and frees the name. No replacement can announce ahead of the
/// withdrawal because no replacement can even be registered until it is done.
///
/// Driven through `DriverState` directly (no sockets — the reactor's multi-task
/// loop cannot be stepped deterministically). With no bound family every
/// withdrawal round fails to deliver, so the withdrawal is force-completed at
/// its 2 s anti-pin ceiling rather than by spending its resend budget; the
/// name-held → name-freed observation is identical either way.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn same_name_replacement_is_rejected_until_withdrawal_completes() {
  use std::{net::Ipv4Addr, time::Duration};

  let opts = crate::options::ServerOptions::default()
    .with_endpoint_config(mdns_proto::EndpointConfig::new().with_probe_unique_names(false));
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let mk = || {
    let mut r = mdns_proto::ServiceRecords::new(
      mdns_proto::Name::try_from_str("_ipp._tcp.local.").unwrap(),
      mdns_proto::Name::try_from_str("repl._ipp._tcp.local.").unwrap(),
      mdns_proto::Name::try_from_str("repl.local.").unwrap(),
      631,
      120,
    );
    r.add_a(Ipv4Addr::new(192, 168, 1, 10));
    mdns_proto::ServiceSpec::new(r)
  };

  // 1. Register A and drive its proto to an announced state so the withdrawal
  //    snapshot is NON-empty (records were confirmed-emitted). Delivery is
  //    simulated via `note_transmit_delivered` so the announce/host guards
  //    latch (no sockets are bound).
  let a = state.register_service(mk(), now).unwrap().handle;
  {
    let ctx = state.services.get_mut(&a).unwrap();
    let mut buf = vec![0u8; 4096];
    let mut t = now;
    for _ in 0..40 {
      t += Duration::from_millis(300);
      let _ = ctx.proto.handle_timeout(t);
      while let Ok(Some(_)) = ctx.proto.poll_transmit(t, &mut buf) {
        ctx.proto.note_transmit_delivered(t);
      }
    }
  }

  // 2. Unregister A → begins the endpoint-owned withdrawal (name held). The ctx
  //    is KEPT (marked withdrawing) and the route is reserved.
  state.remove_service(a, now);
  assert!(
    state
      .services
      .get(&a)
      .map(|c| c.withdrawing)
      .unwrap_or(false),
    "unregister must begin the withdrawal and keep the ctx (withdrawing)"
  );

  // 3. While the withdrawal is in flight the SAME name must be rejected — the
  //    endpoint holds the route, so a replacement cannot announce a fresh
  //    positive TTL ahead of the stale TTL=0.
  match state.register_service(mk(), now) {
    Err(crate::error::RegisterError::NameAlreadyRegistered(_)) => {}
    Err(e) => panic!("a same-name registration must be rejected while withdrawing; got {e:?}"),
    Ok(_) => {
      panic!("a same-name registration must be rejected while the withdrawal holds the name")
    }
  }

  // 4. Drive the withdrawal to completion. With no bound family each round fails
  //    to deliver, so the endpoint force-completes it at the 2 s anti-pin
  //    ceiling; `drain_withdrawals` then frees the route and GCs the ctx.
  let mut scratch = vec![0u8; 4096];
  let mut t = now;
  let mut completed = false;
  for _ in 0..64 {
    t += Duration::from_millis(250);
    state.drain_withdrawals(t, &mut scratch).await;
    if !state.services.contains_key(&a) {
      completed = true;
      break;
    }
  }
  assert!(
    completed,
    "the withdrawal must complete (route freed + driver ctx GC'd) — by its 2 s \
       anti-pin ceiling when no family can deliver"
  );

  // 5. The name is freed → a same-name replacement now registers successfully.
  state
    .register_service(mk(), t)
    .expect("the same name must be re-registerable once the withdrawal completes");
}

/// A `Conflict` queued at an internal retirement must still reach the host
/// after the withdrawal GCs the ctx. With the handle-owned reserved-terminal
/// mailbox this is now TRIVIAL (formerly ): `deliver_service_update` routes
/// the `Conflict` to the mailbox's reserved terminal slot, the mailbox `Arc` is
/// shared with the live `Service` handle, and the withdrawal GC removes the ctx
/// UNCONDITIONALLY — yet the terminal is still drainable by the live reader
/// because the mailbox outlives the ctx. No overflow deque, no deferral.
///
/// Driven through `DriverState` directly (no sockets). With no bound family the
/// withdrawal force-completes at its 2 s anti-pin ceiling.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn queued_conflict_survives_withdrawal_gc() {
  use std::{net::Ipv4Addr, time::Duration};

  use mdns_proto::ServiceUpdate;

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let mut r = mdns_proto::ServiceRecords::new(
    mdns_proto::Name::try_from_str("_ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("cflt._ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("cflt.local.").unwrap(),
    631,
    120,
  );
  r.add_a(Ipv4Addr::new(192, 168, 1, 10));
  // Keep `reg` (the mailbox `Arc` + doorbell receiver) alive: this is the live
  // reader that must still observe the Conflict after the ctx is GC'd.
  let reg = state
    .register_service(mdns_proto::ServiceSpec::new(r), now)
    .unwrap();
  let handle = reg.handle;
  let mailbox = Arc::clone(&reg.mailbox);

  // 1. Drive the proto to an announced state so the withdrawal snapshot is
  //    NON-empty (otherwise the withdrawal completes instantly with nothing to
  //    retract — we want the Conflict to outlive an in-flight withdrawal).
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    let mut buf = vec![0u8; 4096];
    let mut t = now;
    for _ in 0..40 {
      t += Duration::from_millis(300);
      let _ = ctx.proto.handle_timeout(t);
      while let Ok(Some(_)) = ctx.proto.poll_transmit(t, &mut buf) {
        ctx.proto.note_transmit_delivered(t);
      }
    }
  }

  // 2. Deliver a `Conflict` at retirement — it lands in the mailbox's RESERVED
  //    terminal slot (not the non-terminal ring).
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    deliver_service_update(ctx, ServiceUpdate::Conflict);
  }

  // 3. Begin the endpoint-owned withdrawal — exactly what the rename-collision /
  //    encode-failure retirement arms do (mark `withdrawing`, snapshot, hand to
  //    the endpoint). From here `push_updates` skips this ctx.
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    ctx.withdrawing = true;
    let snap = ctx.proto.withdrawal_snapshot();
    state.endpoint.begin_withdrawal(handle, snap, now);
  }

  // 4. Drive the withdrawal to completion. With no bound family each round
  //    fails to deliver, so the endpoint force-completes at the 2 s ceiling;
  //    `drain_withdrawals` then GCs the ctx UNCONDITIONALLY (no deferral).
  let mut scratch = vec![0u8; 4096];
  let mut t = now;
  let mut completed = false;
  for _ in 0..64 {
    t += Duration::from_millis(250);
    state.drain_withdrawals(t, &mut scratch).await;
    if !state.services.contains_key(&handle) {
      completed = true;
      break;
    }
  }
  assert!(
    completed,
    "the withdrawal must complete (route freed + driver ctx GC'd unconditionally)"
  );

  // 5. The Conflict survived the ctx GC: it lives in the handle-owned mailbox's
  //    reserved slot and is still drainable by the live reader.
  let drained = lock_mailbox_for_test(&mailbox);
  assert!(
    matches!(drained, Some(ServiceUpdate::Conflict)),
    "the Conflict queued at retirement must survive the unconditional ctx GC and \
       stay readable from the handle-owned mailbox; got {drained:?}"
  );

  drop(reg);
}

/// a reactor RegisterService that RECLAIMS a renamed-away old
/// name's detached goodbye must not LOSE that goodbye if the caller drops the
/// reply receiver. Under cancel-on-announce the goodbye is cancelled only when the
/// reclaiming service confirms advertising the name; a dropped-reply orphan is
/// removed before it ever announces, so the goodbye is never cancelled and still
/// emits the TTL=0 retraction. Seeds a real detached old-name goodbye by driving
/// an announced service through a §9 rename, then re-registers the OLD name with a
/// dropped reply and asserts the goodbye survives.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn dropped_reply_reclaiming_register_keeps_old_name_goodbye() {
  use std::{
    net::{IpAddr, Ipv4Addr, SocketAddr},
    time::Duration,
  };

  use mdns_proto::{
    Name, ServiceRecords, ServiceSpec,
    event::RouteEvent,
    wire::{Header, MessageBuilder},
  };

  use crate::command::Command;

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let old_inst = Name::try_from_str("Old._ipp._tcp.local.").unwrap();
  let mut r = ServiceRecords::new(
    Name::try_from_str("_ipp._tcp.local.").unwrap(),
    old_inst.clone(),
    Name::try_from_str("old-host.local.").unwrap(),
    631,
    120,
  );
  r.add_a(Ipv4Addr::new(192, 168, 1, 10));
  // Keep `reg` alive for the whole test: dropping it closes the doorbell, which
  // the driver reads as caller-gone and would withdraw the service mid-rename.
  let reg = state.register_service(ServiceSpec::new(r), now).unwrap();
  let handle = reg.handle;

  let mut buf = std::vec![0u8; 4096];

  // Drive "Old" to announced, so its rename hands off a NON-empty goodbye.
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    let mut t = now;
    for _ in 0..40 {
      t += Duration::from_millis(300);
      let _ = ctx.proto.handle_timeout(t);
      while let Ok(Some(_)) = ctx.proto.poll_transmit(t, &mut buf) {
        ctx.proto.note_transmit_delivered(t);
      }
    }
    assert!(
      ctx.proto.advertises_host(),
      "Old must announce before the rename (so the goodbye is non-empty)"
    );
  }

  // A conflicting SRV authority for "Old" with rival rdata (port 9999): we lose
  // the §8.2 tiebreak and rename away.
  let conflict = {
    let target = Name::try_from_str("rival-host.local.").unwrap();
    let mut cbuf = [0u8; 512];
    let mut b = MessageBuilder::<'_, 32>::try_new(&mut cbuf, Header::new()).unwrap();
    b.push_srv_authority(&old_inst, 120, 0, 0, 9999, &target)
      .unwrap();
    let n = b.finish().unwrap();
    cbuf[..n].to_vec()
  };
  let src = SocketAddr::from(([192, 168, 1, 200], 5353));
  let local_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 10));

  // Feed the conflict + drive the proto until "Old" renames away (seeding the
  // detached old-name goodbye via push_updates' surviving-rename handoff).
  let mut t = now;
  let mut renamed = false;
  for _ in 0..80 {
    t += Duration::from_millis(250);
    {
      let ctx = state.services.get_mut(&handle).unwrap();
      let _ = ctx.proto.handle_timeout(t);
      while let Ok(Some(_)) = ctx.proto.poll_transmit(t, &mut buf) {
        ctx.proto.note_transmit_delivered(t);
      }
    }
    {
      let DriverState {
        endpoint, services, ..
      } = &mut state;
      if let Ok(evs) = endpoint.handle(t, src, local_ip, 0, &conflict, false) {
        for ev in evs {
          if let Ok(RouteEvent::ToService(ts)) = ev
            && let Some(ctx) = services.get_mut(&ts.handle())
          {
            ctx.proto.handle_event(ts.into_event(), t);
          }
        }
      }
    }
    state.push_updates(t).await;
    if state
      .services
      .get(&handle)
      .map(|c| c.proto.name().as_str() != old_inst.as_str())
      .unwrap_or(true)
    {
      renamed = true;
      break;
    }
  }
  assert!(
    renamed,
    "Old must rename away under sustained conflict (seeding the detached goodbye)"
  );

  // Re-register the OLD name with a DROPPED reply receiver: `reply.send` fails, so
  // the rollback must RESTORE the reclaimed old-name goodbye.
  let (reply_tx, reply_rx) = futures::channel::oneshot::channel();
  drop(reply_rx);
  let mut r2 = ServiceRecords::new(
    Name::try_from_str("_ipp._tcp.local.").unwrap(),
    old_inst.clone(),
    Name::try_from_str("new-host.local.").unwrap(),
    631,
    120,
  );
  r2.add_a(Ipv4Addr::new(192, 168, 1, 11));
  state.handle_command(
    Command::RegisterService {
      spec: ServiceSpec::new(r2),
      reply: reply_tx,
    },
    t,
  );

  // The reclaimed old-name goodbye SURVIVED the dropped-reply rollback: a TTL=0
  // goodbye is still emitted (without the fix it would have been cancelled and
  // nothing would be due — the new orphan service's withdrawal is empty).
  assert!(
    state
      .endpoint
      .poll_withdrawal_transmit(t, &mut buf)
      .is_some(),
    "the reclaimed old-name goodbye must survive the dropped-reply rollback and still emit"
  );

  drop(reg);
}

/// a terminal emitted DIRECTLY by the proto state machine — here
/// a `HostConflict` (a peer claimed our host name with a different address, RFC
/// 6762 §9) — must RETIRE the service through the SAME path as a synthesized
/// rename-collision Conflict: deliver the terminal into the handle-owned mailbox,
/// begin the endpoint-owned §10.1 withdrawal (so the proto stops serving), and GC
/// the ctx UNCONDITIONALLY once the withdrawal completes. Before the fix the
/// proto-emitted terminal was delivered to the mailbox but `withdrawing` was never
/// set and the withdrawal never began, so a HostConflict left a zombie ctx/route
/// (still answering queries) until the caller dropped the handle.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn proto_emitted_host_conflict_retires_and_gcs_the_service() {
  use std::{
    net::{IpAddr, Ipv4Addr, SocketAddr},
    time::Duration,
  };

  use mdns_proto::{
    event::RouteEvent,
    wire::{Header, MessageBuilder},
  };

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let host = mdns_proto::Name::try_from_str("printer.local.").unwrap();
  let mut r = mdns_proto::ServiceRecords::new(
    mdns_proto::Name::try_from_str("_ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("Printer._ipp._tcp.local.").unwrap(),
    host.clone(),
    631,
    120,
  );
  r.add_a(Ipv4Addr::new(192, 168, 1, 10));
  // Keep `reg` (the mailbox Arc + doorbell) alive: the live reader that must
  // still observe the HostConflict after the ctx is GC'd.
  let reg = state
    .register_service(mdns_proto::ServiceSpec::new(r), now)
    .unwrap();
  let handle = reg.handle;
  let mailbox = Arc::clone(&reg.mailbox);

  // 1. Drive the proto to announced (non-empty withdrawal snapshot; the conflict
  //    hits a SERVING service).
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    let mut buf = vec![0u8; 4096];
    let mut t = now;
    for _ in 0..40 {
      t += Duration::from_millis(300);
      let _ = ctx.proto.handle_timeout(t);
      while let Ok(Some(_)) = ctx.proto.poll_transmit(t, &mut buf) {
        ctx.proto.note_transmit_delivered(t);
      }
    }
  }

  // 2. Feed a §9 host conflict (a peer claims our host name with a DIFFERENT
  //    address) through the REAL inbound path, so the proto emits a HostConflict
  //    via poll() — the proto-emitted terminal `push_updates` must retire. This
  //    mirrors the driver's own receive routing (split-borrow + ToService).
  let conflict = {
    let mut cbuf = [0u8; 512];
    let mut b = MessageBuilder::<'_, 32>::try_new(&mut cbuf, Header::new()).unwrap();
    b.push_a_authority(&host, 120, Ipv4Addr::new(10, 0, 0, 99))
      .unwrap();
    let n = b.finish().unwrap();
    cbuf[..n].to_vec()
  };
  {
    let DriverState {
      endpoint, services, ..
    } = &mut state;
    let route_events = endpoint
      .handle(
        now,
        SocketAddr::from(([192, 168, 1, 200], 5353)),
        IpAddr::V4(Ipv4Addr::new(192, 168, 1, 10)),
        0,
        &conflict,
        false,
      )
      .expect("endpoint.handle must accept the host-conflict packet");
    for ev in route_events {
      if let Ok(RouteEvent::ToService(ts)) = ev
        && let Some(ctx) = services.get_mut(&ts.handle())
      {
        ctx.proto.handle_event(ts.into_event(), now);
      }
    }
  }

  // 3. push_updates drains the proto's HostConflict; the fix routes the terminal
  //    through retirement (deliver + begin the endpoint-owned withdrawal).
  state.push_updates(now).await;
  assert!(
    state
      .services
      .get(&handle)
      .map(|c| c.withdrawing)
      .unwrap_or(false),
    "a proto-emitted HostConflict must begin the withdrawal (withdrawing)"
  );

  // 4. Drive the withdrawal to completion; the ctx must be GC'd UNCONDITIONALLY
  //    (no bound family → force-complete at the 2 s anti-pin ceiling).
  let mut scratch = vec![0u8; 4096];
  let mut t = now;
  let mut gced = false;
  for _ in 0..64 {
    t += Duration::from_millis(250);
    state.drain_withdrawals(t, &mut scratch).await;
    if !state.services.contains_key(&handle) {
      gced = true;
      break;
    }
  }
  assert!(
    gced,
    "the withdrawn ctx must be GC'd after the §10.1 goodbye completes"
  );

  // 5. The HostConflict terminal survived the unconditional ctx GC: it lives in
  //    the handle-owned mailbox and is still drained by the live reader (the
  //    non-terminal Established, if any, drains first; the terminal is last).
  let mut saw_host_conflict = false;
  while let Some(u) = lock_mailbox_for_test(&mailbox) {
    if u.is_host_conflict() {
      saw_host_conflict = true;
    }
  }
  assert!(
    saw_host_conflict,
    "the HostConflict terminal must survive the ctx GC and stay readable from the \
       handle-owned mailbox"
  );

  drop(reg);
}

/// The terminal retirement update survives BOTH a saturated non-terminal ring
/// AND an immediate, unconditional ctx GC (the design-doc scenario; formerly
/// the deferral case). Fill the mailbox's non-terminal `updates` to the cap
/// WITHOUT draining, `set_terminal(Conflict)`, complete the withdrawal so the
/// ctx is GC'd immediately, then drain from the LIVE handle and assert the
/// `Conflict` IS observed and the ctx is gone from `services` — no park, no
/// leak.
#[cfg(feature = "tokio")]
#[tokio::test]
async fn terminal_survives_full_mailbox_and_immediate_ctx_gc() {
  use std::{net::Ipv4Addr, time::Duration};

  use mdns_proto::ServiceUpdate;

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let mut r = mdns_proto::ServiceRecords::new(
    mdns_proto::Name::try_from_str("_ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("stuck._ipp._tcp.local.").unwrap(),
    mdns_proto::Name::try_from_str("stuck.local.").unwrap(),
    631,
    120,
  );
  r.add_a(Ipv4Addr::new(192, 168, 1, 10));
  // Keep `reg` alive across the GC — it is the live reader.
  let reg = state
    .register_service(mdns_proto::ServiceSpec::new(r), now)
    .unwrap();
  let handle = reg.handle;
  let mailbox = Arc::clone(&reg.mailbox);

  // 1. Drive the proto to an announced state so the withdrawal snapshot is
  //    NON-empty (otherwise the withdrawal completes instantly).
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    let mut buf = vec![0u8; 4096];
    let mut t = now;
    for _ in 0..40 {
      t += Duration::from_millis(300);
      let _ = ctx.proto.handle_timeout(t);
      while let Ok(Some(_)) = ctx.proto.poll_transmit(t, &mut buf) {
        ctx.proto.note_transmit_delivered(t);
      }
    }
  }

  // 2. Saturate the non-terminal ring to the cap WITHOUT draining, then reserve
  //    the terminal. The terminal slot is independent of the (full) ring.
  {
    let mut mb = mailbox.lock().unwrap_or_else(|e| e.into_inner());
    mb.fill_non_terminal_to_cap_for_test();
    assert_eq!(
      mb.non_terminal_len(),
      SERVICE_UPDATE_CAPACITY,
      "the non-terminal ring must be saturated at the cap"
    );
    mb.set_terminal(ServiceUpdate::Conflict);
  }

  // 3. Begin the endpoint-owned withdrawal (rename-collision / encode-failure
  //    retirement arm). `push_updates` now skips this ctx.
  {
    let ctx = state.services.get_mut(&handle).unwrap();
    ctx.withdrawing = true;
    let snap = ctx.proto.withdrawal_snapshot();
    state.endpoint.begin_withdrawal(handle, snap, now);
  }

  // 4. Drive the withdrawal to completion. The ctx is GC'd IMMEDIATELY on
  //    completion — no park, no deferral, regardless of the full ring + the
  //    still-undrained reader.
  let mut scratch = vec![0u8; 4096];
  let mut t = now;
  let mut completed = false;
  for _ in 0..64 {
    t += Duration::from_millis(250);
    state.drain_withdrawals(t, &mut scratch).await;
    if !state.services.contains_key(&handle) {
      completed = true;
      break;
    }
  }
  assert!(
    completed,
    "the ctx must be GC'd unconditionally on withdrawal completion"
  );
  assert!(
    !state.services.contains_key(&handle),
    "no leak: the ctx must be gone from `services` after the withdrawal"
  );

  // 5. Drain from the live handle: all cap non-terminal updates, then the
  //    reserved Conflict — it was NEVER dropped despite the full ring and the
  //    immediate GC.
  let mut non_terminal = 0usize;
  let mut saw_conflict = false;
  loop {
    let drained = lock_mailbox_for_test(&mailbox);
    match drained {
      Some(ServiceUpdate::Conflict) => {
        saw_conflict = true;
        break;
      }
      Some(_) => non_terminal += 1,
      None => break,
    }
  }
  assert_eq!(
    non_terminal, SERVICE_UPDATE_CAPACITY,
    "the saturated non-terminal ring must drain in full before the terminal"
  );
  assert!(
    saw_conflict,
    "the reserved terminal Conflict must survive a full mailbox + an immediate, \
       unconditional ctx GC and reach the live reader"
  );

  drop(reg);
}

/// Registering the same instance name twice maps the proto
/// `RegisterServiceError::NameAlreadyRegistered` onto the public
/// `RegisterError::NameAlreadyRegistered` — exercising the `From` arm that
/// translates proto pool errors into the async-API error type. Sync path,
/// so no runtime is needed.
#[cfg(feature = "tokio")]
#[test]
fn duplicate_registration_maps_to_name_already_registered() {
  use std::net::Ipv4Addr;

  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let mk = || {
    let mut r = mdns_proto::ServiceRecords::new(
      mdns_proto::Name::try_from_str("_http._tcp.local.").unwrap(),
      mdns_proto::Name::try_from_str("dup._http._tcp.local.").unwrap(),
      mdns_proto::Name::try_from_str("dup.local.").unwrap(),
      80,
      120,
    );
    r.add_a(Ipv4Addr::new(192, 168, 1, 10));
    mdns_proto::ServiceSpec::new(r)
  };

  state.register_service(mk(), now).unwrap();
  // `ServiceRegistered` (the Ok type) is not `Debug`, so match instead of
  // `unwrap_err`.
  match state.register_service(mk(), now) {
    Err(crate::error::RegisterError::NameAlreadyRegistered(_)) => {}
    Err(e) => panic!("expected NameAlreadyRegistered, got error {e:?}"),
    Ok(_) => panic!("expected NameAlreadyRegistered, but the second registration succeeded"),
  }
}

/// On encode failure (`poll_query_transmit` → `Err`) the reactor driver must
/// call `endpoint.retire_query` so the proto records the terminal transition:
/// `queries_active` decrements to 0 and exactly one of `queries_done` /
/// `queries_timeout` reaches 1. The query slot must also be GC'd (removed
/// from the driver map) so late responses cannot mutate it, consistent with
/// the smoltcp driver which calls retire_query on this error class.
#[cfg(all(feature = "stats", feature = "tokio"))]
#[tokio::test]
async fn unencodable_query_retire_records_terminal_stats() {
  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  let qname = mdns_proto::Name::try_from_str("printer.local.").unwrap();
  let started = state
    .start_query(
      mdns_proto::QuerySpec::new(qname, mdns_proto::wire::ResourceType::A),
      now,
    )
    .unwrap();
  let h = started.handle;

  // Confirm one active query is registered in the proto.
  let before = state.stats.snapshot();
  assert_eq!(
    before.queries_active, 1,
    "one active query before encode failure"
  );
  assert_eq!(before.queries_done, 0, "no terminal yet");

  // Drive drain_transmits with a 1-byte scratch → encode fails for the
  // pending question → retire_query must be called.
  let mut scratch = vec![0u8; 1];
  state.drain_transmits(now, &mut scratch).await;

  // Stats invariant: queries_active == 0, queries_done == 1.
  let after = state.stats.snapshot();
  assert_eq!(
    after.queries_active, 0,
    "queries_active must be 0 after retire_query on encode failure (was leaking)"
  );
  assert_eq!(
    after.queries_done, 1,
    "exactly one terminal (queries_done) must be recorded after encode failure; \
       got queries_done={}, queries_timeout={}",
    after.queries_done, after.queries_timeout,
  );

  // The query slot must be GC'd so late answers cannot mutate retired state.
  assert!(
    !state.queries.contains_key(&h),
    "the retired query slot must be removed from the driver map"
  );

  // The terminal must be set in the mailbox so Query::next surfaces it.
  // Drive a full Query::next cycle: spin up a minimal loopback endpoint
  // with the existing mailbox + doorbell so the consumer can drain the
  // terminal without needing a live command channel.
  let mb = started.mailbox;
  let (cmd_tx, _cmd_rx) = async_channel::unbounded::<crate::command::Command>();
  let mut q = crate::query::Query::new(h, mb, started.doorbell, cmd_tx);
  // The doorbell was already rung by drain_transmits (terminal was pushed);
  // `Query::next` must surface QueryEvent::Terminal on this call.
  let event = tokio::time::timeout(std::time::Duration::from_millis(200), q.next())
    .await
    .expect("Query::next must complete (terminal is already in mailbox)")
    .expect("Query::next must return Some(Terminal), not None");
  assert!(
    matches!(event, crate::query::QueryEvent::Terminal(_)),
    "the first event from Query::next must be the terminal; got {event:?}"
  );
}

/// Regression test for the encode-retired query GC bypass under send pressure.
///
/// The bug: `drain_transmits` collected encode-failed query handles into
/// `encode_retired` but the per-handle credit check (`if credits_remaining ==
/// 0 { return true }`) inside the query loop could fire BEFORE the cleanup
/// block ran, leaving the retired handle resident in `queries` and proto
/// storage even though the terminal was already consumed.
///
/// The fix: replace that early `return true` with `more_pending = true; break`
/// so the GC block at the end of the function ALWAYS executes.
///
/// This test registers one encode-failing query (1-byte scratch) followed by
/// N normal queries (large scratch).  HashMap iteration order is
/// non-deterministic, so regardless of whether the encode-failing handle comes
/// first or last in the `handles` vec, the GC block must remove it by the time
/// `drain_transmits` returns.  With null sockets the credit counter never
/// reaches zero (sends return `used = 0`), so `more_pending` is `false` here;
/// the budget-exhaustion `break` path cannot be exercised without live
/// multicast sockets, but the structural invariant — that the GC block runs on
/// EVERY return path — is verified by the fix and by the code path taken here
/// (normal-completion path also runs the GC block, just like the break path).
///
/// Additionally asserts that the normal queries are still resident (their
/// mailboxes are still open so they haven't been retired), confirming that
/// only the encode-retired handle is cleaned up.
#[cfg(all(feature = "stats", feature = "tokio"))]
#[tokio::test]
async fn encode_retired_gc_runs_with_subsequent_queries_pending() {
  let opts = crate::options::ServerOptions::default();
  let sockets = BoundSockets::<agnostic_net::tokio::Net> {
    v4: None,
    v6: None,
    interface_index: 0,
  };
  let mut state = DriverState::new(&opts, sockets);
  let now = StdInstant::now();

  // Register the encode-failing query: 1-byte scratch ensures encode fails.
  let bad_qname = mdns_proto::Name::try_from_str("encode-fail.local.").unwrap();
  let bad_started = state
    .start_query(
      mdns_proto::QuerySpec::new(bad_qname, mdns_proto::wire::ResourceType::A),
      now,
    )
    .unwrap();
  let bad_h = bad_started.handle;

  // Register N additional queries. Keep the `QueryStarted` structs alive so
  // the doorbell receivers (held by `started.doorbell`) stay open; the
  // driver's liveness check (`!c.doorbell.is_closed()`) would skip any
  // query whose receiver was dropped.
  // N = 4 is enough to confirm the iteration order does not matter.
  let mut normal_started = Vec::new();
  for i in 0u8..4 {
    let name = mdns_proto::Name::try_from_str(&format!("normal-{i}.local.")).unwrap();
    let started = state
      .start_query(
        mdns_proto::QuerySpec::new(name, mdns_proto::wire::ResourceType::A),
        now,
      )
      .unwrap();
    normal_started.push(started);
  }
  let normal_handles: Vec<_> = normal_started.iter().map(|s| s.handle).collect();

  // Confirm five active queries in proto before the drain.
  let before = state.stats.snapshot();
  assert_eq!(before.queries_active, 5, "five active queries before drain");

  // 1-byte scratch → the encode-failing query fails to encode; normal queries
  // also fail (1 byte is too small for any DNS message), so all end up in
  // encode_retired.  This is acceptable: the assertion below checks that the
  // encode-failing handle is gone, irrespective of how many others fail too.
  let mut scratch = vec![0u8; 1];
  let more_pending = state.drain_transmits(now, &mut scratch).await;

  // `more_pending` is false because null sockets never exhaust credits.
  // The credit-exhaustion `break` path requires live multicast sockets and
  // cannot be reproduced deterministically in a unit test; the structural
  // fix (flag + single cleanup path) guarantees correctness on that path too.
  assert!(
    !more_pending,
    "null sockets never exhaust credits; more_pending must be false"
  );

  // The encode-retired query slot MUST be gone from the driver map.
  assert!(
    !state.queries.contains_key(&bad_h),
    "the encode-retired query handle must be removed from the driver map after drain_transmits"
  );

  // All queries saw encode failure (1-byte scratch), so proto counters must
  // reflect all terminals.
  let after = state.stats.snapshot();
  assert_eq!(
    after.queries_active, 0,
    "all five queries must be retired; queries_active must be 0"
  );
  // Five terminals (all queries_done — no timeout, encode fails immediately).
  assert_eq!(
    after.queries_done, 5,
    "five terminals (queries_done) must be recorded; \
       got queries_done={}, queries_timeout={}",
    after.queries_done, after.queries_timeout,
  );

  // The normal handles must also be GC'd (same 1-byte scratch → all fail).
  for &h in &normal_handles {
    assert!(
      !state.queries.contains_key(&h),
      "normal query handle {h:?} must also be removed (all encode-failed with 1-byte scratch)"
    );
  }
}

/// A consumed-oversized datagram (MSG_TRUNC / InvalidData) must bump
/// `packets_rx` AND `packets_dropped` — it was consumed off the socket so it
/// counts toward the receive denominator. `bytes_rx` rises by the buffer
/// capacity (best-effort, the actual payload bytes that landed in our buffer).
///
/// Tests `count_consumed_oversized` directly so no socket bind is needed.
#[cfg(feature = "stats")]
#[test]
fn consumed_oversized_datagram_counts_rx_and_dropped() {
  let stats = std::sync::Arc::new(hick_trace::stats::Stats::default());
  let buf_len: usize = 9000;

  count_consumed_oversized(&stats, buf_len);

  let snap = stats.snapshot();
  assert_eq!(
    snap.packets_rx, 1,
    "packets_rx must be 1 (datagram was consumed)"
  );
  assert_eq!(
    snap.bytes_rx, buf_len as u64,
    "bytes_rx must equal buf_len (best-effort truncated payload)"
  );
  assert_eq!(
    snap.packets_dropped, 1,
    "packets_dropped must be 1 (unusable datagram)"
  );
}

/// A generic recv error that consumed NO datagram must leave all counters at
/// zero — only consumed-unusable datagrams bump `packets_dropped`.
///
/// This mirrors the `handle_recv` path in compio: a socket/driver failure is
/// NOT a datagram event and must not pollute the stats.
#[cfg(feature = "stats")]
#[test]
fn generic_recv_error_does_not_increment_any_stats() {
  let stats = std::sync::Arc::new(hick_trace::stats::Stats::default());

  // Simulate the path taken by `recv_with_meta failed` / `peek_from failed`:
  // we log but do NOT call count_consumed_oversized.
  let _e = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "simulated");
  hick_trace::debug!(error = %_e, "recv_with_meta failed (test simulation — no stats bumped)");
  // (no stats call here — that IS the test)

  let snap = stats.snapshot();
  assert_eq!(
    snap.packets_rx, 0,
    "packets_rx must stay 0 on a generic recv error"
  );
  assert_eq!(
    snap.bytes_rx, 0,
    "bytes_rx must stay 0 on a generic recv error"
  );
  assert_eq!(
    snap.packets_dropped, 0,
    "packets_dropped must stay 0 on a generic recv error"
  );
}