pipedream-rs 0.2.0

A typed, heterogeneous event relay with observable delivery, completion tracking, and lossless message routing
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
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
#![allow(clippy::while_let_loop, clippy::redundant_pattern_matching)]

use super::*;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

// Minimal test: verify drop() closes subscriber channels
#[tokio::test]
async fn test_drop_closes_channels() {
    let stream = Relay::new();
    let mut sub = stream.subscribe::<String>();

    // Drop the stream - this should close the channel
    drop(stream);

    // Receiver should get None
    let result = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .expect("should not timeout");
    assert!(
        result.is_none(),
        "recv should return None when stream dropped"
    );
}

// Test: filter forwarder should close child when parent dropped

#[tokio::test]
async fn test_basic_send_subscribe() {
    let stream = Relay::new();
    let mut sub = stream.subscribe::<String>();

    stream.send("hello".to_string()).await.unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&"hello".to_string()));
}

#[tokio::test]
async fn test_type_filtering() {
    let stream = Relay::new();
    let mut string_sub = stream.subscribe::<String>();
    let mut int_sub = stream.subscribe::<i32>();

    stream.send("hello".to_string()).await.unwrap();
    stream.send(42i32).await.unwrap();

    let string_msg = tokio::time::timeout(Duration::from_millis(100), string_sub.recv())
        .await
        .unwrap();
    let int_msg = tokio::time::timeout(Duration::from_millis(100), int_sub.recv())
        .await
        .unwrap();

    assert_eq!(string_msg.as_deref(), Some(&"hello".to_string()));
    assert_eq!(int_msg.as_deref(), Some(&42i32));
}





#[tokio::test]
async fn test_broadcast_multiple_subscribers() {
    let stream = Relay::new();
    let mut sub1 = stream.subscribe::<String>();
    let mut sub2 = stream.subscribe::<String>();

    stream.send("hello".to_string()).await.unwrap();

    let msg1 = tokio::time::timeout(Duration::from_millis(100), sub1.recv())
        .await
        .unwrap();
    let msg2 = tokio::time::timeout(Duration::from_millis(100), sub2.recv())
        .await
        .unwrap();

    assert_eq!(msg1.as_deref(), Some(&"hello".to_string()));
    assert_eq!(msg2.as_deref(), Some(&"hello".to_string()));
}

#[tokio::test]
async fn test_cascade_close() {
    let stream = Relay::new();
    let filtered = stream.filter::<String, _>(|_| true);
    let mut sub = filtered.subscribe::<String>();

    stream.send("hello".to_string()).await.unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&"hello".to_string()));

    // Drop root stream
    drop(stream);

    // Subscription should close
    let closed = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(closed, None);
}





#[tokio::test]
async fn test_send_to_closed_stream() {
    let stream = Relay::new();
    let derived = stream.filter::<String, _>(|_| true);

    drop(stream);

    // Small delay to let forwarder detect closure
    tokio::task::yield_now().await;

    // Send to derived stream after parent closed
    let result = derived.send("should fail".to_string()).await;
    assert_eq!(result, Err(SendError::Closed));
}



#[tokio::test]
async fn test_tap() {
    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = counter.clone();

    let stream = Relay::new();
    let _tapped = stream.clone().tap::<i32, _, _>(move |n| {
        counter_clone.fetch_add(*n as usize, Ordering::SeqCst);
    });

    // tap() subscribes before spawning, so no pre-send sleep needed
    stream.send(10i32).await.unwrap();
    stream.send(20i32).await.unwrap();

    // Wait for tap task to process messages
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(counter.load(Ordering::SeqCst), 30);
}

#[tokio::test]
async fn test_sink() {
    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = counter.clone();

    let stream = Relay::new();
    stream.sink::<i32, _, _>(move |n| {
        counter_clone.fetch_add(*n as usize, Ordering::SeqCst);
    });

    // sink() subscribes before spawning, so no pre-send sleep needed
    stream.send(5i32).await.unwrap();
    stream.send(15i32).await.unwrap();

    // Wait for sink task to process messages
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(counter.load(Ordering::SeqCst), 20);
}


#[ignore]

#[ignore]


// ==================== STRESS TESTS ====================

#[tokio::test]
async fn stress_test_rapid_send_no_race() {
    // Verify no messages are dropped when sending rapidly after filter setup
    // Use larger buffer to hold all messages (broadcast channels are lossy for slow receivers)
    let stream = Relay::with_channel_size(1024);
    let filtered = stream.filter::<i32, _>(|x| *x % 2 == 0);
    let mut sub = filtered.subscribe::<i32>();

    const COUNT: i32 = 1000;

    // Send rapidly without any sleeps
    for i in 0..COUNT {
        stream.send(i).await.unwrap();
    }

    // Should receive exactly half (all even numbers)
    let mut received = Vec::new();
    for _ in 0..(COUNT / 2) {
        let msg = tokio::time::timeout(Duration::from_secs(1), sub.recv())
            .await
            .expect("timeout waiting for message")
            .expect("stream closed unexpectedly");
        received.push(*msg);
    }

    assert_eq!(received.len(), (COUNT / 2) as usize);
    for (i, val) in received.iter().enumerate() {
        assert_eq!(*val, (i * 2) as i32, "expected even number at index {}", i);
    }
}

#[tokio::test]
async fn stress_test_deep_filter_chain() {
    // Test deeply nested filter chain
    // Use larger buffer because each filter stage has its own channel
    let stream = Relay::with_channel_size(256);

    let filtered = stream
        .filter::<i32, _>(|x| *x > 0)
        .filter::<i32, _>(|x| *x > 10)
        .filter::<i32, _>(|x| *x > 20)
        .filter::<i32, _>(|x| *x > 30)
        .filter::<i32, _>(|x| *x < 100);

    let mut sub = filtered.subscribe::<i32>();

    // Send values, only 31-99 should pass all filters
    for i in 0..150 {
        stream.send(i).await.unwrap();
    }

    let mut received = Vec::new();
    loop {
        match tokio::time::timeout(Duration::from_millis(100), sub.recv()).await {
            Ok(Some(msg)) => received.push(*msg),
            _ => break,
        }
    }

    let expected: Vec<i32> = (31..100).collect();
    assert_eq!(received, expected);
}

#[tokio::test]
async fn stress_test_many_subscribers() {
    // Test many concurrent subscribers
    // Use larger buffer to handle all messages
    let stream = Relay::with_channel_size(128);
    let filtered = stream.filter::<i32, _>(|_| true);

    const NUM_SUBSCRIBERS: usize = 50;
    const NUM_MESSAGES: i32 = 100;

    let mut subs: Vec<_> = (0..NUM_SUBSCRIBERS)
        .map(|_| filtered.subscribe::<i32>())
        .collect();

    for i in 0..NUM_MESSAGES {
        stream.send(i).await.unwrap();
    }

    // Each subscriber should receive all messages
    for (idx, sub) in subs.iter_mut().enumerate() {
        let mut count = 0;
        loop {
            match tokio::time::timeout(Duration::from_millis(100), sub.recv()).await {
                Ok(Some(_)) => count += 1,
                _ => break,
            }
        }
        assert_eq!(
            count, NUM_MESSAGES as usize,
            "subscriber {} received {} messages, expected {}",
            idx, count, NUM_MESSAGES
        );
    }
}

#[tokio::test]
async fn stress_test_dropped_subscriber_mid_stream() {
    // Test that dropping a subscriber doesn't affect others
    let stream = Relay::new();
    let filtered = stream.filter::<i32, _>(|_| true);

    let mut sub1 = filtered.subscribe::<i32>();
    let mut sub2 = filtered.subscribe::<i32>();
    let sub3_to_drop = filtered.subscribe::<i32>();

    // Send some messages
    for i in 0..10 {
        stream.send(i).await.unwrap();
    }

    // Drop one subscriber mid-stream
    drop(sub3_to_drop);

    // Send more messages
    for i in 10..20 {
        stream.send(i).await.unwrap();
    }

    // Remaining subscribers should get all 20 messages
    let mut count1 = 0;
    let mut count2 = 0;

    loop {
        match tokio::time::timeout(Duration::from_millis(100), sub1.recv()).await {
            Ok(Some(_)) => count1 += 1,
            _ => break,
        }
    }

    loop {
        match tokio::time::timeout(Duration::from_millis(100), sub2.recv()).await {
            Ok(Some(_)) => count2 += 1,
            _ => break,
        }
    }

    assert_eq!(count1, 20);
    assert_eq!(count2, 20);
}

