zruntime 0.1.0

A simple single-threaded Rust async runtime
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
//! Tests of a runtime driven by nothing but the `block_on` calls made on it: the tasks, timers
//! and registrations it runs on the thread inside such a call, the wakes that reach that thread
//! from others, and the rules on who may drive it.
//!
//! Most of these run in both flavours, through [`in_both_modes`]; the ones that only mean
//! something in one flavour — a local task holding an `Rc`, a second thread asking to drive a
//! shared runtime — are written for that flavour alone.

use std::{
    any::Any,
    cell::{Cell, RefCell},
    future::{Future, Pending, pending, poll_fn},
    io::{self, Read, Write},
    mem::MaybeUninit,
    panic::{AssertUnwindSafe, catch_unwind},
    pin::{Pin, pin},
    rc::Rc,
    sync::{
        Arc, Mutex, PoisonError,
        atomic::{AtomicBool, AtomicUsize, Ordering},
        mpsc,
    },
    task::{Context, Poll, Wake, Waker},
    thread,
    time::{Duration, Instant},
};

use futures::{Stream, StreamExt, executor::block_on, future::poll_immediate};
use ntest::timeout;
use socket2::{SockRef, Socket};

use super::{or, yield_now};
#[cfg(feature = "event")]
use crate::Event;
use crate::{
    Interest, Interval, Local, LocalRuntime, MissedTickBehavior, Mode, Registration, Runtime,
    Shared, SharedRuntime, Sleep, Task, TimedOut, Timeout,
};

/// Writes the test that follows once per flavour: a module named after it, holding a `local`
/// and a `shared` test that run its body with the mode parameter set to [`Local`] and to
/// [`Shared`].
///
/// The body reaches spawning and registering through [`TestMode`], whose bounds both flavours
/// meet, and everything else through [`Runtime`] itself.
macro_rules! in_both_modes {
    ($(#[$attr:meta])* fn $name:ident<$mode:ident>() $body:block) => {
        $(#[$attr])*
        mod $name {
            use super::*;

            #[test]
            #[timeout(15000)]
            fn local() {
                run::<Local>();
            }

            #[test]
            #[timeout(15000)]
            fn shared() {
                run::<Shared>();
            }

            fn run<$mode>()
            where
                $mode: TestMode,
            $body
        }
    };
}

in_both_modes! {
    fn a_spawned_task_hands_its_output_back<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let task = M::spawn(&runtime, "an answer", async { 42 });

        assert_eq!(runtime.block_on(task).unwrap(), 42);
    }
}

in_both_modes! {
    /// A task runs on the thread inside `block_on`, whether it was spawned from inside the call
    /// or before it.
    fn tasks_run_on_the_thread_inside_block_on<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let before = M::spawn(&runtime, "a task spawned before the call", async {
            thread::current().id()
        });

        let (before, inside) = runtime.block_on(async {
            let inside = M::spawn(&runtime, "a task spawned inside the call", async {
                thread::current().id()
            });

            (before.await.unwrap(), inside.await.unwrap())
        });

        assert_eq!(before, thread::current().id());
        assert_eq!(inside, thread::current().id());
    }
}

in_both_modes! {
    /// Work handed to a runtime nobody is driving waits for the next `block_on` on it.
    fn work_waits_for_the_next_block_on<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let ran = Arc::new(AtomicBool::new(false));
        let task = {
            let ran = ran.clone();
            M::spawn(&runtime, "a task that sets a flag", async move {
                ran.store(true, Ordering::Release);
            })
        };

        // Long enough for a thread of the runtime's own, were there one, to have run the task.
        thread::sleep(Duration::from_millis(50));
        assert!(!ran.load(Ordering::Acquire));

        runtime.block_on(task).unwrap();
        assert!(ran.load(Ordering::Acquire));
    }
}

in_both_modes! {
    #[cfg(feature = "event")]
    fn a_detached_task_runs_to_completion<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let done = Arc::new(Event::new());
        // Taken before the task is spawned, so that the announcement cannot be missed.
        let announced = done.listen();

        M::spawn(&runtime, "a detached task", async move {
            // Left waiting once, so that it is the detached task's later poll that ends it.
            yield_now().await;
            done.notify(1);
        })
        .detach();

        runtime.block_on(announced);
    }
}

in_both_modes! {
    fn dropping_a_task_cancels_and_drops_its_future<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let dropped = Arc::new(AtomicBool::new(false));
        let task = {
            let marker = SetOnDrop(dropped.clone());
            M::spawn(&runtime, "a task that never finishes", async move {
                let _marker = marker;
                pending::<()>().await;
            })
        };
        // A yield is one round of the loop, in which the task is polled and left waiting.
        runtime.block_on(yield_now());
        assert!(!dropped.load(Ordering::Acquire));

        drop(task);

        assert!(dropped.load(Ordering::Acquire));
    }
}

in_both_modes! {
    fn a_panicking_task_fails_its_handle_and_the_runtime_carries_on<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let panicking = M::spawn(&runtime, "a task that panics", async {
            panic!("the task panicked on purpose");
        });

        let error = runtime.block_on(panicking).unwrap_err();

        assert_eq!(error.kind(), io::ErrorKind::Other);
        let next = M::spawn(&runtime, "the task after it", async { 7 });
        assert_eq!(runtime.block_on(next).unwrap(), 7);
    }
}

in_both_modes! {
    /// A future whose destructor panics takes nothing with it: not a value it had already
    /// produced, not the runtime that drops it, and not the handle that cancels it, which sees
    /// the panic itself.
    fn a_panicking_destructor_is_contained<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        // A future of its own rather than an async block holding the marker: the block would drop
        // the marker inside its last poll, which is a panic of that poll.
        let going = M::spawn(&runtime, "a task that panics as it goes", ReadyThenPanicOnDrop(7));

        assert_eq!(runtime.block_on(going).unwrap(), 7);

        let cancelled = M::spawn(&runtime, "a task that never finishes", async {
            let _marker = PanicOnDrop;
            pending::<()>().await;
        });
        runtime.block_on(yield_now());
        // Idle when it is dropped, so the drop of its future is this thread's to make, and the
        // panic is the caller's to see.
        assert!(catch_unwind(AssertUnwindSafe(|| drop(cancelled))).is_err());

        let next = M::spawn(&runtime, "the task after them", async { 7 });
        assert_eq!(runtime.block_on(next).unwrap(), 7);
    }
}

in_both_modes! {
    /// A task is finished once its future has completed, whether or not anyone has awaited it
    /// yet, and stays so once its output is taken; one that is left waiting is not.
    fn a_task_is_finished_once_its_future_has_completed<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let mut completing = M::spawn(&runtime, "a task that finishes at once", async { 42 });
        let waiting = M::spawn(&runtime, "a task that never finishes", pending::<()>());
        // Neither has had its first poll.
        assert!(!completing.is_finished());
        assert!(!waiting.is_finished());

        // A yield is one round of the loop, in which each task is polled once.
        runtime.block_on(yield_now());

        assert!(completing.is_finished());
        assert!(!waiting.is_finished());
        assert_eq!(runtime.block_on(&mut completing).unwrap(), 42);
        assert!(completing.is_finished());
    }
}

in_both_modes! {
    /// A task that panics is finished, as one that completes is, and its handle reports the
    /// failure all the same.
    fn a_panicking_task_is_finished<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let panicking = M::spawn(&runtime, "a task that panics", async {
            panic!("the task panicked on purpose");
        });
        assert!(!panicking.is_finished());

        runtime.block_on(yield_now());

        assert!(panicking.is_finished());
        assert!(runtime.block_on(panicking).is_err());
    }
}

in_both_modes! {
    /// A task whose runtime goes before it ends is finished, whether it was polled before the
    /// runtime went or never was.
    fn a_task_is_finished_once_its_runtime_is_gone<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let polled = M::spawn(&runtime, "a task that never finishes", pending::<()>());
        runtime.block_on(yield_now());
        let never_polled = M::spawn(&runtime, "a task that is never polled", pending::<()>());
        assert!(!polled.is_finished());
        assert!(!never_polled.is_finished());

        drop(runtime);

        assert!(polled.is_finished());
        assert!(never_polled.is_finished());
    }
}

/// A task is not finished while its future is still being dropped, whether it ran to its end or
/// went with its runtime, polled before that or not: what the future held is gone by the time
/// anyone is told that the task is over.
#[test]
#[timeout(15000)]
fn a_local_task_is_finished_only_once_its_future_is_dropped() {
    for end in End::ALL {
        let noted =
            finished_as_future_drops(LocalRuntime::new().unwrap(), end, |runtime, future| {
                runtime.spawn("a task that notes whether it is finished", future)
            });

        assert_eq!(noted, Some(false), "{end:?}");
    }
}

/// A task is not finished while its future is still being dropped, whether it ran to its end or
/// went with its runtime, polled before that or not: what the future held is gone by the time
/// anyone is told that the task is over.
#[test]
#[timeout(15000)]
fn a_shared_task_is_finished_only_once_its_future_is_dropped() {
    for end in End::ALL {
        let noted =
            finished_as_future_drops(SharedRuntime::new().unwrap(), end, |runtime, future| {
                runtime.spawn("a task that notes whether it is finished", future)
            });

        assert_eq!(noted, Some(false), "{end:?}");
    }
}

in_both_modes! {
    /// A task cancelled before its first poll is never polled, and its future is gone by the time
    /// `cancel` returns, which leaves the wait nothing to wait for.
    fn cancelling_a_task_never_polled_drops_its_future_in_the_call<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let polled = Arc::new(AtomicBool::new(false));
        let dropped = Arc::new(AtomicBool::new(false));
        let task = {
            let polled = polled.clone();
            let marker = SetOnDrop(dropped.clone());
            M::spawn(&runtime, "a task that is never polled", async move {
                let _marker = marker;
                polled.store(true, Ordering::Release);
            })
        };

        let cancelling = task.cancel();

        assert!(dropped.load(Ordering::Acquire));
        // A round of the runtime, which comes to the id the task left in its queue and skips it.
        runtime.block_on(yield_now());
        assert!(!polled.load(Ordering::Acquire));
        assert_eq!(runtime.block_on(poll_immediate(cancelling)), Some(None));
    }
}

in_both_modes! {
    /// A task cancelled while it waits has its future dropped in the call, on the thread that
    /// cancels it, and the wait is over as soon as it is polled.
    fn cancelling_a_waiting_task_drops_its_future_in_the_call<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let dropped = Arc::new(AtomicBool::new(false));
        let task = {
            let marker = SetOnDrop(dropped.clone());
            M::spawn(&runtime, "a task that never finishes", async move {
                let _marker = marker;
                pending::<u32>().await
            })
        };
        // A yield is one round of the loop, in which the task is polled and left waiting.
        runtime.block_on(yield_now());
        assert!(!dropped.load(Ordering::Acquire));

        let cancelling = task.cancel();

        assert!(dropped.load(Ordering::Acquire));
        assert_eq!(block_on(poll_immediate(cancelling)), Some(None));
    }
}

in_both_modes! {
    /// A task that finished before it was cancelled hands its output back, which nobody had
    /// taken yet.
    fn cancelling_a_finished_task_hands_its_output_back<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let task = M::spawn(&runtime, "an answer", async { 42 });
        runtime.block_on(yield_now());
        assert!(task.is_finished());

        assert_eq!(block_on(poll_immediate(task.cancel())), Some(Some(42)));
    }
}

in_both_modes! {
    /// A task that panicked has no output to hand back.
    fn cancelling_a_panicked_task_hands_nothing_back<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let task = M::spawn(&runtime, "a task that panics", async {
            panic!("the task panicked on purpose");
        });
        runtime.block_on(yield_now());
        assert!(task.is_finished());

        let resolved = block_on(poll_immediate(task.cancel()));

        assert!(resolved.is_some_and(|output| output.is_none()));
    }
}

in_both_modes! {
    /// A task whose runtime went before it ended has no output to hand back, and its future is
    /// gone already, so there is nothing to wait for.
    fn cancelling_a_task_whose_runtime_is_gone_hands_nothing_back<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let task = M::spawn(&runtime, "a task that never finishes", pending::<u32>());
        runtime.block_on(yield_now());

        drop(runtime);

        assert_eq!(block_on(poll_immediate(task.cancel())), Some(None));
    }
}

in_both_modes! {
    /// A task whose output was taken by awaiting it has nothing left to hand back, and the wait
    /// is over at once rather than waiting for an outcome that has come and gone.
    fn cancelling_a_task_already_awaited_hands_nothing_back<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let mut task = M::spawn(&runtime, "an answer", async { 42 });
        assert_eq!(runtime.block_on(&mut task).unwrap(), 42);

        assert_eq!(block_on(poll_immediate(task.cancel())), Some(None));
    }
}

in_both_modes! {
    /// A future whose destructor panics as `cancel` drops it panics out of that call, as it does
    /// out of a drop of the task, and takes nothing else with it.
    fn a_panicking_destructor_panics_out_of_cancel<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let task = M::spawn(&runtime, "a task that never finishes", async {
            let _marker = PanicOnDrop;
            pending::<()>().await;
        });
        // Idle when it is cancelled, so the drop of its future is this thread's to make, and the
        // panic is the caller's to see.
        runtime.block_on(yield_now());

        assert!(catch_unwind(AssertUnwindSafe(|| task.cancel())).is_err());

        let next = M::spawn(&runtime, "the task after it", async { 7 });
        assert_eq!(runtime.block_on(next).unwrap(), 7);
    }
}

/// A task may cancel itself from inside its own poll, and await that: the wait leaves the poll
/// pending, and the future is dropped once that poll has returned, the wait with it, while the
/// runtime carries on.
#[test]
#[timeout(15000)]
fn a_local_task_may_cancel_itself() {
    let runtime = LocalRuntime::new().unwrap();
    let own = Rc::new(RefCell::new(None::<Task<()>>));
    let log = Rc::new(RefCell::new(Vec::new()));
    let dropped = Arc::new(AtomicBool::new(false));
    let task = {
        let own = own.clone();
        let log = log.clone();
        let marker = SetOnDrop(dropped.clone());
        runtime.spawn("a task that cancels itself", async move {
            let _marker = marker;
            let task = own.borrow_mut().take().expect("the task's own handle");
            let cancelling = task.cancel();
            log.borrow_mut().push("cancelled");
            cancelling.await;
            log.borrow_mut().push("resumed");
        })
    };
    *own.borrow_mut() = Some(task);

    runtime.block_on(yield_now());

    assert_eq!(*log.borrow(), ["cancelled"]);
    assert!(dropped.load(Ordering::Acquire));
    let next = runtime.spawn("the task after it", async { 7 });
    assert_eq!(runtime.block_on(next).unwrap(), 7);
}

/// A task may cancel itself from inside its own poll, and await that: the wait leaves the poll
/// pending, and the future is dropped once that poll has returned, the wait with it, while the
/// runtime carries on. Nothing of the task is left behind then: the waker the wait registered is
/// the task's own, and dropping the cancelled handle lets go of it, or the task would keep itself,
/// and the runtime's remote with it, alive for good.
#[test]
#[timeout(15000)]
fn a_shared_task_may_cancel_itself() {
    let runtime = SharedRuntime::new().unwrap();
    let remotes = Arc::strong_count(&runtime.core.remote);
    let own = Arc::new(Mutex::new(None::<Task<(), Shared>>));
    let log = Arc::new(Mutex::new(Vec::new()));
    let dropped = Arc::new(AtomicBool::new(false));
    let task = {
        let own = own.clone();
        let log = log.clone();
        let marker = SetOnDrop(dropped.clone());
        runtime.spawn("a task that cancels itself", async move {
            let _marker = marker;
            let task = own.lock().unwrap().take().expect("the task's own handle");
            let cancelling = task.cancel();
            log.lock().unwrap().push("cancelled");
            cancelling.await;
            log.lock().unwrap().push("resumed");
        })
    };
    *own.lock().unwrap() = Some(task);

    runtime.block_on(yield_now());

    assert_eq!(*log.lock().unwrap(), ["cancelled"]);
    assert!(dropped.load(Ordering::Acquire));
    assert_eq!(Arc::strong_count(&runtime.core.remote), remotes);
    let next = runtime.spawn("the task after it", async { 7 });
    assert_eq!(runtime.block_on(next).unwrap(), 7);
}

/// A task cancelled as its runtime drops it — from the destructor of its own future, here — is
/// waited for until its future is gone, though the runtime can no longer be reached to stop it:
/// the runtime is dropping that future already, and the wait lasts until it has.
#[test]
#[timeout(15000)]
fn a_task_cancelled_as_its_runtime_goes_is_waited_for_until_its_future_is_gone() {
    let runtime = LocalRuntime::new().unwrap();
    let own = Rc::new(RefCell::new(None));
    let cancelling = Rc::new(RefCell::new(None));
    let pending_at_once = Rc::new(Cell::new(None));
    let task = {
        let canceller = CancelOnDrop {
            task: own.clone(),
            cancelling: cancelling.clone(),
            pending_at_once: pending_at_once.clone(),
        };
        runtime.spawn("a task that cancels itself as it goes", async move {
            let _canceller = canceller;
            pending::<()>().await;
        })
    };
    *own.borrow_mut() = Some(task);
    runtime.block_on(yield_now());

    drop(runtime);

    // The future was still being dropped when the wait was first polled...
    assert_eq!(pending_at_once.get(), Some(true));
    // ...and is gone by now.
    let cancelling = cancelling.borrow_mut().take().unwrap();
    assert_eq!(block_on(poll_immediate(cancelling)), Some(None));
}

/// A task cancelled while another thread polls it is dropped by that thread once its poll
/// returns, and the wait for it lasts until then. Where that poll leaves the task waiting, there
/// is no output to hand back.
#[test]
#[timeout(15000)]
fn cancelling_a_task_another_thread_polls_waits_for_that_poll() {
    let runtime = SharedRuntime::new().unwrap();
    let dropped = Arc::new(AtomicBool::new(false));
    let marker = SetOnDrop(dropped.clone());
    let (task, release, driver) = held_in_its_poll(&runtime, async move {
        let _marker = marker;
        pending::<u32>().await
    });

    let mut cancelling = pin!(task.cancel());

    assert!(block_on(poll_immediate(cancelling.as_mut())).is_none());
    assert!(!dropped.load(Ordering::Acquire));
    release.send(()).unwrap();
    assert_eq!(block_on(cancelling), None);
    assert!(dropped.load(Ordering::Acquire));
    driver.join().unwrap();
}