#[tokio::test]
async fn stress_test_concurrent_senders() {
    // Multiple tasks sending concurrently
    // Use larger buffer to handle all messages from all senders
    let stream = Relay::with_channel_size(1024);
    let mut sub = stream.subscribe::<i32>();

    const SENDERS: usize = 10;
    const MESSAGES_PER_SENDER: usize = 100;

    let handles: Vec<_> = (0..SENDERS)
        .map(|sender_id| {
            let stream = stream.clone();
            tokio::spawn(async move {
                for i in 0..MESSAGES_PER_SENDER {
                    stream
                        .send((sender_id * MESSAGES_PER_SENDER + i) as i32)
                        .await
                        .unwrap();
                }
            })
        })
        .collect();

    // Wait for all senders
    for handle in handles {
        handle.await.unwrap();
    }

    // Count received messages
    let mut count = 0;
    loop {
        match tokio::time::timeout(Duration::from_millis(100), sub.recv()).await {
            Ok(Some(_)) => count += 1,
            _ => break,
        }
    }

    assert_eq!(count, SENDERS * MESSAGES_PER_SENDER);
}

#[tokio::test]
async fn stress_test_fan_in_fan_out() {
    // Multiple sources piping to one target, then fanning out to multiple subscribers
    // Use larger buffer to handle all messages
    let sources: Vec<_> = (0..5).map(|_| Relay::with_channel_size(128)).collect();
    let target = Relay::with_channel_size(128);

    // Spawn forward tasks (forward blocks until source closes)
    for source in &sources {
        let source = source.clone();
        let target = target.clone();
        tokio::spawn(async move { source.forward(&target).await });
    }

    // Give forward tasks time to set up
    tokio::time::sleep(Duration::from_millis(10)).await;

    let mut subs: Vec<_> = (0..5).map(|_| target.subscribe::<i32>()).collect();

    // Each source sends 20 messages
    for (src_idx, source) in sources.iter().enumerate() {
        for i in 0..20 {
            source.send((src_idx * 100 + i) as i32).await.unwrap();
        }
    }

    // Each subscriber should receive 100 messages (5 sources * 20 messages)
    for (idx, sub) in subs.iter_mut().enumerate() {
        let mut count = 0;
        loop {
            match tokio::time::timeout(Duration::from_millis(100), sub.recv()).await {
                Ok(Some(_)) => count += 1,
                _ => break,
            }
        }
        assert_eq!(count, 100, "subscriber {} received {} messages", idx, count);
    }
}

#[tokio::test]
async fn stress_test_rapid_filter_create_and_send() {
    // Rapidly create filters and send - tests that ready signals work correctly
    let stream = Relay::new();

    for iteration in 0..100 {
        let filtered = stream.filter::<i32, _>(move |x| *x == iteration);
        let mut sub = filtered.subscribe::<i32>();

        // Send immediately after filter creation
        stream.send(iteration).await.unwrap();
        stream.send(iteration + 1000).await.unwrap(); // This one shouldn't match

        let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
            .await
            .expect("timeout")
            .expect("no message");

        assert_eq!(*msg, iteration);
    }
}

#[tokio::test]
async fn stress_test_split_under_load() {
    // Test filter with heavy load - split by value (even/odd)
    // Note: split::<T>() splits by TYPE, not by value. Use filter for value-based splitting.
    // Use larger buffer to handle all messages
    let stream = Relay::with_channel_size(512);

    // Filter to separate even/odd by value
    let evens_filtered = stream.filter::<i32, _>(|x| *x % 2 == 0);
    let odds_filtered = stream.filter::<i32, _>(|x| *x % 2 != 0);

    let mut even_sub = evens_filtered.subscribe::<i32>();
    let mut odd_sub = odds_filtered.subscribe::<i32>();

    const COUNT: i32 = 500;

    for i in 0..COUNT {
        stream.send(i).await.unwrap();
    }

    let mut even_count = 0;
    let mut odd_count = 0;

    loop {
        match tokio::time::timeout(Duration::from_millis(100), even_sub.recv()).await {
            Ok(Some(msg)) => {
                assert_eq!(*msg % 2, 0, "expected even, got {}", *msg);
                even_count += 1;
            }
            _ => break,
        }
    }

    loop {
        match tokio::time::timeout(Duration::from_millis(100), odd_sub.recv()).await {
            Ok(Some(msg)) => {
                assert_eq!(*msg % 2, 1, "expected odd, got {}", *msg);
                odd_count += 1;
            }
            _ => break,
        }
    }

    assert_eq!(even_count, (COUNT / 2) as usize);
    assert_eq!(odd_count, (COUNT / 2) as usize);
}

#[tokio::test]
async fn stress_test_interleaved_subscribe_send() {
    // Interleave subscribes and sends
    // Use larger buffer to handle messages
    let stream = Relay::with_channel_size(64);
    let filtered = stream.filter::<i32, _>(|_| true);

    let mut all_subs = Vec::new();

    for i in 0..50 {
        // Subscribe
        all_subs.push(filtered.subscribe::<i32>());

        // Send
        stream.send(i).await.unwrap();
        // Yield to let forwarder process the message before next subscribe
        tokio::task::yield_now().await;
    }

    // First subscriber should have received all 50 messages
    // Last subscriber should have received only 1 message (the last one)
    for (idx, sub) in all_subs.iter_mut().enumerate() {
        let mut count = 0;
        loop {
            match tokio::time::timeout(Duration::from_millis(50), sub.recv()).await {
                Ok(Some(_)) => count += 1,
                _ => break,
            }
        }
        let expected = 50 - idx;
        assert_eq!(
            count, expected,
            "subscriber {} received {} messages, expected {}",
            idx, count, expected
        );
    }
}

// ==================== ERROR HANDLING TESTS ====================

#[derive(Debug, Clone)]
struct TestError(String);

impl std::fmt::Display for TestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TestError: {}", self.0)
    }
}

impl std::error::Error for TestError {}

#[tokio::test]
async fn test_sink_error_propagation() {
    let stream = Relay::new();

    // Sink that returns error for "bad" messages
    stream.sink::<String, _, _>(|msg| {
        if msg.as_str() == "bad" {
            return Err(TestError("message was bad".to_string()));
        }
        Ok(())
    });

    // Good message should succeed
    let result = stream.send("good".to_string()).await;
    assert!(result.is_ok());

    // Bad message should return error
    let result = stream.send("bad".to_string()).await;
    assert!(result.is_err());
    if let Err(SendError::Downstream(err)) = result {
        assert_eq!(err.source, "sink");
    } else {
        panic!("Expected Downstream error");
    }
}