/// A task cancelled while another thread polls it, whose poll then finishes it, hands its output
/// back once that thread has dropped its future: the task finished before the cancellation
/// reached it.
#[test]
#[timeout(15000)]
fn cancelling_a_task_another_thread_polls_to_its_end_hands_its_output_back() {
    let runtime = SharedRuntime::new().unwrap();
    let dropped = Arc::new(AtomicBool::new(false));
    let marker = SetOnDrop(dropped.clone());
    let (task, release, driver) = held_in_its_poll(&runtime, async move {
        let _marker = marker;
        42
    });

    let mut cancelling = pin!(task.cancel());

    assert!(block_on(poll_immediate(cancelling.as_mut())).is_none());
    assert!(!dropped.load(Ordering::Acquire));
    release.send(()).unwrap();
    assert_eq!(block_on(cancelling), Some(42));
    assert!(dropped.load(Ordering::Acquire));
    driver.join().unwrap();
}

/// A task cancelled while another thread polls it, whose future's destructor panics as that
/// thread drops it, ends the wait all the same: the panic is contained there, and the task is
/// still told to be over.
#[test]
#[timeout(15000)]
fn cancelling_a_task_whose_destructor_panics_on_another_thread_ends_the_wait() {
    let runtime = SharedRuntime::new().unwrap();
    let (task, release, driver) = held_in_its_poll(&runtime, async {
        let _marker = PanicOnDrop;
        pending::<u32>().await
    });

    let mut cancelling = pin!(task.cancel());

    assert!(block_on(poll_immediate(cancelling.as_mut())).is_none());
    release.send(()).unwrap();
    assert_eq!(block_on(cancelling), None);
    driver.join().unwrap();
}

/// Dropping the wait for a cancelled task lets go of the task's outcome, as dropping the task
/// does: a task that finishes in the poll it was cancelled during drops its output there, rather
/// than leave it to whoever kept a clone of its waker.
#[test]
#[timeout(15000)]
fn dropping_the_wait_for_a_cancelled_task_lets_its_output_go() {
    let runtime = SharedRuntime::new().unwrap();
    let kept = Arc::new(Mutex::new(None));
    let output_dropped = Arc::new(AtomicBool::new(false));
    let (task, release, driver) = {
        let kept = kept.clone();
        let output = SetOnDrop(output_dropped.clone());
        held_in_its_poll(&runtime, async move {
            let waker = poll_fn(|cx| Poll::Ready(cx.waker().clone())).await;
            *kept.lock().unwrap() = Some(waker);

            output
        })
    };

    drop(task.cancel());
    release.send(()).unwrap();
    driver.join().unwrap();

    assert!(kept.lock().unwrap().is_some());
    assert!(output_dropped.load(Ordering::Acquire));
}

/// The wait for a cancelled task on a shared runtime may be sent to, and awaited on, another
/// thread, as the task itself may.
#[test]
#[timeout(15000)]
fn the_wait_for_a_cancelled_shared_task_crosses_threads() {
    let runtime = SharedRuntime::new().unwrap();
    let cancelling = runtime
        .spawn("a task that never finishes", pending::<u32>())
        .cancel();

    let resolved = thread::spawn(move || block_on(cancelling)).join().unwrap();

    assert_eq!(resolved, None);
}

in_both_modes! {
    /// A wake from another thread ends the wait the thread inside `block_on` is in.
    ///
    /// Nothing to watch and no deadline, so the wait is bounded by nothing but the notification
    /// the wake writes.
    fn a_wake_from_another_thread_ends_the_wait<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let wakers = Arc::new(Mutex::new(None::<Waker>));
        let waker_thread = {
            let wakers = wakers.clone();
            thread::spawn(move || {
                thread::sleep(Duration::from_millis(50));
                let waker = wakers.lock().unwrap_or_else(PoisonError::into_inner).take();
                if let Some(waker) = waker {
                    waker.wake();
                }
            })
        };
        let started = Instant::now();

        runtime.block_on(async {
            let mut polled = false;
            poll_fn(|cx| {
                if polled {
                    return Poll::Ready(());
                }
                polled = true;
                *wakers.lock().unwrap_or_else(PoisonError::into_inner) = Some(cx.waker().clone());

                Poll::Pending
            })
            .await
        });

        assert!(started.elapsed() >= Duration::from_millis(50));
        assert!(started.elapsed() < Duration::from_secs(5));
        waker_thread.join().unwrap();
    }
}

in_both_modes! {
    fn sleep_resolves_once_the_duration_has_passed<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();

        runtime.block_on(runtime.sleep(Duration::from_millis(50)));

        assert!(started.elapsed() >= Duration::from_millis(50));
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A sleep shorter than a millisecond ends well before a millisecond has passed: the wait
    /// does not round its timeout up to whole milliseconds, as epoll's own timeout would, which
    /// would have every one of these sleeps take a millisecond at least. The bound leaves several
    /// times the sleep's length for a busy machine to wake the thread late.
    ///
    /// Written for Linux and Android, whose epoll takes a timeout in milliseconds; other kernels
    /// may let timers that close together go off at once, a millisecond late.
    #[cfg(any(target_os = "linux", target_os = "android"))]
    fn a_short_sleep_does_not_wait_a_whole_millisecond<M>() {
        const ROUNDS: u32 = 20;
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();

        for _ in 0..ROUNDS {
            runtime.block_on(runtime.sleep(Duration::from_micros(100)));
        }

        assert!(started.elapsed() < Duration::from_millis(1) * ROUNDS);
    }
}

in_both_modes! {
    fn sleep_until_resolves_once_the_deadline_has_passed<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();
        let deadline = started + Duration::from_millis(50);

        runtime.block_on(runtime.sleep_until(deadline));

        assert!(Instant::now() >= deadline);
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A deadline that has passed by the first poll is no reason to wait: the timer completes
    /// on that poll, with nothing left behind for the reactor to fire.
    fn sleep_until_a_passed_deadline_is_ready_on_its_first_poll<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        // The clock has moved on by the time of the poll, however little.
        let deadline = Instant::now();

        let polled = runtime.block_on(poll_immediate(runtime.sleep_until(deadline)));

        assert!(polled.is_some());
    }
}

in_both_modes! {
    /// A timer says when it comes due, whichever way it was asked for, and has no such moment
    /// where the clock cannot name one.
    fn a_sleep_reports_its_deadline<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let deadline = Instant::now() + Duration::from_secs(60);

        assert_eq!(runtime.sleep_until(deadline).deadline(), Some(deadline));
        assert!(runtime.sleep(Duration::MAX).deadline().is_none());
    }
}

in_both_modes! {
    /// A reset to a later deadline holds a timer back until that deadline, though the timer was
    /// already waiting on the earlier one.
    fn a_sleep_reset_to_a_later_deadline_resolves_at_it<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();

        runtime.block_on(async {
            let mut sleep = runtime.sleep(Duration::from_millis(10));
            // Polled first, so that the reset moves a deadline the runtime already waits on.
            assert!(poll_immediate(&mut sleep).await.is_none());
            sleep.reset_after(Duration::from_millis(50));
            sleep.await;
        });

        assert!(started.elapsed() >= Duration::from_millis(50));
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A reset to an earlier deadline brings a waiting timer forward, rather than leaving it to
    /// the deadline it was polled with.
    fn a_sleep_reset_to_an_earlier_deadline_resolves_at_it<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();

        runtime.block_on(async {
            let mut sleep = runtime.sleep(Duration::from_secs(10));
            assert!(poll_immediate(&mut sleep).await.is_none());
            sleep.reset_after(Duration::from_millis(20));
            sleep.await;
        });

        assert!(started.elapsed() >= Duration::from_millis(20));
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A timer that has completed completes again once reset, at its new deadline.
    fn a_completed_sleep_reset_completes_again<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let gap = Duration::from_millis(20);

        let since_the_reset = runtime.block_on(async {
            let mut sleep = runtime.sleep(Duration::from_millis(5));
            (&mut sleep).await;
            let reset = Instant::now();
            sleep.reset_after(gap);
            sleep.await;

            reset.elapsed()
        });

        assert!(since_the_reset >= gap);
        assert!(since_the_reset < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A task waiting on a timer is woken at the deadline the timer is reset to, though nothing
    /// polls the timer after the reset: the waker it was polled with moves with the deadline.
    ///
    /// That waker is one the test counts the wakes of, rather than the `block_on` call's own,
    /// which would show nothing: the call polls its future again after every wait, whatever
    /// ended it.
    fn a_reset_wakes_the_waiting_task_at_the_new_deadline<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let mut sleep = runtime.sleep(Duration::from_secs(10));
        let (counter, waker) = counting_waker();
        let mut polled = false;
        let started = Instant::now();

        runtime.block_on(poll_fn(|_| {
            if !polled {
                polled = true;
                let first = Pin::new(&mut sleep).poll(&mut Context::from_waker(&waker));
                assert!(first.is_pending());
                sleep.reset_after(Duration::from_millis(20));

                return Poll::Pending;
            }
            // The timer is never polled again, so what ends the call is the counted waker, woken
            // at the new deadline by the wake that moved with it. Had the reset let that waker
            // go instead, nothing would ever count, and once the new deadline had passed, no
            // deadline would be left to bound the wait: the call would never end.
            if counter.count() > 0 {
                Poll::Ready(())
            } else {
                Poll::Pending
            }
        }));

        assert!(started.elapsed() >= Duration::from_millis(20));
        assert!(started.elapsed() < Duration::from_secs(5));
        assert_eq!(counter.count(), 1);
    }
}

in_both_modes! {
    fn a_timeout_hands_back_what_the_future_produced<M>() {
        let runtime = Runtime::<M>::new().unwrap();

        let output = runtime.block_on(runtime.timeout(Duration::from_secs(10), async { 7 }));

        assert_eq!(output, Ok(7));
    }
}

in_both_modes! {
    fn a_timeout_on_a_future_that_never_completes_times_out<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();

        let output = runtime.block_on(runtime.timeout(Duration::from_millis(50), pending::<()>()));

        assert_eq!(output, Err(TimedOut));
        assert!(started.elapsed() >= Duration::from_millis(50));
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A deadline that has passed by the first poll times a waiting future out on that poll,
    /// with no wait for the reactor to fire it.
    fn a_timeout_at_a_passed_deadline_times_out_on_its_first_poll<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let timeout = runtime.timeout_at(Instant::now(), pending::<()>());

        let polled = runtime.block_on(poll_immediate(timeout));

        assert_eq!(polled, Some(Err(TimedOut)));
    }
}

in_both_modes! {
    /// The future has its turn before the deadline is looked at, so one that is ready hands back
    /// its output even where the deadline has passed already.
    fn a_timeout_polls_the_future_before_the_deadline<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let timeout = runtime.timeout_at(Instant::now(), async { 7 });

        let polled = runtime.block_on(poll_immediate(timeout));

        assert_eq!(polled, Some(Ok(7)));
    }
}

in_both_modes! {
    /// A future that cannot be moved once polled, an async block that holds a borrow of its own
    /// across a wait, is run in place.
    fn a_timeout_runs_a_future_that_is_not_unpin<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let started = Instant::now();

        let output = runtime.block_on(runtime.timeout(Duration::from_secs(10), async {
            let values = [3, 5, 7];
            let last = &values[2];
            runtime.sleep(Duration::from_millis(10)).await;

            *last
        }));

        assert_eq!(output, Ok(7));
        assert!(started.elapsed() >= Duration::from_millis(10));
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// A future that ran out of time is not dropped with it: the timeout hands it back, to be
    /// driven to completion with no deadline at all.
    fn a_timeout_hands_back_its_future_after_timing_out<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        // Shut until the time-out, so that the future cannot win the race against the deadline
        // however slowly this runs.
        let open = Cell::new(false);
        let gate = Box::pin(poll_fn(|_| {
            if open.get() {
                Poll::Ready(7)
            } else {
                Poll::Pending
            }
        }));
        let mut timeout = runtime.timeout(Duration::from_millis(10), gate);

        assert_eq!(runtime.block_on(&mut timeout), Err(TimedOut));
        open.set(true);
        let output = runtime.block_on(timeout.into_inner());

        assert_eq!(output, 7);
    }
}

/// A time-out is an I/O error of the kind made for it, which carries the time-out itself, so
/// that code returning an `io::Result` can hand one on with `?`.
#[test]
#[timeout(15000)]
fn timed_out_converts_into_an_io_error_of_its_kind() {
    let error = io::Error::from(TimedOut);

    assert_eq!(error.kind(), io::ErrorKind::TimedOut);
    assert!(error.get_ref().is_some_and(|inner| inner.is::<TimedOut>()));
}

in_both_modes! {
    /// Each tick is the moment it was scheduled for, a period after the one before, the first one
    /// period after the interval was made.
    fn an_interval_ticks_once_a_period<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(20);
        let started = Instant::now();

        let ticks = runtime.block_on(async {
            let mut interval = runtime.interval(period);
            [interval.tick().await, interval.tick().await, interval.tick().await]
        });

        assert!(ticks[0] >= started + period);
        assert!(ticks[0] < started + Duration::from_secs(1));
        assert_eq!(ticks[1] - ticks[0], period);
        assert_eq!(ticks[2] - ticks[1], period);
        assert!(started.elapsed() >= 3 * period);
        assert!(started.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    /// An interval started at a moment that has passed already ticks on its first poll, with that
    /// moment, and keeps to the schedule from it.
    fn an_interval_at_a_passed_start_ticks_at_once<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(20);
        let start = Instant::now();
        let mut interval = runtime.interval_at(start, period);

        let first = runtime.block_on(poll_immediate(interval.tick()));
        let second = runtime.block_on(interval.tick());

        assert_eq!(first, Some(start));
        assert_eq!(second, start + period);
        assert!(Instant::now() >= start + period);
    }
}

in_both_modes! {
    fn an_interval_is_a_stream_of_its_ticks<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(5);
        let start = Instant::now() + period;
        let interval = runtime.interval_at(start, period);

        let ticks: Vec<Instant> = runtime.block_on(interval.take(3).collect());

        assert_eq!(ticks, [start, start + period, start + 2 * period]);
    }
}

in_both_modes! {
    /// Ticks missed while the thread was blocked are handed out one after the other, each with
    /// the moment it was scheduled for, so the schedule is kept.
    fn an_interval_bursts_through_missed_ticks<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(20);

        let (first, after) = runtime.block_on(async {
            let mut interval = runtime.interval(period);
            assert_eq!(interval.missed_tick_behavior(), MissedTickBehavior::Burst);
            let first = interval.tick().await;
            // Blocked rather than waiting, so that the runtime cannot hand out a tick meanwhile.
            thread::sleep(period * 7 / 2);

            (first, [interval.tick().await, interval.tick().await, interval.tick().await])
        });

        assert_eq!(after, [first + period, first + 2 * period, first + 3 * period]);
    }
}

in_both_modes! {
    /// After a tick missed, the next one comes a full period after the late one was handed out.
    fn an_interval_delays_after_missed_ticks<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(20);

        let (stalled_until, late, next) = runtime.block_on(async {
            let mut interval = runtime.interval(period);
            interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
            interval.tick().await;
            thread::sleep(period * 7 / 2);
            let stalled_until = Instant::now();

            (stalled_until, interval.tick().await, interval.tick().await)
        });

        assert!(late < stalled_until);
        assert!(next >= stalled_until + period);
        assert!(Instant::now() >= next);
    }
}

in_both_modes! {
    /// After ticks missed, the next one is the first of the original schedule still to come.
    fn an_interval_skips_missed_ticks<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(20);

        let (first, stalled_until, late, next) = runtime.block_on(async {
            let mut interval = runtime.interval(period);
            interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
            let first = interval.tick().await;
            thread::sleep(period * 7 / 2);
            let stalled_until = Instant::now();

            (first, stalled_until, interval.tick().await, interval.tick().await)
        });

        // The tick that was due as the stall began is handed out late, and those missed after it
        // are not: the one after it is the first still to come once the stall was over.
        assert!(late < stalled_until);
        assert!(next > stalled_until);
        assert_eq!((late - first).as_nanos() % period.as_nanos(), 0);
        assert_eq!((next - first).as_nanos() % period.as_nanos(), 0);
    }
}

in_both_modes! {
    /// A reset starts the schedule over, one period from the reset, however close the tick it
    /// replaces was.
    fn an_interval_reset_ticks_a_period_after_it<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let period = Duration::from_millis(20);
        let mut interval = runtime.interval_at(Instant::now(), period);

        let reset = Instant::now();
        interval.reset();
        let tick = runtime.block_on(interval.tick());

        assert!(tick >= reset + period);
        assert!(Instant::now() >= tick);
        assert!(reset.elapsed() < Duration::from_secs(5));
    }
}

in_both_modes! {
    fn an_interval_with_a_zero_period_panics<M>() {
        let runtime = Runtime::<M>::new().unwrap();

        let interval = catch_unwind(AssertUnwindSafe(|| runtime.interval(Duration::ZERO)));
        let interval_at = catch_unwind(AssertUnwindSafe(|| {
            runtime.interval_at(Instant::now(), Duration::ZERO)
        }));

        assert!(interval.is_err());
        assert!(interval_at.is_err());
    }
}

in_both_modes! {
    /// An interval whose ticks the clock cannot name never ticks: one asked for a period further
    /// ahead than the clock can name has no first tick, and one started now hands out its first
    /// tick and then no other.
    ///
    /// As a stream, neither ends, and neither promises any tick to come either: the size hint of
    /// an interval is no more than any stream's.
    fn an_interval_beyond_the_clock_never_ticks<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let mut never = runtime.interval(Duration::MAX);
        let start = Instant::now();
        let mut once = runtime.interval_at(start, Duration::MAX);

        assert!(runtime.block_on(poll_immediate(never.tick())).is_none());
        assert_eq!(Stream::size_hint(&never), (0, None));
        assert_eq!(runtime.block_on(poll_immediate(once.tick())), Some(start));
        // The tick after it is a whole period after `start`, beyond the clock.
        assert!(runtime.block_on(poll_immediate(once.tick())).is_none());
        assert_eq!(Stream::size_hint(&once), (0, None));
    }
}