#[tokio::test]
async fn test_tap_error_propagation() {
    let stream = Relay::new();

    // Tap that returns error for negative numbers
    let _tapped = stream.tap::<i32, _, _>(|n| {
        if *n < 0 {
            return Err(TestError("negative number".to_string()));
        }
        Ok(())
    });

    // Positive number should succeed
    let result = stream.send(42i32).await;
    assert!(result.is_ok());

    // Negative number should return error
    let result = stream.send(-1i32).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_error_subscription() {
    let stream = Relay::new();

    // Subscribe to errors
    let mut error_sub = stream.subscribe::<RelayError>();

    // Sink that returns error
    stream.sink::<String, _, _>(|msg| {
        if msg.as_str() == "error" {
            return Err(TestError("triggered error".to_string()));
        }
        Ok(())
    });

    // Send error-triggering message
    let _ = stream.send("error".to_string()).await;

    // Should receive the error as a message
    let error = tokio::time::timeout(Duration::from_millis(100), error_sub.recv())
        .await
        .unwrap();
    assert!(error.is_some());
    let error = error.unwrap();
    assert_eq!(error.source, "sink");
}

#[tokio::test]
async fn test_sink_without_error() {
    let stream = Relay::new();
    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = counter.clone();

    // Sink that returns () (no error)
    stream.sink::<i32, _, _>(move |n| {
        counter_clone.fetch_add(*n as usize, Ordering::SeqCst);
        // Returns (), which is converted to Ok(()) by IntoResult
    });

    stream.send(10i32).await.unwrap();
    stream.send(20i32).await.unwrap();

    // Wait for processing
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(counter.load(Ordering::SeqCst), 30);
}

#[tokio::test]
async fn test_fire_and_forget() {
    let stream = Relay::new();
    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = counter.clone();

    stream.sink::<i32, _, _>(move |n| {
        counter_clone.fetch_add(*n as usize, Ordering::SeqCst);
    });

    // Fire and forget - send without awaiting completion
    // But we still need to await to send the message
    let _ = stream.send(10i32).await;
    let _ = stream.send(20i32).await;

    // Wait for processing
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(counter.load(Ordering::SeqCst), 30);
}

#[tokio::test]
async fn test_send_any() {
    use std::any::TypeId;

    let stream = Relay::new();
    let mut sub = stream.subscribe::<String>();

    // Send a type-erased value with explicit TypeId
    let value: Arc<dyn std::any::Any + Send + Sync> = Arc::new("hello".to_string());
    stream
        .send_any(value, TypeId::of::<String>())
        .await
        .unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&"hello".to_string()));
}

#[tokio::test]
async fn test_send_envelope() {
    use crate::Envelope;

    let stream = Relay::new();
    let mut sub = stream.subscribe::<i32>();

    // Create and send envelope directly
    let envelope = Envelope::new(42i32, 0, None);
    stream.send_envelope(envelope).await.unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&42i32));
}

#[tokio::test]
async fn test_within_basic() {
    let stream = Relay::new();
    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = counter.clone();

    let mut sub = stream.subscribe::<i32>();
    stream.within(move || async move {
        while let Some(n) = sub.recv().await {
            counter_clone.fetch_add(*n as usize, Ordering::SeqCst);
        }
    });

    stream.send(10i32).await.unwrap();
    stream.send(20i32).await.unwrap();

    // Wait for processing
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(counter.load(Ordering::SeqCst), 30);
}

#[tokio::test]
async fn test_multiple_handlers_all_must_complete() {
    let stream = Relay::new();
    let counter1 = Arc::new(AtomicUsize::new(0));
    let counter2 = Arc::new(AtomicUsize::new(0));
    let c1 = counter1.clone();
    let c2 = counter2.clone();

    // Two handlers
    stream.sink::<i32, _, _>(move |n| {
        c1.fetch_add(*n as usize, Ordering::SeqCst);
    });
    stream.sink::<i32, _, _>(move |n| {
        c2.fetch_add(*n as usize * 2, Ordering::SeqCst);
    });

    // Send should wait for both handlers
    stream.send(10i32).await.unwrap();

    // Wait for processing
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(counter1.load(Ordering::SeqCst), 10);
    assert_eq!(counter2.load(Ordering::SeqCst), 20);
}

#[tokio::test]
async fn test_error_from_one_handler() {
    let stream = Relay::new();
    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = counter.clone();

    // One handler that succeeds
    stream.sink::<i32, _, _>(move |n| {
        counter_clone.fetch_add(*n as usize, Ordering::SeqCst);
    });

    // Another handler that fails for negative
    stream.sink::<i32, _, _>(|n| {
        if *n < 0 {
            return Err(TestError("negative".to_string()));
        }
        Ok(())
    });

    // Positive should succeed (both handlers run)
    stream.send(10i32).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;
    assert_eq!(counter.load(Ordering::SeqCst), 10);

    // Negative should fail (first handler still runs)
    let result = stream.send(-5i32).await;
    assert!(result.is_err());

    // Wait and check first handler still processed
    tokio::time::sleep(Duration::from_millis(50)).await;
    // Note: First handler runs regardless but we might get -5 as usize overflow
    // Let's use u32 to avoid this
}

#[tokio::test]
async fn test_no_handlers_immediate_return() {
    let stream = Relay::new();

    // No handlers registered
    // send().await should return immediately
    let result = stream.send("test".to_string()).await;
    assert!(result.is_ok());
}

// ==================== RACE CONDITION / STRESS TESTS FOR ERROR HANDLING ====================