in_both_modes! {
    fn a_registration_reports_readiness<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let (source, mut peer) = pair();
        let registration = M::register(&runtime, source.clone()).unwrap();
        // Written from another thread once the read below is waiting, so that the byte is one the
        // wait reports rather than one the first attempt to read finds for itself.
        let writer = thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            peer.write_all(&[7]).unwrap();
        });

        let read = runtime.block_on(read_one(&registration, &source));

        assert_eq!(read.unwrap(), 1);
        writer.join().unwrap();
    }
}

in_both_modes! {
    /// A runtime watches a descriptor through one registration at a time: a second one is turned
    /// away while the first lives, and made once it is gone.
    fn a_descriptor_is_registered_once_at_a_time<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let (source, _peer) = pair();
        let registration = M::register(&runtime, source.clone()).unwrap();

        let again = M::register(&runtime, source.clone()).unwrap_err();
        assert_eq!(again.kind(), io::ErrorKind::AlreadyExists);

        drop(registration);
        M::register(&runtime, source).unwrap();
    }
}

in_both_modes! {
    /// A wait for readiness completes once another thread has written to the peer, and the read
    /// that follows it finds the byte at once, with no retry of its own.
    fn a_registration_waits_for_readiness<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let (source, mut peer) = pair();
        let registration = M::register(&runtime, source.clone()).unwrap();
        // Written from another thread once the wait below is under way, so that the byte is one
        // the wait reports rather than one that was there before it.
        let writer = thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            peer.write_all(&[7]).unwrap();
        });

        runtime.block_on(registration.ready(Interest::Readable)).unwrap();

        assert_eq!(read_ready_byte(&source), 7);
        writer.join().unwrap();
    }
}

/// Four tasks of a local runtime wait for the readiness of one registration at once, and one
/// byte ends all four waits, leaving the byte there to be read.
#[test]
#[timeout(15000)]
fn four_local_tasks_wait_for_one_readiness_together() {
    let runtime = LocalRuntime::new().unwrap();
    let (source, mut peer) = pair();
    let registration = Rc::new(runtime.register(source.clone()).unwrap());
    let tasks: Vec<_> = (0..4)
        .map(|_| {
            let registration = registration.clone();
            runtime.spawn("a task waiting for readiness", async move {
                registration.ready(Interest::Readable).await.unwrap();
            })
        })
        .collect();
    // Written from another thread once the four tasks are waiting.
    let writer = thread::spawn(move || {
        thread::sleep(Duration::from_millis(50));
        peer.write_all(&[7]).unwrap();
    });

    runtime.block_on(async {
        for task in tasks {
            task.await.unwrap();
        }
    });

    // Waiting for readiness took nothing from the source.
    let read = runtime.block_on(read_one(&registration, &source));
    assert_eq!(read.unwrap(), 1);
    writer.join().unwrap();
}

/// Four tasks of a shared runtime wait for the readiness of one registration at once, and one
/// byte ends all four waits, leaving the byte there to be read.
#[test]
#[timeout(15000)]
fn four_shared_tasks_wait_for_one_readiness_together() {
    let runtime = SharedRuntime::new().unwrap();
    let (source, mut peer) = pair();
    let registration = Arc::new(runtime.register(source.clone()).unwrap());
    let tasks: Vec<_> = (0..4)
        .map(|_| {
            let registration = registration.clone();
            runtime.spawn("a task waiting for readiness", async move {
                registration.ready(Interest::Readable).await.unwrap();
            })
        })
        .collect();
    // Written from another thread once the four tasks are waiting.
    let writer = thread::spawn(move || {
        thread::sleep(Duration::from_millis(50));
        peer.write_all(&[7]).unwrap();
    });

    runtime.block_on(async {
        for task in tasks {
            task.await.unwrap();
        }
    });

    // Waiting for readiness took nothing from the source.
    let read = runtime.block_on(read_one(&registration, &source));
    assert_eq!(read.unwrap(), 1);
    writer.join().unwrap();
}

in_both_modes! {
    /// A wait for readiness is not completed by the wake of a timer it shares its task with.
    ///
    /// The timer wakes the task, which polls both futures again, and the wait for readiness has
    /// to take that for what it is: a poll the source did not cause. Nothing is written to the
    /// peer, which is kept so that the source does not become readable by its end.
    fn a_readiness_wait_is_not_completed_by_a_timer<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let (source, _peer) = pair();
        let registration = M::register(&runtime, source).unwrap();
        let started = Instant::now();

        let winner = runtime.block_on(or(
            async {
                registration.ready(Interest::Readable).await.unwrap();
                "ready"
            },
            async {
                runtime.sleep(Duration::from_millis(20)).await;
                "timer"
            },
        ));

        assert_eq!(winner, "timer");
        assert!(started.elapsed() >= Duration::from_millis(20));
    }
}

in_both_modes! {
    /// A wait for readiness dropped while it waits, whether by a poll that gave up on it or by
    /// the race it lost, leaves the runtime able to finish its `block_on` and the registration
    /// able to wait again.
    fn a_dropped_readiness_wait_leaves_the_registration_usable<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let (source, mut peer) = pair();
        let registration = M::register(&runtime, source.clone()).unwrap();

        // Dropped after a single poll, which left it waiting.
        let polled = runtime.block_on(poll_immediate(registration.ready(Interest::Readable)));
        assert!(polled.is_none());
        // Dropped by the race it lost, when the timer won.
        let winner = runtime.block_on(or(
            async {
                registration.ready(Interest::Readable).await.unwrap();
                "ready"
            },
            async {
                runtime.sleep(Duration::from_millis(20)).await;
                "timer"
            },
        ));
        assert_eq!(winner, "timer");
        // Written from another thread once the wait below is under way.
        let writer = thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            peer.write_all(&[7]).unwrap();
        });

        runtime.block_on(registration.ready(Interest::Readable)).unwrap();

        assert_eq!(read_ready_byte(&source), 7);
        writer.join().unwrap();
    }
}

in_both_modes! {
    /// A socket of one protocol family is watched beside a wake channel of another.
    ///
    /// Winsock takes every socket of one `select` call to come from a single service provider,
    /// and a runtime's wake channel is a loopback TCP connection while a caller may register an
    /// `AF_UNIX` socket directly. A wait therefore holds sockets of both families, and this is
    /// where that mixture is asked for: an `AF_UNIX` socket registered on a runtime is reported
    /// readable just as a socket of the wake channel's own family is.
    #[cfg(windows)]
    fn an_af_unix_socket_is_watched_beside_the_tcp_wake_socket<M>() {
        use std::{
            env, fs,
            os::windows::io::{AsSocket, OwnedSocket},
            process,
            time::SystemTime,
        };

        use uds_windows::{UnixListener, UnixStream};

        let runtime = Runtime::<M>::new().unwrap();
        // Named after this process, this moment and the flavour under test, so that two runs of
        // the suite, or the two flavours of this test running side by side, cannot meet over one
        // path.
        let since_epoch = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap();
        let path = env::temp_dir().join(format!(
            "zruntime-{}-{}-{}.sock",
            process::id(),
            since_epoch.as_nanos(),
            std::any::type_name::<M>().rsplit("::").next().unwrap_or_default(),
        ));
        let listener = UnixListener::bind(&path).unwrap();
        let client = UnixStream::connect(&path).unwrap();
        let (mut server, _) = listener.accept().unwrap();
        client.set_nonblocking(true).unwrap();
        server.set_nonblocking(true).unwrap();
        // The stream keeps the handle it was made with, so what the source is given is a
        // duplicate of it.
        let owned: OwnedSocket = client.as_socket().try_clone_to_owned().unwrap();
        let source: TestSource = Arc::new(owned);
        let registration = M::register(&runtime, source.clone()).unwrap();
        // Written from another thread once the read below is waiting, so that the byte is one the
        // wait reports rather than one the first attempt to read finds for itself.
        let writer = thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            server.write_all(&[7]).unwrap();
        });

        let read = runtime.block_on(read_one(&registration, &source));

        assert_eq!(read.unwrap(), 1);
        writer.join().unwrap();
        drop(listener);
        fs::remove_file(&path).unwrap();
    }
}

in_both_modes! {
    /// A runtime that goes with tasks still on it drops their futures and fails their handles,
    /// whether they were polled before it went or never were.
    fn dropping_the_runtime_fails_pending_handles<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let polled = M::spawn(&runtime, "a task that never finishes", pending::<()>());
        runtime.block_on(yield_now());
        let dropped = Arc::new(AtomicBool::new(false));
        let never_polled = {
            let marker = SetOnDrop(dropped.clone());
            M::spawn(&runtime, "a task that is never polled", async move {
                let _marker = marker;
            })
        };

        drop(runtime);

        assert!(dropped.load(Ordering::Acquire));
        for task in [polled, never_polled] {
            let error = futures::executor::block_on(task).unwrap_err();
            assert_eq!(error.to_string(), "the task's runtime is gone");
        }
    }
}

in_both_modes! {
    /// A `block_on` from a thread that is driving a runtime already panics rather than hangs:
    /// from inside the future of a `block_on` on the same runtime, or on another runtime of
    /// either flavour. The call that panics leaves the one it was made from driving as before.
    fn a_nested_block_on_panics<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let local = LocalRuntime::new().unwrap();
        let shared = SharedRuntime::new().unwrap();

        let nested = runtime.block_on(async {
            let panicked = [
                catch_unwind(AssertUnwindSafe(|| runtime.block_on(async {}))).is_err(),
                catch_unwind(AssertUnwindSafe(|| local.block_on(async {}))).is_err(),
                catch_unwind(AssertUnwindSafe(|| shared.block_on(async {}))).is_err(),
            ];
            // Still driving: a task and a timer spawned after the panics run as ever.
            let task = M::spawn(&runtime, "a task after the panics", async { 7 });
            runtime.sleep(Duration::from_millis(1)).await;

            (panicked, task.await.unwrap())
        });

        assert_eq!(nested, ([true; 3], 7));
        // And the thread drives each of them in turn once it has left the first.
        assert_eq!(local.block_on(async { 7 }), 7);
        assert_eq!(shared.block_on(async { 7 }), 7);
    }
}

in_both_modes! {
    /// The runtime a thread drives is on record for as long as the thread is inside `block_on` on
    /// it, and not a moment longer, whether the call returns or unwinds.
    fn the_runtime_a_thread_drives_is_on_record_while_it_is_inside_block_on<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        assert!(M::driven().is_none());

        runtime.block_on(async {
            let driven = M::driven().expect("the runtime this thread drives");
            assert!(std::ptr::eq(&*driven, &*runtime.core));
        });
        assert!(M::driven().is_none());

        let panicked = catch_unwind(AssertUnwindSafe(|| {
            runtime.block_on(async { panic!("a future that panics") });
        }));
        assert!(panicked.is_err());
        assert!(M::driven().is_none());
    }
}

in_both_modes! {
    /// A `block_on` whose future panics leaves the runtime to be driven again, by this thread or
    /// another.
    fn a_panic_in_the_future_leaves_the_runtime_drivable<M>() {
        let runtime = Runtime::<M>::new().unwrap();
        let panicked = catch_unwind(AssertUnwindSafe(|| {
            runtime.block_on(async { panic!("a future that panics") });
        }));
        assert!(panicked.is_err());

        let task = M::spawn(&runtime, "the task after it", async { 7 });
        assert_eq!(runtime.block_on(task).unwrap(), 7);
    }
}

/// A local runtime runs futures that are not `Send`: a task can hold an `Rc` and borrow a
/// `RefCell` across its awaits.
#[test]
#[timeout(15000)]
fn a_local_task_can_hold_an_rc() {
    let runtime = LocalRuntime::new().unwrap();
    let log = Rc::new(RefCell::new(Vec::new()));
    let task = {
        let log = log.clone();
        let sleeper = runtime.clone();
        runtime.spawn("a task holding an `Rc`", async move {
            log.borrow_mut().push("before");
            sleeper.sleep(Duration::from_millis(1)).await;
            log.borrow_mut().push("after");

            Rc::strong_count(&log)
        })
    };

    let count = runtime.block_on(task).unwrap();

    assert_eq!(count, 2);
    assert_eq!(*log.borrow(), ["before", "after"]);
}

/// A thread of its own wakes a local task through the task's waker, and breaks the wait the
/// thread inside `block_on` is in to do so.
///
/// A waker is `Send` whatever the task it wakes is, so a local task waiting on a thread's work is
/// woken by that thread as any other task is.
#[cfg(feature = "event")]
#[test]
#[timeout(15000)]
fn a_thread_wakes_a_local_task() {
    let runtime = LocalRuntime::new().unwrap();
    let event = Arc::new(Event::new());
    let listener = event.listen();
    let local = Rc::new(7);
    let task = runtime.spawn("a local task waiting on a thread", async move {
        listener.await;
        *local
    });
    let notifier = thread::spawn(move || {
        thread::sleep(Duration::from_millis(50));
        event.notify(1);
    });
    let started = Instant::now();

    assert_eq!(runtime.block_on(task).unwrap(), 7);

    assert!(started.elapsed() >= Duration::from_millis(50));
    assert!(started.elapsed() < Duration::from_secs(5));
    notifier.join().unwrap();
}

/// A task spawned from another thread onto a shared runtime breaks the wait of the thread inside
/// `block_on` on it, which then runs the task.
#[cfg(feature = "event")]
#[test]
#[timeout(15000)]
fn a_spawn_from_another_thread_reaches_the_driving_thread() {
    let runtime = SharedRuntime::new().unwrap();
    let ran = Arc::new(Event::new());
    // Taken before the thread starts, so that the announcement cannot be missed.
    let announced = ran.listen();
    let spawner = {
        let runtime = runtime.clone();
        thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            runtime
                .spawn("a task spawned from another thread", async move {
                    ran.notify(1);
                })
                .detach();
        })
    };

    runtime.block_on(announced);

    spawner.join().unwrap();
}

/// A shared runtime is driven by one thread at a time, so a second thread asking to drive it
/// while the first does is told so rather than left to race it.
#[cfg(feature = "event")]
#[test]
#[timeout(15000)]
fn a_second_concurrent_block_on_panics() {
    let runtime = SharedRuntime::new().unwrap();
    let inside = Arc::new(Event::new());
    let release = Arc::new(Event::new());
    // Both taken before the thread starts, so that neither notification can be missed.
    let announced = inside.listen();
    let released = release.listen();
    let driver = {
        let runtime = runtime.clone();
        thread::spawn(move || {
            runtime.block_on(async move {
                inside.notify(1);
                released.await;
            })
        })
    };
    futures::executor::block_on(announced);

    let panicked = catch_unwind(AssertUnwindSafe(|| runtime.block_on(async {}))).unwrap_err();

    let message = panicked
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| panicked.downcast_ref::<String>().map(String::as_str))
        .unwrap_or_default();
    assert!(message.contains("another thread is driving"), "{message}");
    release.notify(1);
    driver.join().unwrap();
    // And once the first thread has left, this one may drive it.
    assert_eq!(runtime.block_on(async { 7 }), 7);
}

/// A task spawned with no runtime to name goes on the local runtime the thread drives, whether it
/// is spawned from the future of `block_on` or from inside a task of that runtime, and not on any
/// other runtime there is. The futures hold an `Rc`, which only a local runtime takes.
#[test]
#[timeout(15000)]
fn spawn_local_puts_the_task_on_the_runtime_the_thread_drives() {
    let runtime = LocalRuntime::new().unwrap();
    let other = LocalRuntime::new().unwrap();

    let (from_block_on, from_a_task) = runtime.block_on(async {
        let local = Rc::new(7);
        let task = crate::spawn_local({
            let local = local.clone();
            async move { *local }
        });
        // Queued here, on this runtime and on no other, before it has had a turn.
        assert_eq!(runtime.core.scheduler.live_tasks(), 1);
        assert_eq!(other.core.scheduler.live_tasks(), 0);
        let from_block_on = task.await.unwrap();

        let spawning = crate::spawn_local({
            let runtime = runtime.clone();
            async move {
                let spawned = crate::spawn_local(async move { *local });
                // This task and the one it spawned, both on the runtime that is running them.
                assert_eq!(runtime.core.scheduler.live_tasks(), 2);

                spawned.await.unwrap()
            }
        });

        (from_block_on, spawning.await.unwrap())
    });

    assert_eq!((from_block_on, from_a_task), (7, 7));
    assert_eq!(other.core.scheduler.live_tasks(), 0);
}

/// A task spawned with no runtime to name goes on the shared runtime the thread drives, whether
/// the runtime was made by `Runtime::new` or not, from the future of `block_on` and from inside a
/// task of that runtime alike, and runs on the thread inside `block_on` as every task of it does.
#[test]
#[timeout(15000)]
fn spawn_puts_the_task_on_the_runtime_the_thread_drives() {
    let runtime = SharedRuntime::new().unwrap();
    let other = SharedRuntime::new().unwrap();

    let (from_block_on, from_a_task) = runtime.block_on(async {
        let task = crate::spawn(async { thread::current().id() });
        // Queued here, on this runtime and on no other, before it has had a turn.
        assert_eq!(runtime.core.scheduler.live_tasks(), 1);
        assert_eq!(other.core.scheduler.live_tasks(), 0);
        let from_block_on = task.await.unwrap();

        let spawning = crate::spawn({
            let runtime = runtime.clone();
            async move {
                let spawned = crate::spawn(async { thread::current().id() });
                // This task and the one it spawned, both on the runtime that is running them.
                assert_eq!(runtime.core.scheduler.live_tasks(), 2);

                spawned.await.unwrap()
            }
        });

        (from_block_on, spawning.await.unwrap())
    });

    assert_eq!(from_block_on, thread::current().id());
    assert_eq!(from_a_task, thread::current().id());
    assert_eq!(other.core.scheduler.live_tasks(), 0);
}