#[tokio::test]
async fn stress_test_concurrent_errors() {
    // Multiple handlers receiving the same message, some succeed, some fail
    let stream = Relay::new();
    let success_count = Arc::new(AtomicUsize::new(0));
    let error_count = Arc::new(AtomicUsize::new(0));

    // 5 handlers: odd ones fail, even ones succeed
    for i in 0..5 {
        let sc = success_count.clone();
        let ec = error_count.clone();
        stream.sink::<i32, _, _>(move |_n| {
            if i % 2 == 1 {
                ec.fetch_add(1, Ordering::SeqCst);
                Err(TestError(format!("handler {} failed", i)))
            } else {
                sc.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });
    }

    // Send 100 messages
    for _ in 0..100 {
        let result = stream.send(1i32).await;
        // Should get an error (from odd handlers) but all handlers still run
        assert!(result.is_err());
    }

    // Wait for all processing
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Each message processed by 3 success handlers and 2 error handlers
    assert_eq!(success_count.load(Ordering::SeqCst), 300); // 100 * 3
    assert_eq!(error_count.load(Ordering::SeqCst), 200); // 100 * 2
}

#[tokio::test]
async fn stress_test_rapid_error_sending() {
    let stream = Relay::with_channel_size(1024);

    // Handler that always fails
    stream.sink::<i32, _, _>(|_| Err(TestError("always fails".to_string())));

    // Send many messages rapidly
    let mut error_count = 0;
    for i in 0..500 {
        let result = stream.send(i).await;
        if result.is_err() {
            error_count += 1;
        }
    }

    // All should fail
    assert_eq!(error_count, 500);
}

#[tokio::test]
async fn stress_test_error_subscription_under_load() {
    let stream = Relay::with_channel_size(1024);
    let errors_received = Arc::new(AtomicUsize::new(0));
    let errors_received_clone = errors_received.clone();

    // Subscribe to errors
    let mut error_sub = stream.subscribe::<RelayError>();
    tokio::spawn(async move {
        while let Some(_err) = error_sub.recv().await {
            errors_received_clone.fetch_add(1, Ordering::SeqCst);
        }
    });

    // Handler that fails for even numbers
    stream.sink::<i32, _, _>(|n| {
        if *n % 2 == 0 {
            Err(TestError("even number".to_string()))
        } else {
            Ok(())
        }
    });

    // Send 200 messages (100 even, 100 odd)
    for i in 0..200 {
        let _ = stream.send(i).await;
    }

    // Wait for error propagation
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Should have received 100 errors (one per even number)
    assert_eq!(errors_received.load(Ordering::SeqCst), 100);
}

#[tokio::test]
async fn stress_test_concurrent_senders_with_errors() {
    let stream = Relay::with_channel_size(2048);
    let error_count = Arc::new(AtomicUsize::new(0));
    let success_count = Arc::new(AtomicUsize::new(0));

    // Handler that fails for multiples of 10
    let ec = error_count.clone();
    let sc = success_count.clone();
    stream.sink::<i32, _, _>(move |n| {
        if *n % 10 == 0 {
            ec.fetch_add(1, Ordering::SeqCst);
            Err(TestError("multiple of 10".to_string()))
        } else {
            sc.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    });

    // Spawn multiple concurrent senders
    let handles: Vec<_> = (0..10)
        .map(|sender_id| {
            let stream = stream.clone();
            tokio::spawn(async move {
                for i in 0..100 {
                    let value = sender_id * 100 + i;
                    let _ = stream.send(value).await;
                }
            })
        })
        .collect();

    // Wait for all senders
    for handle in handles {
        handle.await.unwrap();
    }

    // Wait for processing
    tokio::time::sleep(Duration::from_millis(100)).await;

    // 1000 messages total, 100 are multiples of 10 (0, 10, 20, ...)
    // Each sender sends 10 multiples of 10 (0, 10, 20, ... 90)
    // Total errors: 10 senders * 10 multiples = 100
    assert_eq!(error_count.load(Ordering::SeqCst), 100);
    assert_eq!(success_count.load(Ordering::SeqCst), 900);
}

#[tokio::test]
async fn stress_test_handler_registration_during_sends() {
    let stream = Relay::new();
    let total_received = Arc::new(AtomicUsize::new(0));

    // Start sending immediately
    let stream_clone = stream.clone();
    let sender_handle = tokio::spawn(async move {
        for i in 0..100 {
            let _ = stream_clone.send(i).await;
            // Small yield to allow handler registration
            if i % 10 == 0 {
                tokio::task::yield_now().await;
            }
        }
    });

    // Register handlers while sending is in progress
    for _ in 0..5 {
        tokio::task::yield_now().await;
        let tr = total_received.clone();
        stream.sink::<i32, _, _>(move |_| {
            tr.fetch_add(1, Ordering::SeqCst);
        });
    }

    sender_handle.await.unwrap();
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Some messages should have been received by some handlers
    // The exact count depends on timing, but should be > 0
    assert!(total_received.load(Ordering::SeqCst) > 0);
}

#[tokio::test]
async fn stress_test_within_with_panics() {
    let stream = Relay::new();
    let processed = Arc::new(AtomicUsize::new(0));
    let processed_clone = processed.clone();

    // Subscribe to errors to verify panics are caught
    let mut error_sub = stream.subscribe::<RelayError>();
    let panic_errors = Arc::new(AtomicUsize::new(0));
    let pe = panic_errors.clone();
    tokio::spawn(async move {
        while let Some(err) = error_sub.recv().await {
            if err.source == "within" || err.source == "subscription" {
                pe.fetch_add(1, Ordering::SeqCst);
            }
        }
    });

    // Handler using within() that panics on certain values
    let mut sub = stream.subscribe::<i32>();
    stream.within(move || async move {
        while let Some(n) = sub.recv().await {
            processed_clone.fetch_add(1, Ordering::SeqCst);
            if *n == 50 {
                panic!("deliberate panic at 50");
            }
        }
    });

    // Send messages until panic
    for i in 0..100 {
        let result = stream.send(i).await;
        if i == 50 {
            // The panic should cause an error
            // But due to timing, it might not be immediate
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        if i > 50 && result.is_err() {
            // After panic, sends might fail
            break;
        }
    }

    tokio::time::sleep(Duration::from_millis(100)).await;

    // Should have processed up to 50 messages
    assert!(processed.load(Ordering::SeqCst) >= 50);
}

#[tokio::test]
async fn stress_test_mixed_success_and_error_types() {
    let stream = Relay::new();

    let string_count = Arc::new(AtomicUsize::new(0));
    let int_count = Arc::new(AtomicUsize::new(0));
    let error_count = Arc::new(AtomicUsize::new(0));

    let sc = string_count.clone();
    stream.sink::<String, _, _>(move |_| {
        sc.fetch_add(1, Ordering::SeqCst);
    });

    let ic = int_count.clone();
    let ec = error_count.clone();
    stream.sink::<i32, _, _>(move |n| {
        ic.fetch_add(1, Ordering::SeqCst);
        if *n < 0 {
            ec.fetch_add(1, Ordering::SeqCst);
            Err(TestError("negative".to_string()))
        } else {
            Ok(())
        }
    });

    // Send mixed types
    for i in 0..50 {
        let _ = stream.send(format!("msg_{}", i)).await;
        let _ = stream.send(i).await;
        let _ = stream.send(-i).await; // Negative triggers error
    }

    tokio::time::sleep(Duration::from_millis(200)).await;

    // Allow for slight timing variations
    let sc = string_count.load(Ordering::SeqCst);
    let ic = int_count.load(Ordering::SeqCst);
    let ec = error_count.load(Ordering::SeqCst);

    assert!(
        (48..=50).contains(&sc),
        "string_count {} not in expected range",
        sc
    );
    assert!(
        (98..=100).contains(&ic),
        "int_count {} not in expected range",
        ic
    );
    assert!(
        (48..=50).contains(&ec),
        "error_count {} not in expected range",
        ec
    );
}

#[tokio::test]
async fn stress_test_concurrent_close_propagation() {
    // Test that dropping parent closes all derived streams correctly
    // even when there are many of them
    for _ in 0..10 {
        let stream = Relay::new();
        let mut subs = Vec::new();

        // Create many derived streams and their subscriptions
        for _ in 0..20 {
            let derived = stream.filter::<String, _>(|_| true);
            let sub = derived.subscribe::<String>();
            subs.push((derived, sub));
        }

        // Send a message to ensure forwarders are active
        stream.send("test".to_string()).await.unwrap();

        // Receive the message from all subscriptions first
        for (_derived, sub) in &mut subs {
            let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
                .await
                .expect("should receive message")
                .expect("message should exist");
            assert_eq!(&*msg, "test");
        }

        // Drop parent
        drop(stream);

        // All subscriptions should close (return None on next recv)
        for (_derived, mut sub) in subs {
            let result = tokio::time::timeout(Duration::from_millis(500), sub.recv())
                .await
                .expect("should not timeout");
            assert!(
                result.is_none(),
                "subscription should close when parent dropped"
            );
        }
    }
}

#[tokio::test]
async fn stress_test_race_drop_and_send() {
    // Test race between drop and send - should not hang or panic
    for _ in 0..50 {
        let stream = Relay::new();
        let derived = stream.filter::<i32, _>(|_| true);
        let stream_clone = stream.clone();

        // Spawn sender
        let sender = tokio::spawn(async move {
            for i in 0..10 {
                let _ = stream_clone.send(i).await;
            }
        });

        // Race: drop parent while sending
        tokio::task::yield_now().await;
        drop(stream);

        // Sender should complete (may get Closed errors)
        let _ = tokio::time::timeout(Duration::from_millis(500), sender)
            .await
            .expect("sender should not hang");

        // Derived should be closed
        tokio::time::sleep(Duration::from_millis(50)).await;
        assert!(derived.is_closed());
    }
}

#[tokio::test]
async fn stress_test_rapid_subscribe_unsubscribe() {
    // Test rapid subscribe/unsubscribe doesn't cause issues
    let stream = Relay::new();

    // Spawn a sender
    let stream_clone = stream.clone();
    let sender = tokio::spawn(async move {
        for i in 0..200 {
            let _ = stream_clone.send(i).await;
            tokio::task::yield_now().await;
        }
    });

    // Rapidly subscribe and drop
    for _ in 0..100 {
        let mut sub = stream.subscribe::<i32>();
        // Maybe receive one message
        let _ = tokio::time::timeout(Duration::from_micros(100), sub.recv()).await;
        drop(sub);
    }

    sender.await.unwrap();
}

// ==================== ADDITIONAL EDGE CASE TESTS ====================

#[tokio::test]
async fn test_multiple_errors_first_wins() {
    // When multiple handlers fail, first error should be returned
    let stream = Relay::new();

    // Three handlers that all fail
    stream.sink::<i32, _, _>(|_| Err(TestError("error 1".to_string())));
    stream.sink::<i32, _, _>(|_| Err(TestError("error 2".to_string())));
    stream.sink::<i32, _, _>(|_| Err(TestError("error 3".to_string())));

    let result = stream.send(42i32).await;
    assert!(result.is_err());

    // Should get one of the errors (first to complete wins)
    if let Err(SendError::Downstream(e)) = result {
        assert!(e.source == "sink");
    } else {
        panic!("Expected Downstream error");
    }
}

#[tokio::test]
async fn test_operations_on_closed_stream() {
    let stream = Relay::new();
    let derived = stream.filter::<String, _>(|_| true);

    // Drop the root - this should propagate closure
    drop(stream);

    // Wait for closure to propagate through the forwarder
    // The forwarder task needs to run and detect parent closure
    // Use a longer timeout via recv() instead of is_closed() which may race
    let mut sub = derived.subscribe::<String>();
    let closed_result = tokio::time::timeout(Duration::from_millis(500), sub.recv())
        .await
        .expect("forwarder should close child within 500ms");
    assert!(closed_result.is_none(), "should receive None when closed");

    // Now derived should definitely be closed
    assert!(derived.is_closed());

    // Send on closed derived stream
    let result = derived.send("test".to_string()).await;
    assert_eq!(result, Err(SendError::Closed));

    // Filter on closed stream returns closed stream
    let filtered = derived.filter::<String, _>(|_| true);
    assert!(filtered.is_closed());
}

#[tokio::test]
async fn test_tracker_zero_expected() {
    // When no handlers, send should complete immediately
    let stream = Relay::new();

    // No handlers registered
    let start = std::time::Instant::now();
    let result = stream.send("test".to_string()).await;
    let elapsed = start.elapsed();

    assert!(result.is_ok());
    assert!(
        elapsed < Duration::from_millis(50),
        "Should complete immediately"
    );
}

#[tokio::test]
async fn test_sink_panic_propagation() {
    let stream = Relay::new();
    let mut error_sub = stream.subscribe::<RelayError>();

    // Sink that panics
    stream.sink::<i32, _, _>(|n| {
        if *n == 42 {
            panic!("deliberate panic");
        }
    });

    // Send panic-triggering value
    let result = stream.send(42i32).await;
    assert!(result.is_err());

    // Error should be subscribable
    let error = tokio::time::timeout(Duration::from_millis(100), error_sub.recv())
        .await
        .unwrap();
    assert!(error.is_some());
    let error = error.unwrap();
    assert_eq!(error.source, "sink");
    assert!(error.error.to_string().contains("panic"));
}

#[tokio::test]
async fn test_tap_panic_propagation() {
    let stream = Relay::new();

    // Tap that panics
    let _tapped = stream.tap::<String, _, _>(|s| {
        if s.as_str() == "boom" {
            panic!("tap panic");
        }
    });

    // Normal message succeeds
    let result = stream.send("hello".to_string()).await;
    assert!(result.is_ok());

    // Panic message fails
    let result = stream.send("boom".to_string()).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_handler_slow_completion() {
    let stream = Relay::new();
    let completed = Arc::new(AtomicUsize::new(0));
    let completed_clone = completed.clone();

    // Slow handler
    stream.sink::<i32, _, _>(move |_| {
        std::thread::sleep(Duration::from_millis(50));
        completed_clone.fetch_add(1, Ordering::SeqCst);
    });

    // Send should wait for slow handler
    let start = std::time::Instant::now();
    stream.send(1i32).await.unwrap();
    let elapsed = start.elapsed();

    assert!(
        elapsed >= Duration::from_millis(40),
        "Should wait for handler"
    );
    assert_eq!(completed.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn test_subscription_drop_mid_message() {
    let stream = Relay::new();
    let processed = Arc::new(AtomicUsize::new(0));
    let processed_clone = processed.clone();

    // Handler that will be dropped mid-processing
    let sub = stream.subscribe::<i32>();
    stream.within({
        let mut sub = sub;
        move || async move {
            if let Some(_) = sub.recv().await {
                processed_clone.fetch_add(1, Ordering::SeqCst);
                // Simulate work then drop subscription by exiting
            }
        }
    });

    // Give the within task time to subscribe
    tokio::time::sleep(Duration::from_millis(10)).await;

    // This should complete (within doesn't block send)
    stream.send(1i32).await.unwrap();

    tokio::time::sleep(Duration::from_millis(50)).await;
    assert_eq!(processed.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn test_chained_maps() {
    let stream = Relay::new();

    // Chain of maps: i32 -> double -> square -> string
    let result = stream
        .map::<i32, i32, _>(|n| n * 2)
        .map::<i32, i32, _>(|n| n * n)
        .map::<i32, String, _>(|n| format!("result: {}", n));

    let mut sub = result.subscribe::<String>();

    stream.send(3i32).await.unwrap(); // 3 -> 6 -> 36 -> "result: 36"

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap()
        .unwrap();

    assert_eq!(&*msg, "result: 36");
}

#[tokio::test]
async fn test_error_does_not_block_subsequent_sends() {
    let stream = Relay::new();
    let success_count = Arc::new(AtomicUsize::new(0));
    let sc = success_count.clone();

    // Handler that fails on negative
    stream.sink::<i32, _, _>(move |n| {
        if *n < 0 {
            return Err(TestError("negative".to_string()));
        }
        sc.fetch_add(1, Ordering::SeqCst);
        Ok(())
    });

    // First send fails
    let r1 = stream.send(-1i32).await;
    assert!(r1.is_err());

    // Subsequent sends should still work
    let r2 = stream.send(1i32).await;
    assert!(r2.is_ok());

    let r3 = stream.send(2i32).await;
    assert!(r3.is_ok());

    tokio::time::sleep(Duration::from_millis(50)).await;
    assert_eq!(success_count.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn test_mixed_handler_types_completion() {
    // Handlers for different types should all complete
    let stream = Relay::new();
    let string_done = Arc::new(AtomicUsize::new(0));
    let int_done = Arc::new(AtomicUsize::new(0));
    let sd = string_done.clone();
    let id = int_done.clone();

    stream.sink::<String, _, _>(move |_| {
        sd.fetch_add(1, Ordering::SeqCst);
    });

    stream.sink::<i32, _, _>(move |_| {
        id.fetch_add(1, Ordering::SeqCst);
    });

    // Send string - both handlers count, but only string handler processes
    stream.send("test".to_string()).await.unwrap();

    // Send int - both handlers count, but only int handler processes
    stream.send(42i32).await.unwrap();

    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(string_done.load(Ordering::SeqCst), 1);
    assert_eq!(int_done.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn test_rapid_handler_registration_deregistration() {
    let stream = Relay::new();
    let total = Arc::new(AtomicUsize::new(0));

    // Rapidly register and let handlers complete
    for _ in 0..10 {
        let t = total.clone();
        stream.sink::<i32, _, _>(move |_| {
            t.fetch_add(1, Ordering::SeqCst);
        });
    }

    // Send one message
    stream.send(1i32).await.unwrap();

    tokio::time::sleep(Duration::from_millis(100)).await;

    // All 10 handlers should have processed
    assert_eq!(total.load(Ordering::SeqCst), 10);
}

#[ignore]

// ============================================================================
// Tests for new convenience methods
// ============================================================================

#[tokio::test]
async fn test_handler_count() {
    let stream = Relay::new();

    // Initially no handlers
    assert_eq!(stream.handler_count(), 0);

    // Add a sink
    stream.sink::<i32, _, _>(|_| {});
    tokio::time::sleep(Duration::from_millis(10)).await;
    assert_eq!(stream.handler_count(), 1);

    // Add a tap
    stream.tap::<i32, _, _>(|_| {});
    tokio::time::sleep(Duration::from_millis(10)).await;
    assert_eq!(stream.handler_count(), 2);

    // subscribe() doesn't count
    let _sub = stream.subscribe::<i32>();
    assert_eq!(stream.handler_count(), 2);
}

#[ignore]

#[ignore]

#[ignore]
#[tokio::test]
async fn test_batch_basic() {
    let stream = Relay::new();

    // Batch integers into groups of 3
    let batched = stream.batch::<i32>(3);
    let mut sub = batched.subscribe::<Vec<i32>>();

    // Send 5 items
    for i in 1..=5 {
        stream.send(i).await.unwrap();
    }

    // First batch should be [1, 2, 3]
    let batch1 = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(&*batch1, &vec![1, 2, 3]);

    // Drop stream to flush remaining
    drop(stream);

    // Second batch should be [4, 5]
    let batch2 = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(&*batch2, &vec![4, 5]);
}

#[ignore]
#[tokio::test]
async fn test_batch_exact_multiple() {
    let stream = Relay::new();

    // Batch into groups of 2
    let batched = stream.batch::<i32>(2);
    let mut sub = batched.subscribe::<Vec<i32>>();

    // Send exactly 4 items (2 full batches)
    for i in 1..=4 {
        stream.send(i).await.unwrap();
    }

    let batch1 = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(&*batch1, &vec![1, 2]);

    let batch2 = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(&*batch2, &vec![3, 4]);
}

#[ignore]
#[tokio::test]
async fn test_batch_with_sink() {
    let stream = Relay::new();
    let batches_received = Arc::new(AtomicUsize::new(0));
    let br = batches_received.clone();

    let batched = stream.batch::<String>(2);

    // Use tap instead of sink - tap returns self for chaining
    // and the batched stream doesn't need to track completion
    let mut sub = batched.subscribe::<Vec<String>>();

    // Spawn receiver
    let handle = tokio::spawn(async move {
        while let Some(batch) = sub.recv().await {
            assert!(batch.len() <= 2);
            br.fetch_add(1, Ordering::SeqCst);
        }
    });

    // Send 4 items (exactly 2 full batches)
    stream.send("a".to_string()).await.unwrap();
    stream.send("b".to_string()).await.unwrap();
    stream.send("c".to_string()).await.unwrap();
    stream.send("d".to_string()).await.unwrap();

    // Close stream to flush any remaining and stop receiver
    drop(stream);

    // Wait for receiver to finish
    let _ = tokio::time::timeout(Duration::from_millis(500), handle).await;

    // Should have received 2 batches
    assert_eq!(batches_received.load(Ordering::SeqCst), 2);
}

#[tokio::test]
#[should_panic(expected = "batch size must be > 0")]
async fn test_batch_zero_size_panics() {
    let stream = Relay::new();
    let _ = stream.batch::<i32>(0);
}

// ============================================================================
// NEW API TESTS: Stream, Duplex, Readable, Writable, Pipe
// ============================================================================

#[tokio::test]
async fn test_stream_basic_send_subscribe() {
    let stream = Relay::new();
    let mut sub = stream.subscribe::<String>();

    stream.send("hello".to_string()).await.unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&"hello".to_string()));
}



#[tokio::test]
async fn test_channel_no_echo() {
    // Channel sender and receiver are separate - no automatic echo
    let (tx, rx) = Relay::channel();

    let mut sub = rx.subscribe::<String>();

    // Send message
    tx.send("test".to_string()).await.unwrap();

    // Should be received on subscription
    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&"test".to_string()));
}

/* REMOVED: Duplex API no longer exists

*/


/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_close() {
    let duplex = Duplex::new();

    assert!(!duplex.is_closed());

    duplex.close();
    assert!(duplex.input.is_closed());
}

*/


#[tokio::test]
async fn test_stream_is_loopback() {
    // Stream with shared inner should loopback
    let stream = Relay::new();
    let mut sub = stream.subscribe::<i32>();

    stream.send(123i32).await.unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&123i32));
}

#[tokio::test]
async fn test_stream_close() {
    let stream = Relay::new();

    assert!(!stream.is_closed());
    stream.close();
    assert!(stream.is_closed());

    // Send should fail on closed stream
    let result = stream.send("test".to_string()).await;
    assert!(matches!(result, Err(SendError::Closed)));
}

// ============================================================================
// INVARIANT TESTS - Adversarial tests for core guarantees
// ============================================================================
//
// These tests verify the core invariants:
// I1 - Completion safety: send().await returns Ok only when all tracked handlers completed
// I2 - Failure propagation: handler failures propagate to sender and are emitted once
// I3 - Echo prevention: no envelope delivered back to its origin via piping
// I4 - No silent deadlock: send() only waits for actively running handlers
// I5 - Closure monotonicity: once closed, no new messages accepted, downstreams close

// ==================== I1: Completion Safety ====================

#[tokio::test]
async fn invariant_send_waits_for_all_tracked_handlers() {
    // I1: send().await must wait for all tracked handlers to complete
    let relay = Relay::new();
    let flag = Arc::new(AtomicBool::new(false));

    let f = flag.clone();
    relay.tap::<i32, _, _>(move |_| {
        std::thread::sleep(Duration::from_millis(50));
        f.store(true, Ordering::SeqCst);
    });

    relay.send(1i32).await.unwrap();

    assert!(
        flag.load(Ordering::SeqCst),
        "send() must wait for handler to complete"
    );
}

#[tokio::test]
async fn invariant_multiple_handlers_all_complete() {
    // I1: All tracked handlers must complete before send() returns
    let relay = Relay::new();
    let count = Arc::new(AtomicUsize::new(0));

    for _ in 0..5 {
        let c = count.clone();
        relay.sink::<i32, _, _>(move |_| {
            std::thread::sleep(Duration::from_millis(10));
            c.fetch_add(1, Ordering::SeqCst);
        });
    }

    relay.send(1i32).await.unwrap();

    assert_eq!(
        count.load(Ordering::SeqCst),
        5,
        "all 5 handlers must complete"
    );
}

// ==================== I2: Failure Propagation ====================

#[tokio::test]
async fn invariant_downstream_error_propagates() {
    // I2: Handler errors must propagate to sender
    let relay = Relay::new();

    relay.tap::<i32, _, Result<(), std::io::Error>>(|_| {
        Err(std::io::Error::new(std::io::ErrorKind::Other, "fail"))
    });

    let result = relay.send(1i32).await;
    assert!(
        matches!(result, Err(SendError::Downstream(_))),
        "error must propagate"
    );
}

#[tokio::test]
async fn invariant_panic_propagates_as_error() {
    // I2: Handler panics must propagate as errors
    let relay = Relay::new();

    relay.tap::<i32, _, ()>(|_| {
        panic!("deliberate panic");
    });

    let result = relay.send(1i32).await;
    assert!(
        matches!(result, Err(SendError::Downstream(_))),
        "panic must propagate as error"
    );
}

#[tokio::test]
async fn invariant_error_emitted_exactly_once() {
    // I2: Errors should be emitted exactly once to error subscribers
    let relay = Relay::new();
    let error_count = Arc::new(AtomicUsize::new(0));
    let ec = error_count.clone();

    let mut error_sub = relay.subscribe::<RelayError>();
    tokio::spawn(async move {
        while let Some(_) = error_sub.recv().await {
            ec.fetch_add(1, Ordering::SeqCst);
        }
    });

    relay.sink::<i32, _, _>(|_| Err(TestError("fail".to_string())));

    let _ = relay.send(1i32).await;
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(
        error_count.load(Ordering::SeqCst),
        1,
        "error must be emitted exactly once"
    );
}

// ==================== I3: Echo Prevention ====================

#[tokio::test]
async fn invariant_echo_prevention_bidirectional_pipe() {
    // I3: Bidirectional piping must not cause feedback loops
    let a = Relay::new();
    let b = Relay::new();

    let a2 = a.clone();
    let b2 = b.clone();
    tokio::spawn(async move {
        a2.forward(&b2).await;
    });
    let b3 = b.clone();
    let a3 = a.clone();
    tokio::spawn(async move {
        b3.forward(&a3).await;
    });
    tokio::time::sleep(Duration::from_millis(10)).await;

    let count = Arc::new(AtomicUsize::new(0));
    let c = count.clone();

    a.sink::<i32, _, _>(move |_| {
        c.fetch_add(1, Ordering::SeqCst);
    });

    a.send(1i32).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(
        count.load(Ordering::SeqCst),
        1,
        "message must be received exactly once, not echoed"
    );
}

#[tokio::test]
async fn invariant_echo_prevention_triangle() {
    // I3: Echo prevention in a triangle topology
    let a = Relay::new();
    let b = Relay::new();
    let c = Relay::new();

    let a2 = a.clone();
    let b2 = b.clone();
    tokio::spawn(async move {
        a2.forward(&b2).await;
    });
    let b3 = b.clone();
    let c2 = c.clone();
    tokio::spawn(async move {
        b3.forward(&c2).await;
    });
    let c3 = c.clone();
    let a3 = a.clone();
    tokio::spawn(async move {
        c3.forward(&a3).await;
    });
    tokio::time::sleep(Duration::from_millis(10)).await;

    let count = Arc::new(AtomicUsize::new(0));
    let cnt = count.clone();

    a.sink::<i32, _, _>(move |_| {
        cnt.fetch_add(1, Ordering::SeqCst);
    });

    a.send(1i32).await.unwrap();
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Message originates from a, goes to b, then c, but should NOT come back to a
    assert_eq!(
        count.load(Ordering::SeqCst),
        1,
        "message must not loop back"
    );
}

// ==================== I4: No Silent Deadlock ====================

#[tokio::test]
async fn invariant_no_deadlock_on_handler_panic() {
    // I4: Panicking handler must not cause send() to deadlock
    let relay = Relay::new();

    relay.tap::<i32, _, _>(|v| {
        if *v == 1 {
            panic!("boom");
        }
    });

    let result = tokio::time::timeout(Duration::from_millis(500), relay.send(1i32)).await;

    assert!(result.is_ok(), "send() must not deadlock on handler panic");
}

#[tokio::test]
async fn invariant_handler_count_restored_after_panic() {
    // I4: handler_count must return to correct value after panic
    let relay = Relay::new();

    relay.tap::<i32, _, ()>(|_| {
        panic!("boom");
    });

    let _ = relay.send(1i32).await;
    tokio::time::sleep(Duration::from_millis(50)).await;

    // After panic, handler task exits and HandlerGuard decrements count
    // But the task is still running (just completed one iteration)
    // Actually, tap spawns a long-running task that loops, so it doesn't exit on one panic
    // Let's close the relay to terminate the task
    relay.close();
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(
        relay.handler_count(),
        0,
        "handler_count must be 0 after relay closed"
    );
}

#[tokio::test]
async fn invariant_no_deadlock_filter_setup() {
    // I4: Filter setup must not cause deadlock even under concurrent sends
    let relay = Relay::new();

    // Setup filter and immediately try to send
    let filtered = relay.filter::<i32, _>(|v| *v > 0);
    let mut sub = filtered.subscribe::<i32>();

    let result = tokio::time::timeout(Duration::from_millis(100), relay.send(42i32)).await;

    assert!(
        result.is_ok(),
        "send() must not deadlock during filter setup"
    );

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv()).await;
    assert!(msg.is_ok(), "message should arrive");
}

// ==================== I5: Closure Monotonicity ====================

#[tokio::test]
async fn invariant_closed_relay_rejects_sends() {
    // I5: Closed relay must reject new messages
    let relay = Relay::new();
    relay.close();

    let result = relay.send(1i32).await;
    assert!(matches!(result, Err(SendError::Closed)));
}

#[tokio::test]
async fn invariant_closure_propagates_to_derived() {
    // I5: Closing parent must eventually close all derived relays
    let relay = Relay::new();
    let filtered = relay.filter::<i32, _>(|_| true);
    let mapped = filtered.map::<i32, i32, _>(|v| *v * 2);

    relay.close();
    tokio::time::sleep(Duration::from_millis(100)).await;

    assert!(filtered.is_closed(), "filtered must be closed");
    assert!(mapped.is_closed(), "mapped must be closed");
}

#[tokio::test]
async fn invariant_subscriptions_close_on_relay_close() {
    // I5: Subscriptions must receive None when relay closes
    let relay = Relay::new();
    let mut sub = relay.subscribe::<i32>();

    relay.send(1i32).await.unwrap();
    relay.close();

    // Should receive the message
    let msg = sub.recv().await;
    assert!(msg.is_some());

    // Then should receive None
    let closed = sub.recv().await;
    assert!(closed.is_none(), "subscription must close");
}

// ==================== Soak/Fuzz Test ====================

#[tokio::test]
async fn soak_random_pipeline_no_deadlock() {
    // Stress test: random pipeline operations should not deadlock
    let relay = Relay::with_channel_size(256);

    // Spawn many concurrent pipeline builders
    let handles: Vec<_> = (0..20)
        .map(|i| {
            let r = relay.clone();
            tokio::spawn(async move {
                let filtered = r.filter::<i32, _>(move |v| v % 2 == i % 2);
                let mapped = filtered.map::<i32, i32, _>(|v| v + 1);
                mapped.sink::<i32, _, _>(|_| {});
            })
        })
        .collect();

    // Wait for pipelines to be set up
    for h in handles {
        let _ = h.await;
    }

    // Send many messages
    for i in 0..100 {
        let result = tokio::time::timeout(Duration::from_millis(100), relay.send(i)).await;

        assert!(result.is_ok(), "send {} must not deadlock", i);
    }
}

// ==================== Regression Tests (fixes we implemented) ====================

#[tokio::test]
async fn regression_ready_guard_signals_on_drop() {
    // Tests that ReadyGuard signals readiness even if forwarder task is cancelled
    // This would have caused deadlocks before the fix
    let relay = Relay::new();

    // Create filter which uses ReadyGuard internally
    let _filtered = relay.filter::<i32, _>(|_| true);

    // If ReadyGuard doesn't signal on drop, this would deadlock
    let result = tokio::time::timeout(Duration::from_millis(100), relay.send(1i32)).await;

    assert!(
        result.is_ok(),
        "send must not deadlock - ReadyGuard should signal on drop"
    );
}

#[tokio::test]
async fn regression_handler_guard_decrements_on_panic() {
    // Tests that HandlerGuard decrements handler_count even on panic
    let relay = Relay::new();

    // Create sink that will panic
    relay.sink::<i32, _, ()>(|_| {
        panic!("intentional panic");
    });

    // Verify handler was registered
    assert_eq!(relay.handler_count(), 1);

    // Send will trigger panic
    let _ = relay.send(1i32).await;

    // Close to terminate the handler loop
    relay.close();
    tokio::time::sleep(Duration::from_millis(50)).await;

    // HandlerGuard should have decremented on task exit
    assert_eq!(
        relay.handler_count(),
        0,
        "handler_count must be 0 after handler task exits"
    );
}

#[tokio::test]
async fn regression_ready_signals_use_notify_not_oneshot() {
    // Tests that ready signal mechanism uses notify_one (stores permit) not notify_waiters
    // This would have caused race conditions before the fix
    let relay = Relay::new();

    // Rapidly create many filters and send
    for i in 0..50 {
        let _filtered = relay.filter::<i32, _>(move |v| *v == i);

        let result = tokio::time::timeout(Duration::from_millis(100), relay.send(i)).await;

        assert!(
            result.is_ok(),
            "send {} must not deadlock with rapid filter creation",
            i
        );
    }
}

#[tokio::test]
async fn regression_ready_guard_releases_arc() {
    // Tests that ReadyGuard releases Arc<Inner> after signaling
    // This ensures parent stream can be properly cleaned up
    let relay = Relay::new();
    let filtered = relay.filter::<String, _>(|_| true);
    let mut sub = filtered.subscribe::<String>();

    // Send a message to ensure forwarder is running
    relay.send("test".to_string()).await.unwrap();

    // Receive the message
    let _ = sub.recv().await;

    // Drop parent - should propagate closure to child
    drop(relay);

    // Child should close within a reasonable time
    // (This would hang if ReadyGuard held Arc<Inner>)
    let result = tokio::time::timeout(Duration::from_millis(500), sub.recv()).await;

    assert!(
        result.is_ok(),
        "closure should propagate - ReadyGuard must release Arc"
    );
    assert!(
        result.unwrap().is_none(),
        "subscription should return None on closure"
    );
}

#[tokio::test]
async fn soak_concurrent_subscribe_unsubscribe() {
    // Stress test: rapid subscribe/unsubscribe during sends
    let relay = Relay::with_channel_size(256);
    let relay_clone = relay.clone();

    // Sender task
    let sender = tokio::spawn(async move {
        for i in 0..200 {
            let _ = relay_clone.send(i).await;
            tokio::task::yield_now().await;
        }
    });

    // Subscriber churn task
    let relay_clone2 = relay.clone();
    let churner = tokio::spawn(async move {
        for _ in 0..50 {
            let sub = relay_clone2.subscribe::<i32>();
            tokio::task::yield_now().await;
            drop(sub);
        }
    });

    let results = tokio::time::timeout(Duration::from_secs(5), async {
        let _ = sender.await;
        let _ = churner.await;
    })
    .await;

    assert!(
        results.is_ok(),
        "must not deadlock under subscribe/unsubscribe churn"
    );
}



// ==================== Forward/Pipe Independence Tests ====================
//
// These tests verify that forward/pipe do NOT propagate closure to the target.
// The target is an independent stream that may have other sources, so closing
// one source should NOT close the target. Derived streams (filter, map, etc.)
// handle closure propagation differently - they DO close their children.


#[tokio::test]
async fn test_derived_streams_propagate_closure() {
    // Derived streams (filter, map, etc.) SHOULD propagate closure to children.
    // This is different from forward/pipe which connect independent streams.

    let parent = Relay::new();
    let filtered = parent.filter::<String, _>(|_| true);
    let mut sub = filtered.subscribe::<String>();

    // Send a message to verify it works
    parent.send("test".to_string()).await.unwrap();

    let msg = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(msg.as_deref(), Some(&"test".to_string()));

    // Drop parent - derived stream should close
    drop(parent);

    // Subscription should close (receive None)
    let closed = tokio::time::timeout(Duration::from_millis(500), sub.recv())
        .await
        .expect("should not timeout - closure should propagate to derived");
    assert!(
        closed.is_none(),
        "derived subscription should close when parent dropped"
    );
}

// ============================================================================
// DUPLEX TRANSFORM UTILITY TESTS
// ============================================================================

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_transform_basic() {
    // Transform String → usize (string length)
    let duplex = Duplex::new().transform::<String, usize, _>(|s| s.len());

    let mut sub = duplex.output.subscribe::<usize>();

    // Send string to writable, expect length on readable
    duplex.input.send("hello".to_string()).await.unwrap();

    let len = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();
    assert_eq!(len.as_deref(), Some(&5usize));
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_transform_passthrough_other_types() {
    // Transform String → usize, but i32 should pass through
    let duplex = Duplex::new().transform::<String, usize, _>(|s| s.len());

    let mut string_len_sub = duplex.output.subscribe::<usize>();
    let mut int_sub = duplex.output.subscribe::<i32>();

    // Send both types
    duplex.input.send("hello".to_string()).await.unwrap();
    duplex.input.send(42i32).await.unwrap();

    // String transformed to length
    let len = tokio::time::timeout(Duration::from_millis(100), string_len_sub.recv())
        .await
        .unwrap();
    assert_eq!(len.as_deref(), Some(&5usize));

    // i32 passes through unchanged
    let num = tokio::time::timeout(Duration::from_millis(100), int_sub.recv())
        .await
        .unwrap();
    assert_eq!(num.as_deref(), Some(&42i32));
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_transform_chained() {
    // Chain transforms: i32 → double, then String → length
    let duplex = Duplex::new()
        .transform::<i32, i32, _>(|n| n * 2)
        .transform::<String, usize, _>(|s| s.len());

    let mut int_sub = duplex.output.subscribe::<i32>();
    let mut len_sub = duplex.output.subscribe::<usize>();

    duplex.input.send(5i32).await.unwrap();
    duplex.input.send("test".to_string()).await.unwrap();

    // i32 is doubled
    let doubled = tokio::time::timeout(Duration::from_millis(100), int_sub.recv())
        .await
        .unwrap();
    assert_eq!(doubled.as_deref(), Some(&10i32));

    // String is converted to length
    let len = tokio::time::timeout(Duration::from_millis(100), len_sub.recv())
        .await
        .unwrap();
    assert_eq!(len.as_deref(), Some(&4usize));
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_filter_transform_matching() {
    // Filter-transform: only transform positive i32 → String
    let duplex = Duplex::new().filter_transform::<i32, String, _, _>(
        |n| *n > 0,                     // predicate
        |n| format!("positive: {}", n), // transform
    );

    let mut string_sub = duplex.output.subscribe::<String>();
    let mut int_sub = duplex.output.subscribe::<i32>();

    // Positive - transformed to String
    duplex.input.send(42i32).await.unwrap();

    let transformed = tokio::time::timeout(Duration::from_millis(100), string_sub.recv())
        .await
        .unwrap();
    assert_eq!(transformed.as_deref(), Some(&"positive: 42".to_string()));

    // Negative - passed through as i32
    duplex.input.send(-5i32).await.unwrap();

    let passthrough = tokio::time::timeout(Duration::from_millis(100), int_sub.recv())
        .await
        .unwrap();
    assert_eq!(passthrough.as_deref(), Some(&-5i32));
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_filter_transform_other_types_passthrough() {
    // Filter-transform on i32, String should pass through
    let duplex = Duplex::new().filter_transform::<i32, String, _, _>(
        |n| *n > 0,
        |n| format!("num: {}", n),
    );

    let mut string_sub = duplex.output.subscribe::<String>();

    // String type passes through unchanged (not the filter type)
    duplex
        .input
        .send("hello".to_string())
        .await
        .unwrap();

    let passthrough = tokio::time::timeout(Duration::from_millis(100), string_sub.recv())
        .await
        .unwrap();
    assert_eq!(passthrough.as_deref(), Some(&"hello".to_string()));
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_passthrough_except() {
    // Passthrough all except String
    let duplex = Duplex::new().passthrough_except::<String>();

    let mut int_sub = duplex.output.subscribe::<i32>();
    let mut string_sub = duplex.output.subscribe::<String>();

    // i32 passes through
    duplex.input.send(42i32).await.unwrap();

    let num = tokio::time::timeout(Duration::from_millis(100), int_sub.recv())
        .await
        .unwrap();
    assert_eq!(num.as_deref(), Some(&42i32));

    // String is consumed (not passed through)
    duplex.input.send("hello".to_string()).await.unwrap();

    let result = tokio::time::timeout(Duration::from_millis(50), string_sub.recv()).await;
    assert!(
        result.is_err(),
        "String should be consumed, not passed through"
    );
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_passthrough_all() {
    // Passthrough all types
    let duplex = Duplex::new().passthrough_all();

    let mut int_sub = duplex.output.subscribe::<i32>();
    let mut string_sub = duplex.output.subscribe::<String>();

    duplex.input.send(42i32).await.unwrap();
    duplex.input.send("hello".to_string()).await.unwrap();

    let num = tokio::time::timeout(Duration::from_millis(100), int_sub.recv())
        .await
        .unwrap();
    assert_eq!(num.as_deref(), Some(&42i32));

    let text = tokio::time::timeout(Duration::from_millis(100), string_sub.recv())
        .await
        .unwrap();
    assert_eq!(text.as_deref(), Some(&"hello".to_string()));
}

*/

/* REMOVED: Duplex API no longer exists

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_no_echo_with_transform() {
    // Verify duplex with transform still prevents echo
    // Duplex has separate readable/writable - no echo by design
    let duplex = Duplex::new().transform::<i32, String, _>(|n| format!("{}", n));

    // Subscribe to readable side
    let mut readable_sub = duplex.output.subscribe::<String>();

    // Send i32 to writable - should transform to String on readable
    duplex.input.send(42i32).await.unwrap();

    // Transformed String should appear on readable
    let result = tokio::time::timeout(Duration::from_millis(100), readable_sub.recv()).await;
    assert!(result.is_ok(), "Transformed output should appear on readable");
    assert_eq!(result.unwrap().as_deref(), Some(&"42".to_string()));

    // But sending String directly to writable should NOT appear back on writable
    // (This is already tested by test_duplex_no_echo, but we verify the combination)
}

*/

/* REMOVED: Duplex API no longer exists
#[tokio::test]
async fn test_duplex_multiple_transforms_same_type() {
    // Multiple transforms that consume the same type - first wins
    let duplex = Duplex::new()
        .transform::<i32, String, _>(|n| format!("first: {}", n))
        .transform::<i32, String, _>(|n| format!("second: {}", n));

    let mut sub = duplex.output.subscribe::<String>();

    duplex.input.send(42i32).await.unwrap();

    // First transform should process the i32
    let msg1 = tokio::time::timeout(Duration::from_millis(100), sub.recv())
        .await
        .unwrap();

    // Should get one transformed string (from first transform)
    // The second transform won't see it because it's consumed by the first
    assert!(msg1.is_some());
    let value = msg1.unwrap();
    assert!(
        value.contains("42"),
        "Should contain the transformed number"
    );
}

*/