/// A local task has no runtime to go on where the thread drives no local runtime: outside any
/// `block_on`, and inside the `block_on` of a shared runtime, whose tasks may run on any thread. A
/// call that panics leaves the runtime it was made inside driving as before.
#[test]
#[timeout(15000)]
fn spawn_local_panics_where_the_thread_drives_no_local_runtime() {
    let outside = catch_unwind(AssertUnwindSafe(|| crate::spawn_local(async {}))).unwrap_err();
    assert!(panic_message(&*outside).contains("no local runtime"));

    let shared = SharedRuntime::new().unwrap();
    let (message, next) = shared.block_on(async {
        let inside = catch_unwind(AssertUnwindSafe(|| crate::spawn_local(async {}))).unwrap_err();
        // Still driving: a task spawned after the panic runs as ever.
        let next = crate::spawn(async { 7 });

        (panic_message(&*inside).to_owned(), next.await.unwrap())
    });

    assert!(message.contains("no local runtime"), "{message}");
    assert_eq!(next, 7);
}

/// Without the `helper` feature, which would give a thread that drives no shared runtime the
/// process's to spawn on, there is none for a shared task to go on there: outside any `block_on`,
/// and inside the `block_on` of a local runtime.
#[cfg(not(feature = "helper"))]
#[test]
#[timeout(15000)]
fn spawn_panics_where_the_thread_drives_no_shared_runtime() {
    let outside = catch_unwind(AssertUnwindSafe(|| crate::spawn(async {}))).unwrap_err();
    assert!(panic_message(&*outside).contains("no shared runtime"));

    let local = LocalRuntime::new().unwrap();
    let (message, next) = local.block_on(async {
        let inside = catch_unwind(AssertUnwindSafe(|| crate::spawn(async {}))).unwrap_err();
        // Still driving: a task spawned after the panic runs as ever.
        let next = crate::spawn_local(async { 7 });

        (panic_message(&*inside).to_owned(), next.await.unwrap())
    });

    assert!(message.contains("no shared runtime"), "{message}");
    assert_eq!(next, 7);
}

/// A task spawned with no runtime to name, inside a `block_on` run from the destructor of a
/// thread-local as its thread ends, goes on the runtime that call drives, as it does anywhere
/// else: a local runtime, and a shared one made by `Runtime::new`, whose task the thread inside
/// the call runs.
///
/// The thread drives a runtime of each flavour once the value is in place, so that the record of
/// the runtime it drives is first reached after that value: thread-local destructors run in
/// reverse order of registration, and a record with a destructor of its own would be gone by the
/// time the value's runs. A panic out of a destructor there is an abort, so the destructor catches
/// what panics and hands it on.
#[test]
#[timeout(15000)]
fn a_spawn_inside_a_block_on_as_a_thread_ends_goes_on_the_runtime_it_drives() {
    /// What a spawn inside a `block_on` of each flavour came to: the local task's output, and
    /// whether the shared task ran on the thread inside the call, or what panicked instead.
    type Spawned = (Result<u32, String>, Result<bool, String>);

    /// Spawns inside a `block_on` of each flavour as it is dropped, and sends what came of it.
    struct SpawnsAsItGoes(Option<mpsc::Sender<Spawned>>);

    impl Drop for SpawnsAsItGoes {
        fn drop(&mut self) {
            let Some(spawned) = self.0.take() else {
                return;
            };
            DROPPING.with(|dropping| dropping.set(true));
            let local = catch_unwind(|| {
                LocalRuntime::new()
                    .unwrap()
                    .block_on(async { crate::spawn_local(async { 7 }).await.unwrap() })
            });
            let shared = catch_unwind(|| {
                SharedRuntime::new().unwrap().block_on(async {
                    crate::spawn(async { DROPPING.with(Cell::get) })
                        .await
                        .unwrap()
                })
            });
            let message = |panic: Box<dyn Any + Send>| panic_message(&*panic).to_owned();
            let _ = spawned.send((local.map_err(message), shared.map_err(message)));
        }
    }

    thread_local! {
        static SPAWNS_AS_IT_GOES: RefCell<SpawnsAsItGoes> =
            const { RefCell::new(SpawnsAsItGoes(None)) };

        /// Whether this thread is the one dropping the value above, for a task to tell which
        /// thread runs it. With no destructor, so that it is there to read as the thread ends.
        static DROPPING: Cell<bool> = const { Cell::new(false) };
    }

    let (sender, spawned) = mpsc::channel();
    let thread = thread::spawn(move || {
        SPAWNS_AS_IT_GOES.with(|spawns| spawns.borrow_mut().0 = Some(sender));
        LocalRuntime::new().unwrap().block_on(async {});
        SharedRuntime::new().unwrap().block_on(async {});
    });

    assert!(thread.join().is_ok());
    // The shared task run by the thread dropping the value, inside the call, rather than by a
    // helper thread of some other runtime. Both compared at once, so that a failure shows both.
    assert_eq!(spawned.recv().unwrap(), (Ok(7), Ok(true)));
}

/// A task spawned with no name is named by the call that spawned it, as its `Debug` shows, while a
/// name given prints as the string it is.
#[test]
#[timeout(15000)]
fn a_task_spawned_with_no_name_is_named_by_where_it_was_spawned() {
    let local = LocalRuntime::new().unwrap();
    let shared = SharedRuntime::new().unwrap();

    let (local_task, local_line) =
        local.block_on(async { (crate::spawn_local(async {}), line!()) });
    let (shared_task, shared_line) = shared.block_on(async { (crate::spawn(async {}), line!()) });
    let named = local.spawn("socket reader", async {});

    // The line is the one of the call, and the column is left to the formatter.
    assert!(
        format!("{local_task:?}").contains(&format!("task: spawned at {}:{local_line}:", file!())),
        "{local_task:?}"
    );
    assert!(
        format!("{shared_task:?}")
            .contains(&format!("task: spawned at {}:{shared_line}:", file!())),
        "{shared_task:?}"
    );
    assert_eq!(
        format!("{named:?}"),
        r#"Task(JoinHandle { task: "socket reader", detached: false, .. })"#
    );
}

/// What a shared runtime's handles are shared as: a task handle and a registration live in state
/// that several threads reach, a runtime handle is cloned into the tasks it spawns, and a timer,
/// a timeout or an interval goes wherever the task awaiting it is polled.
#[test]
fn shared_handles_cross_threads() {
    fn sent_and_shared<T>()
    where
        T: Send + Sync + Unpin,
    {
    }

    sent_and_shared::<SharedRuntime>();
    sent_and_shared::<Task<(), Shared>>();
    sent_and_shared::<Registration<Shared>>();
    sent_and_shared::<Sleep<Shared>>();
    sent_and_shared::<Timeout<Pending<()>, Shared>>();
    sent_and_shared::<Interval<Shared>>();
}

/// Spawning and registering, through whichever of the two flavours' own methods `Self` names.
///
/// The two take different bounds — a shared runtime's tasks and sources have to be `Send` — so
/// this takes the stricter of each, which the tests written for both flavours meet.
trait TestMode: Mode {
    /// Spawns `future` on `runtime`.
    fn spawn<T>(
        runtime: &Runtime<Self>,
        name: &'static str,
        future: impl Future<Output = T> + Send + 'static,
    ) -> Task<T, Self>
    where
        T: Send + 'static;

    /// Registers `source` on `runtime`.
    fn register(runtime: &Runtime<Self>, source: TestSource) -> io::Result<Registration<Self>>;
}

impl TestMode for Local {
    fn spawn<T>(
        runtime: &LocalRuntime,
        name: &'static str,
        future: impl Future<Output = T> + Send + 'static,
    ) -> Task<T, Local>
    where
        T: Send + 'static,
    {
        runtime.spawn(name, future)
    }

    fn register(runtime: &LocalRuntime, source: TestSource) -> io::Result<Registration<Local>> {
        runtime.register(source)
    }
}

impl TestMode for Shared {
    fn spawn<T>(
        runtime: &SharedRuntime,
        name: &'static str,
        future: impl Future<Output = T> + Send + 'static,
    ) -> Task<T, Shared>
    where
        T: Send + 'static,
    {
        runtime.spawn(name, future)
    }

    fn register(runtime: &SharedRuntime, source: TestSource) -> io::Result<Registration<Shared>> {
        runtime.register(source)
    }
}

/// Reads one byte from `source` through `registration`, the way a caller reads its socket.
async fn read_one<M>(registration: &Registration<M>, source: &TestSource) -> io::Result<usize>
where
    M: Mode,
{
    poll_fn(|cx| {
        let mut byte = [MaybeUninit::<u8>::uninit(); 1];

        registration.poll_io(cx, Interest::Readable, || {
            SockRef::from(source).recv(&mut byte)
        })
    })
    .await
}

/// Reads the one byte `source` has right now, with no waiting and no retry, the way a caller does
/// that has just been told the source is ready: a `WouldBlock` here fails the test.
fn read_ready_byte(source: &TestSource) -> u8 {
    let mut byte = [0];
    let socket = SockRef::from(source);
    assert_eq!((&*socket).read(&mut byte).unwrap(), 1);

    byte[0]
}

/// The message a panic carried, which is a string for a panic made from a string literal or a
/// format string, as every one this crate makes is.
fn panic_message(payload: &(dyn Any + Send)) -> &str {
    payload
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| payload.downcast_ref::<String>().map(String::as_str))
        .unwrap_or_default()
}

/// Sets the flag it holds when it is dropped.
struct SetOnDrop(Arc<AtomicBool>);

impl Drop for SetOnDrop {
    fn drop(&mut self) {
        self.0.store(true, Ordering::Release);
    }
}

/// Panics when it is dropped.
struct PanicOnDrop;

impl Drop for PanicOnDrop {
    fn drop(&mut self) {
        panic!("dropped with a panic on purpose");
    }
}

/// A future that is ready at once with the value it holds, and panics when it is dropped.
struct ReadyThenPanicOnDrop(u32);

impl Future for ReadyThenPanicOnDrop {
    type Output = u32;

    fn poll(self: std::pin::Pin<&mut Self>, _cx: &mut std::task::Context<'_>) -> Poll<u32> {
        Poll::Ready(self.0)
    }
}

impl Drop for ReadyThenPanicOnDrop {
    fn drop(&mut self) {
        panic!("dropped with a panic on purpose");
    }
}

/// Spawns on `runtime`, through `spawn`, a task whose future notes whether its own task is
/// finished as the future is dropped, and has the task end as `end` says.
///
/// Gives what the future noted, `None` where it noted nothing, after checking that the task is
/// finished once it has ended.
fn finished_as_future_drops<M>(
    runtime: Runtime<M>,
    end: End,
    spawn: impl FnOnce(&Runtime<M>, NoteFinishedOnDrop<M>) -> Task<(), M>,
) -> Option<bool>
where
    M: Mode,
{
    let slot = Arc::new(Mutex::new(None));
    let noted = Arc::new(Mutex::new(None));
    let task = spawn(
        &runtime,
        NoteFinishedOnDrop {
            ready: matches!(end, End::Completes),
            task: slot.clone(),
            noted: noted.clone(),
        },
    );
    *slot.lock().unwrap() = Some(task);

    match end {
        End::Completes => runtime.block_on(yield_now()),
        End::GoesWithItsRuntimePolled => {
            runtime.block_on(yield_now());
            drop(runtime);
        }
        End::GoesWithItsRuntimeUnpolled => drop(runtime),
    }

    assert!(slot.lock().unwrap().as_ref().unwrap().is_finished());

    *noted.lock().unwrap()
}

/// How the task [`finished_as_future_drops`] spawns comes to its end.
#[derive(Clone, Copy, Debug)]
enum End {
    /// Its future is ready on its first poll.
    Completes,
    /// Its future is left waiting by its first poll, and then goes with the runtime.
    GoesWithItsRuntimePolled,
    /// Its future goes with the runtime before it is ever polled.
    GoesWithItsRuntimeUnpolled,
}

impl End {
    const ALL: [Self; 3] = [
        Self::Completes,
        Self::GoesWithItsRuntimePolled,
        Self::GoesWithItsRuntimeUnpolled,
    ];
}

/// A future that is ready at once where `ready`, and waits for ever otherwise, and that notes
/// whether the task it belongs to is finished when it is dropped.
///
/// The task is kept in a slot of the test's own, which the future reaches it through.
struct NoteFinishedOnDrop<M>
where
    M: Mode,
{
    ready: bool,
    task: Arc<Mutex<Option<Task<(), M>>>>,
    noted: Arc<Mutex<Option<bool>>>,
}

impl<M> Future for NoteFinishedOnDrop<M>
where
    M: Mode,
{
    type Output = ();

    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
        if self.ready {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

impl<M> Drop for NoteFinishedOnDrop<M>
where
    M: Mode,
{
    fn drop(&mut self) {
        let finished = self.task.lock().unwrap().as_ref().map(Task::is_finished);
        *self.noted.lock().unwrap() = finished;
    }
}

/// Cancels the task in `task` as it is dropped, polls the wait for it once, and leaves that wait
/// in `cancelling`, noting in `pending_at_once` whether that poll left it pending.
///
/// What it finds goes into the test's own cells rather than into assertions of its own: a
/// destructor the runtime runs has its panics contained, and an assertion failing there would
/// fail nothing.
struct CancelOnDrop {
    task: Rc<RefCell<Option<Task<()>>>>,
    cancelling: Rc<RefCell<Option<Cancelling>>>,
    pending_at_once: Rc<Cell<Option<bool>>>,
}

impl Drop for CancelOnDrop {
    fn drop(&mut self) {
        let Some(task) = self.task.borrow_mut().take() else {
            return;
        };
        let mut cancelling = Box::pin(task.cancel());
        let polled = cancelling
            .as_mut()
            .poll(&mut Context::from_waker(Waker::noop()));
        self.pending_at_once.set(Some(polled.is_pending()));
        *self.cancelling.borrow_mut() = Some(cancelling);
    }
}

/// The wait for a cancelled local task, boxed so that a test can keep it.
type Cancelling = Pin<Box<dyn Future<Output = Option<()>>>>;

/// Spawns `future` on `runtime` behind a first poll that holds the thread making it until the
/// sender handed back is sent to or dropped, and has a thread of its own make that poll, inside a
/// `block_on` on `runtime` that lasts one round of its loop.
///
/// Returns once that poll has begun, with the task, that sender, and that thread, which leaves
/// `block_on` once the poll has returned and the scheduler has done with the task what the poll
/// left it to do.
fn held_in_its_poll<F>(
    runtime: &SharedRuntime,
    future: F,
) -> (
    Task<F::Output, Shared>,
    mpsc::Sender<()>,
    thread::JoinHandle<()>,
)
where
    F: Future + Send + 'static,
    F::Output: Send + 'static,
{
    let (announce, announced) = mpsc::channel();
    let (release, released) = mpsc::channel();
    let task = runtime.spawn("a task held in its poll", async move {
        announce.send(()).unwrap();
        // A sender dropped by a test that failed first lets the poll go on as well.
        let _ = released.recv();
        future.await
    });
    let driver = {
        let runtime = runtime.clone();
        // A yield is one round of the loop, in which the task is polled once.
        thread::spawn(move || runtime.block_on(yield_now()))
    };
    announced.recv().unwrap();

    (task, release, driver)
}

/// A counter and the waker that counts into it.
fn counting_waker() -> (Arc<Counter>, Waker) {
    let counter = Arc::new(Counter::default());
    let waker = Waker::from(counter.clone());

    (counter, waker)
}

/// A waker that counts how often it has been woken.
#[derive(Default)]
struct Counter(AtomicUsize);

impl Counter {
    /// How often this waker has been woken.
    fn count(&self) -> usize {
        self.0.load(Ordering::Acquire)
    }
}

impl Wake for Counter {
    fn wake(self: Arc<Self>) {
        self.wake_by_ref();
    }

    fn wake_by_ref(self: &Arc<Self>) {
        self.0.fetch_add(1, Ordering::AcqRel);
    }
}

/// A source shareable between the registration a test makes and the reads or writes it makes
/// directly: an owned descriptor of its own, behind an `Arc` of the test's own.
#[cfg(unix)]
pub(super) type TestSource = Arc<std::os::fd::OwnedFd>;
#[cfg(windows)]
pub(super) type TestSource = Arc<std::os::windows::io::OwnedSocket>;

/// A connected pair: the source to register, and the far end to drive it from.
pub(super) fn pair() -> (TestSource, Socket) {
    let (near, far) = connected();
    near.set_nonblocking(true).unwrap();
    far.set_nonblocking(true).unwrap();

    (owned_source(near), far)
}

/// Wraps `socket` as a [`TestSource`]: one clone goes to the registration, the other is kept
/// for the reads and writes the tests make directly.
#[cfg(unix)]
fn owned_source(socket: Socket) -> TestSource {
    let owned: std::os::fd::OwnedFd = socket.into();

    Arc::new(owned)
}

/// Wraps `socket` as a [`TestSource`]: one clone goes to the registration, the other is kept
/// for the reads and writes the tests make directly.
#[cfg(windows)]
fn owned_source(socket: Socket) -> TestSource {
    let owned: std::os::windows::io::OwnedSocket = socket.into();

    Arc::new(owned)
}

/// Two sockets connected to one another.
#[cfg(unix)]
fn connected() -> (Socket, Socket) {
    Socket::pair(socket2::Domain::UNIX, socket2::Type::STREAM, None).unwrap()
}

/// Two sockets connected to one another.
///
/// Winsock has no socket pair, so this is a loopback connection which a listener of its own
/// accepts and then has no further use for.
#[cfg(windows)]
fn connected() -> (Socket, Socket) {
    use std::net::{TcpListener, TcpStream};

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let far = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
    let near = loop {
        let (accepted, _) = listener.accept().unwrap();
        // A loopback listener is reachable by anything else on the machine, so a connection that
        // is not the one made just above is turned away rather than taken for it.
        if accepted.peer_addr().unwrap() == far.local_addr().unwrap() {
            break accepted;
        }
    };

    // One-byte messages travel this pair, as they do the poller's wake pair, so neither end
    // holds a send back for the peer's acknowledgement of the one before it.
    near.set_nodelay(true).unwrap();
    far.set_nodelay(true).unwrap();

    (near.into(), far.into())
}