lightstreamer-rs 1.0.0

A Rust client for the Lightstreamer TLCP protocol, for real-time data streaming.
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
//! The client: connect, subscribe, and get out of the way.
//!
//! This is the whole public surface of the crate's behaviour. Everything below
//! it — the session state machine, the wire parser, the transports — is
//! private, because it is not something a caller should have to reason about
//! to use a protocol client.
//!
//! # The shape of a program
//!
//! ```no_run
//! use futures_util::StreamExt;
//! use lightstreamer_rs::{
//!     AdapterSet, Client, ClientConfig, FieldSchema, ItemGroup, ServerAddress,
//!     Subscription, SubscriptionEvent, SubscriptionMode,
//! };
//!
//! # async fn run() -> lightstreamer_rs::Result<()> {
//! let config = ClientConfig::builder(ServerAddress::try_new("https://push.lightstreamer.com")?)
//!     .with_adapter_set(AdapterSet::try_new("DEMO")?)
//!     .build()?;
//!
//! // Not interested in session events: `drop` opts out. Binding them to
//! //`_session_events` would keep the stream alive and unread, which stalls
//! // the client once it fills — see `SessionEvents`.
//! let (client, session_events) = Client::connect(config).await?;
//! drop(session_events);
//!
//! let mut updates = client
//!     .subscribe(Subscription::new(
//!         SubscriptionMode::Merge,
//!         ItemGroup::from_items(["item1"])?,
//!         FieldSchema::from_fields(["last_price"])?,
//!     ))
//!     .await?;
//!
//! while let Some(event) = updates.next().await {
//!     if let SubscriptionEvent::Update(update) = event {
//!         println!("{}: {:?}", update.item_name(), update.changed_fields());
//!     }
//! }
//! # Ok(())
//! # }
//! ```

mod events;
mod message;
mod router;
mod subscription;
mod updates;

pub use events::{
    ClosedReason, Connected, Continuity, DisconnectReason, Recovery, RecoveryOutcome, Resubscribed,
    ServerInfo, SessionEvent, SessionEvents, StateValidity, SubscriptionId,
};
pub use message::{Message, MessageOutcome, MessageResult, SequenceName};
pub use subscription::{
    BufferSize, FieldSchema, FrequencyLimit, ItemGroup, MaxFrequency, Snapshot, Subscription,
    SubscriptionMode,
};
pub use updates::{CommandFields, Filtering, SubscriptionEvent, UpdateFrequency, Updates};

use tokio::sync::{mpsc, oneshot};

use crate::config::ClientConfig;
use crate::error::{Error, Result};
use crate::session::{self, SessionHandle};

use self::events::SessionEvents as SessionEventStream;
use self::router::{Router, RouterCommand};

/// Hands out one identity per [`Client`] built in this process.
///
/// Starts above [`ClientId::DETACHED`] so that no real client can ever share
/// an identity with a hand-made [`crate::test_util`] handle.
static NEXT_CLIENT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

/// Which [`Client`] a [`SubscriptionId`] came from.
///
/// Not exposed: a caller has no use for it, and its whole job is to make a
/// handle from one client unusable on another. See [`SubscriptionId`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct ClientId(u64);

impl ClientId {
    /// The identity of a handle that belongs to no client, which
    /// [`crate::test_util`] mints. It matches no real client, so such a handle
    /// can name a subscription in an assertion but never cancel one.
    #[cfg(feature = "test-util")]
    pub(crate) const DETACHED: Self = Self(0);

    /// The next unused identity.
    ///
    /// Fails rather than wrapping. Reaching this needs 2⁶⁴ clients in one
    /// process, but a wrapped identity would silently make two clients'
    /// handles interchangeable again, which is the whole thing being
    /// prevented.
    fn allocate() -> Result<Self> {
        use std::sync::atomic::Ordering::Relaxed;
        NEXT_CLIENT_ID
            .fetch_update(Relaxed, Relaxed, |current| current.checked_add(1))
            .map(Self)
            .map_err(|_| Error::Internal {
                reason: "this process has run out of client identities".to_owned(),
            })
    }

    /// The identity as a number, for `Display` and for logs.
    pub(crate) const fn get(self) -> u64 {
        self.0
    }
}

/// The channel sizes and budgets a client is built with.
#[derive(Debug, Clone, Copy)]
struct Capacities {
    /// Events buffered per subscription stream.
    update: std::num::NonZeroUsize,
    /// Events buffered on the session stream.
    session_event: std::num::NonZeroUsize,
    /// How long [`Client::disconnect`] waits before forcing a stop.
    shutdown_timeout: std::time::Duration,
}

/// Holds one session event produced before the caller could hold the stream.
///
/// The buffer is capped at the capacity the caller configured, which is the
/// budget it already said it wanted for session events. Past that, further
/// pre-connection events are **discarded with a warning**: they are session
/// events for a session that has not bound yet, whose outcome
/// [`Client::connect`] reports as its own return value, and blocking instead
/// would deadlock the bind against a stream nobody can yet read.
fn stage(staged: &mut std::collections::VecDeque<SessionEvent>, event: SessionEvent, limit: usize) {
    if staged.len() < limit {
        staged.push_back(event);
        return;
    }
    tracing::warn!(
        limit,
        "discarding a session event produced before connect() returned"
    );
}

/// A live Lightstreamer session.
///
/// Get one from [`Client::connect`]. It is a handle, not the session itself:
/// the work happens on background tasks, and the client is what you use to
/// steer them.
///
/// # Shutdown is part of the contract
///
/// **Dropping the client shuts the session down.** The background tasks stop,
/// the socket is closed, and every stream you are still holding ends. You
/// never need `std::process::exit` to stop a `lightstreamer-rs` client, and
/// you never need to remember a deregistration step.
///
/// Dropping does not tell the *server* the session is over: it just goes
/// quiet, and the server keeps the session buffering until its own timeout
/// expires [`docs/spec/02-session-lifecycle.md` §6.3]. Call
/// [`Client::disconnect`] when you want the server to know at once — for
/// instance because the same user is limited to a fixed number of sessions.
///
/// # Cloning
///
/// The client is **not** `Clone`, deliberately: its identity is what decides
/// when the session ends, and a clone would make "dropping the client shuts
/// the session down" untrue in a way that is hard to see at the call site. To
/// use it from several tasks, put it in an [`std::sync::Arc`] — the session
/// then ends when the last reference goes, which is the same rule stated once.
#[derive(Debug)]
pub struct Client {
    /// This client's identity, carried by every [`SubscriptionId`] it hands
    /// out so that another client's handle cannot be spent here.
    id: ClientId,
    handle: SessionHandle,
    router: mpsc::UnboundedSender<RouterCommand>,
    update_capacity: std::num::NonZeroUsize,
    /// How long [`Client::disconnect`] waits for the ordered shutdown before
    /// forcing one. Taken from
    /// [`ConnectionOptions::with_open_timeout`](crate::ConnectionOptions::with_open_timeout),
    /// which is already this crate's answer to "how long may one server round
    /// trip take"; a destroy is exactly one.
    shutdown_timeout: std::time::Duration,
    /// The background tasks, so that [`Client::disconnect`] can wait for them
    /// instead of merely asking them to stop.
    tasks: Vec<tokio::task::JoinHandle<()>>,
    /// Whether dropping this client should order an immediate stop.
    ///
    /// Always true except while [`Client::disconnect`] is running its ordered
    /// shutdown, which needs the session to survive long enough to tell the
    /// server.
    stop_on_drop: bool,
    /// Held only to be dropped. Closing it is how the background tasks learn
    /// the client is gone, which is what makes "dropping the client shuts the
    /// session down" true.
    _alive: mpsc::Sender<()>,
}

impl Drop for Client {
    /// Shuts the session down.
    ///
    /// Closing the channels the background tasks watch is not enough on its
    /// own: a task blocked delivering into a stream the caller stopped reading
    /// is not watching anything. The explicit stop is what makes "dropping the
    /// client shuts the session down" true even then — it is exempt from
    /// backpressure by construction, and whatever was still undelivered goes
    /// with the streams it was going to.
    fn drop(&mut self) {
        if self.stop_on_drop {
            self.handle.stop();
        }
    }
}

impl Client {
    /// Opens a session and waits until it is streaming.
    ///
    /// Returns the client and the session's event stream. The stream is handed
    /// over here rather than fetched later so that ignoring it is a decision
    /// you write down — `drop(events)` — instead of an omission that quietly
    /// costs you the recovery notifications
    /// (`docs/adr/0005-recovery-is-visible-in-the-event-stream.md`).
    ///
    /// This resolves when the server has confirmed the session, so a
    /// [`Client`] you hold is one that was streaming at least once. It does
    /// **not** mean the connection is still up now: reconnection happens
    /// underneath, and the session event stream is where you learn about it.
    ///
    /// # Errors
    ///
    /// - [`Error::Transport`] if the connection could not be established at
    ///   all, or the address could not be turned into an endpoint.
    /// - [`Error::Session`] if the server refused the session with a code that
    ///   admits no retry — bad credentials (Appendix A code 1) and an unknown
    ///   Adapter Set (code 2) are the usual ones.
    /// - [`Error::ReconnectExhausted`] if every attempt allowed by
    ///   [`RetryPolicy`](crate::RetryPolicy) failed. Codes the specification
    ///   marks temporary are retried before this is reported, and the last
    ///   failure is carried in the error rather than reduced to a count.
    /// - [`Error::Internal`] if this crate could not build a request it should
    ///   always be able to build.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use lightstreamer_rs::{AdapterSet, Client, ClientConfig, ServerAddress};
    ///
    /// # async fn run() -> lightstreamer_rs::Result<()> {
    /// let config = ClientConfig::builder(ServerAddress::try_new("https://push.lightstreamer.com")?)
    ///     .with_adapter_set(AdapterSet::try_new("DEMO")?)
    ///     .build()?;
    ///
    /// let (client, events) = Client::connect(config).await?;
    /// # drop((client, events));
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(skip(config), fields(address = %config.address(), transport = config.transport().as_str()))]
    pub async fn connect(config: ClientConfig) -> Result<(Self, SessionEvents)> {
        let capacities = Capacities {
            update: config.options().update_capacity(),
            session_event: config.options().session_event_capacity(),
            shutdown_timeout: config.options().open_timeout(),
        };

        // Which transport carries the session, and how it is built, is the
        // session layer's composition root — not this one. The façade names no
        // adapter, so a second transport is added below without touching the
        // semver-governed surface
        // [`docs/adr/0007-transport-port-shape.md` §3].
        let parts = session::connect_configured(config)?;
        Self::assemble(parts, capacities).await
    }

    /// Wires the driver, the router and the unsubscriber together and waits
    /// for the first bind.
    ///
    /// Generic over the transport so that the whole client — not merely the
    /// session layer — can be exercised over a scripted one, with no socket and
    /// no real timer [`docs/adr/0007-transport-port-shape.md`]. It takes the
    /// session already composed, because choosing and building a transport is
    /// not this layer's business.
    async fn assemble<T>(
        parts: (
            session::SessionDriver<T>,
            SessionHandle,
            mpsc::Receiver<session::SessionEvent>,
        ),
        capacities: Capacities,
    ) -> Result<(Self, SessionEvents)>
    where
        T: session::Transport + Send + 'static,
    {
        let Capacities {
            update: update_capacity,
            session_event: session_capacity,
            shutdown_timeout,
        } = capacities;
        let id = ClientId::allocate()?;
        let (driver, handle, session_events) = parts;

        let (router_tx, router_rx) = mpsc::unbounded_channel();
        let (unsubscribe_tx, unsubscribe_rx) = mpsc::unbounded_channel();
        let (session_out_tx, mut session_out_rx) = mpsc::channel(session_capacity.get());
        let (alive_tx, alive_rx) = mpsc::channel(1);
        let (ready_tx, ready_rx) = oneshot::channel();

        let router = Router::new(
            id,
            session_events,
            router_rx,
            session_out_tx,
            unsubscribe_tx,
            ready_tx,
            handle.stop_signal(),
        );

        let tasks = vec![
            tokio::spawn(async move {
                let closed = driver.run().await;
                tracing::debug!(?closed, "session driver stopped");
            }),
            tokio::spawn(router.run()),
            tokio::spawn(router::issue_unsubscriptions(
                handle.clone(),
                unsubscribe_rx,
                alive_rx,
                handle.stop_signal(),
            )),
        ];

        // The router fires readiness once, on the first bind or the first
        // terminal failure. Waiting for it *while draining* the event channel
        // is not an optimisation: nobody holds the event stream yet, so a
        // startup that retries can fill the channel, and a router blocked on a
        // full channel never processes the bind that readiness is waiting for.
        // Draining here is what stops `connect` waiting on itself.
        let mut staged = std::collections::VecDeque::new();
        tokio::pin!(ready_rx);
        let outcome = loop {
            tokio::select! {
                biased;
                result = &mut ready_rx => break result,
                event = session_out_rx.recv() => match event {
                    Some(event) => stage(&mut staged, event, session_capacity.get()),
                    // The router stopped without saying anything, which can
                    // only happen if everything was torn down first.
                    None => break Ok(Err(Error::Disconnected)),
                },
            }
        };
        let connected = match outcome {
            Ok(Ok(connected)) => connected,
            Ok(Err(error)) => return Err(error),
            Err(_) => return Err(Error::Disconnected),
        };
        // Deliberately without the session identifier. `LS_session` is what a
        // control request or a rebind names the session with
        // [`docs/spec/03-requests.md` §5.1], so it is a bearer value, and this
        // crate does not put one in a log line on a caller's behalf. It is on
        // `Connected` for a caller who wants to.
        tracing::info!(
            client = id.get(),
            keepalive = ?connected.keepalive,
            request_limit_bytes = connected.request_limit_bytes,
            "session established"
        );

        Ok((
            Self {
                id,
                handle,
                router: router_tx,
                update_capacity,
                shutdown_timeout,
                tasks,
                stop_on_drop: true,
                _alive: alive_tx,
            },
            SessionEventStream::with_staged(staged, session_out_rx),
        ))
    }

    /// Subscribes to a set of items and returns the stream of what happens to
    /// them.
    ///
    /// The subscription is requested immediately and re-created automatically
    /// on any session that replaces a lost one, so the stream you get back
    /// survives a reconnection without any action from you. Whether the server
    /// accepted it is reported *on the stream*, as
    /// [`SubscriptionEvent::Activated`] or [`SubscriptionEvent::Rejected`] —
    /// this call does not wait for that round trip, because a caller usually
    /// wants to subscribe to several things and then start reading.
    ///
    /// # Errors
    ///
    /// - [`Error::Config`] if the subscription's options and its mode cannot
    ///   go together — see [`Subscription::validate`], which this calls before
    ///   anything is sent. Checking here rather than at encoding time is what
    ///   makes an impossible subscription a mistake you see at the call site
    ///   instead of a stream that quietly never activates.
    /// - [`Error::Disconnected`] if the session has already stopped.
    /// - [`Error::Internal`] if this client has run out of the identifiers it
    ///   names subscriptions by — which takes 2⁶⁴ of them, and is reported
    ///   rather than wrapped around because a reused identifier would silently
    ///   conflate two subscriptions.
    ///
    /// Everything the *server* has to say about the subscription arrives on
    /// the returned stream instead, where it can be interleaved correctly with
    /// the data.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use futures_util::StreamExt;
    /// use lightstreamer_rs::{
    ///     Client, FieldSchema, ItemGroup, Snapshot, Subscription, SubscriptionEvent,
    ///     SubscriptionMode,
    /// };
    ///
    /// # async fn run(client: &Client) -> lightstreamer_rs::Result<()> {
    /// let mut updates = client
    ///     .subscribe(
    ///         Subscription::new(
    ///             SubscriptionMode::Merge,
    ///             ItemGroup::from_items(["item1", "item2"])?,
    ///             FieldSchema::from_fields(["last_price", "time"])?,
    ///         )
    ///         .with_snapshot(Snapshot::On),
    ///     )
    ///     .await?;
    ///
    /// while let Some(event) = updates.next().await {
    ///     match event {
    ///         SubscriptionEvent::Update(update) => {
    ///             println!("{}: {:?}", update.item_name(), update.changed_fields());
    ///         }
    ///         SubscriptionEvent::Rejected(error) => {
    ///             eprintln!("the server refused it: {error}");
    ///             break;
    ///         }
    ///         _ => {}
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(skip(self, subscription), fields(mode = subscription.mode().as_str()))]
    pub async fn subscribe(&self, subscription: Subscription) -> Result<Updates> {
        // Before anything is allocated or registered: a combination the mode
        // does not admit is the caller's mistake, and it is one here rather
        // than a `Deferred` on a stream that never activates.
        subscription.validate()?;

        let (events_tx, events_rx) = mpsc::channel(self.update_capacity.get());
        // The key is allocated, and everything it names registered, *before*
        // the request that could produce a notification for it is queued. The
        // other order leaves a window in which an immediate `SUBOK`, an early
        // update, or a `REQERR` refusing the subscription outright arrives with
        // nowhere to go [`docs/spec/03-requests.md` §13] — and a refusal that
        // lands in that window would leave the caller holding a stream that
        // never receives its own terminal event.
        //
        // The router's channel is unbounded, so this cannot block and cannot
        // fail while the router lives, and the router takes its commands ahead
        // of session events.
        let key = self
            .handle
            .allocate_key()
            .map_err(|error| Error::Internal {
                reason: error.to_string(),
            })?;
        let id = SubscriptionId::new(self.id, key);
        self.router
            .send(RouterCommand::Register {
                id,
                events: events_tx,
            })
            .map_err(|_| Error::Disconnected)?;

        if let Err(error) = self
            .handle
            .subscribe_with_key(key, subscription.into_spec())
            .await
        {
            // Nothing was subscribed, so nothing is unsubscribed: the
            // registration is simply undone.
            tracing::debug!(id = id.get(), %error, "the subscription could not be queued");
            let _ = self.router.send(RouterCommand::Unregister { id });
            return Err(Error::Disconnected);
        }

        tracing::debug!(id = id.get(), "subscribed");
        Ok(Updates::new(id, events_rx, self.router.clone()))
    }

    /// Unsubscribes, and waits for the request to be handed to the session.
    ///
    /// Usually unnecessary: dropping the [`Updates`] stream unsubscribes on
    /// its own. Use this when you want the unsubscription to be ordered with
    /// respect to your other calls, or when you want to keep reading the
    /// stream until [`SubscriptionEvent::Unsubscribed`] arrives — after which
    /// the server sends nothing more for those items
    /// [`docs/spec/04-notifications.md` §3.4].
    ///
    /// Updates may still arrive between this call and that event; that is the
    /// protocol's behaviour, not a race in this crate.
    ///
    /// # Errors
    ///
    /// - [`Error::ForeignSubscription`] if the handle was created by a
    ///   different client. Nothing is sent: every client numbers its
    ///   subscriptions from one, so the same number names different data on
    ///   each of them, and acting on it would cancel somebody else's.
    /// - [`Error::Disconnected`] if the session has already stopped, in which
    ///   case the subscription is gone regardless.
    pub async fn unsubscribe(&self, id: SubscriptionId) -> Result<()> {
        if id.client() != self.id {
            return Err(Error::ForeignSubscription { id: id.get() });
        }
        self.handle
            .unsubscribe(id.key())
            .await
            .map_err(|_| Error::Disconnected)
    }

    /// Sends a message to the server's Metadata Adapter
    /// [`docs/spec/03-requests.md` §12].
    ///
    /// `Ok(())` means the message was **queued for the session**, not that it
    /// reached the server. What actually happened to it is reported on the
    /// session event stream as [`SessionEvent::Message`] — not here, because
    /// the round trip runs through the Metadata Adapter and may take as long
    /// as the application behind it does.
    ///
    /// The guarantee is one report per message: **every message that asked for
    /// an outcome produces exactly one [`SessionEvent::Message`]**, whether it
    /// was processed, rejected by the adapter, refused by the server, or never
    /// sent at all. A [`Message::fire_and_forget`] one produces a report only
    /// in the last two cases, since it declined the notification the other two
    /// would have arrived on.
    ///
    /// Note that the server acknowledges *acceptance* and *processing*
    /// separately: acceptance is internal to this crate, and
    /// [`MessageResult::Delivered`] is the one that means the Metadata Adapter
    /// actually ran [`docs/spec/04-notifications.md` §4.1].
    ///
    /// # Messages are never resent
    ///
    /// If the session is replaced while a message is in flight, this crate
    /// reports [`MessageResult::NotSent`] rather than sending it again. The
    /// server deduplicates messages by their progressive number *within a
    /// session*, so replaying one across a replacement would restart the
    /// numbering it deduplicates against. Whether to retry is a decision only
    /// the caller can make correctly.
    ///
    /// # Errors
    ///
    /// - [`Error::Config`] if the message's parts cannot be sent together —
    ///   see [`Message::validate`], which this calls first.
    /// - [`Error::Disconnected`] if the session has already stopped.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::num::NonZeroU32;
    /// use lightstreamer_rs::{Client, Message, SequenceName};
    ///
    /// # async fn run(client: &Client) -> lightstreamer_rs::Result<()> {
    /// client
    ///     .send_message(
    ///         Message::new("BUY 100", NonZeroU32::MIN)
    ///             .in_sequence(SequenceName::try_new("ORDERS")?),
    ///     )
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(skip(self, message))]
    pub async fn send_message(&self, message: Message) -> Result<()> {
        // The text is the caller's and may be anything, so nothing about the
        // message is logged; what is checked here is only its shape.
        message.validate()?;
        self.handle
            .send_message(message.into_outgoing())
            .await
            .map_err(|_| Error::Disconnected)
    }

    /// Ends the session, telling the server so, and waits until it is over.
    ///
    /// The server closes the session at once instead of holding it open until
    /// its own timeout [`docs/spec/02-session-lifecycle.md` §6.3]. When this
    /// returns, the `destroy` has been written, the socket has been closed, and
    /// every background task this client owns has finished — so a caller may
    /// shut its runtime down immediately afterwards without cutting the
    /// shutdown short. Every stream this client handed out has ended, the last
    /// session event being a [`SessionEvent::Closed`].
    ///
    /// Dropping the client instead ends the session *without* telling the
    /// server, and without waiting. Prefer this call when the session limit per
    /// user matters, or when you want a clean line in the server's log.
    ///
    /// # The wait is bounded
    ///
    /// A server that never answers, or a stream the caller stopped reading,
    /// cannot make this hang: after
    /// [`ConnectionOptions::with_open_timeout`](crate::ConnectionOptions::with_open_timeout)
    /// — the same budget one server round trip is given anywhere else — the
    /// shutdown stops being polite and the tasks are stopped regardless. The
    /// socket is closed on that path too.
    ///
    /// # Errors
    ///
    /// [`Error::Disconnected`] if the session had already stopped — which is
    /// the outcome you asked for, so it is usually safe to ignore.
    /// [`Error::Internal`] if the session did not finish within the budget
    /// above; it has been stopped anyway, and nothing is left running.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn run(client: lightstreamer_rs::Client) {
    /// // An already-stopped session is not a failure worth propagating here.
    /// let _ = client.disconnect().await;
    /// # }
    /// ```
    #[tracing::instrument(skip(self))]
    pub async fn disconnect(mut self) -> Result<()> {
        // From here on, dropping this value must not cut the session short:
        // the point of the call is to let it finish saying goodbye.
        self.stop_on_drop = false;
        let budget = self.shutdown_timeout;
        let mut tasks = std::mem::take(&mut self.tasks);
        let handle = self.handle.clone();

        // Asking is itself bounded. The command channel is bounded, and a
        // driver blocked delivering into a stream nobody is reading must not
        // be able to swallow the request to end it.
        let asked = match tokio::time::timeout(budget, self.handle.destroy(None)).await {
            Ok(Ok(())) => Ok(()),
            Ok(Err(_)) => Err(Error::Disconnected),
            Err(_) => Err(Error::Internal {
                reason: "the session did not accept the destroy request in time".to_owned(),
            }),
        };

        // Everything that keeps the background tasks alive goes now: the
        // router's command channel, the liveness token, and this client's own
        // session handle. What is left is the ordered shutdown itself.
        drop(self);

        let mut outcome = asked;
        if tokio::time::timeout(budget, join_all(&mut tasks))
            .await
            .is_err()
        {
            tracing::warn!(
                ?budget,
                "the session did not shut down in time; stopping it"
            );
            // Exempt from backpressure by construction: this is what a stream
            // the caller stopped reading cannot hold up.
            handle.stop();
            if tokio::time::timeout(budget, join_all(&mut tasks))
                .await
                .is_err()
            {
                for task in &tasks {
                    task.abort();
                }
                join_all(&mut tasks).await;
            }
            if outcome.is_ok() {
                outcome = Err(Error::Internal {
                    reason: "the session did not shut down within the configured budget".to_owned(),
                });
            }
        }
        drop(handle);
        outcome
    }
}

/// Waits for every task, keeping the ones not yet finished.
///
/// Written to be cancel-safe: a handle is removed only once it has resolved,
/// so a caller that times this out still owns whatever is still running and can
/// escalate rather than detach it.
async fn join_all(tasks: &mut Vec<tokio::task::JoinHandle<()>>) {
    while let Some(task) = tasks.last_mut() {
        if let Err(error) = task.await
            && !error.is_cancelled()
        {
            tracing::warn!(%error, "a client task did not finish cleanly");
        }
        tasks.pop();
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use futures_util::{FutureExt, StreamExt};
    use tokio::sync::{mpsc, oneshot};

    use super::*;
    use crate::client::events::Connected;
    use crate::client::router::Router;
    use crate::session::{
        BindKind, BoundInfo, SessionClosed as WireClosed, SessionEvent as WireEvent, SessionId,
        SubscriptionError, SubscriptionEvent as WireSubscriptionEvent,
    };
    use std::time::Duration;

    /// A fresh client identity, as [`Client::assemble`] would allocate one.
    fn a_client_id() -> ClientId {
        match ClientId::allocate() {
            Ok(id) => id,
            Err(error) => panic!("this process has client identities left: {error}"),
        }
    }

    /// Everything a router test needs, wired but with no session behind it.
    struct Harness {
        /// The identity the router stamps its handles with.
        client: ClientId,
        /// Pretends to be the session driver.
        session_tx: mpsc::Sender<WireEvent>,
        commands: mpsc::UnboundedSender<RouterCommand>,
        session_events: SessionEvents,
        ready: oneshot::Receiver<Result<Box<Connected>>>,
        unsubscribed: mpsc::UnboundedReceiver<crate::session::SubscriptionKey>,
        /// Raises the stop lane, as a client being torn down would.
        stop: tokio::sync::watch::Sender<bool>,
    }

    fn harness(session_capacity: usize) -> Harness {
        let (session_tx, session_rx) = mpsc::channel(16);
        let (commands_tx, commands_rx) = mpsc::unbounded_channel();
        let (out_tx, out_rx) = mpsc::channel(session_capacity);
        let (unsub_tx, unsub_rx) = mpsc::unbounded_channel();
        let (ready_tx, ready_rx) = oneshot::channel();
        let (stop_tx, stop_rx) = tokio::sync::watch::channel(false);

        let client = a_client_id();
        let router = Router::new(
            client,
            session_rx,
            commands_rx,
            out_tx,
            unsub_tx,
            ready_tx,
            stop_rx,
        );
        tokio::spawn(router.run());

        Harness {
            client,
            session_tx,
            commands: commands_tx,
            session_events: SessionEvents::with_staged(std::collections::VecDeque::new(), out_rx),
            ready: ready_rx,
            unsubscribed: unsub_rx,
            stop: stop_tx,
        }
    }

    fn bound(kind: BindKind) -> WireEvent {
        WireEvent::Bound(Box::new(BoundInfo {
            session_id: SessionId::new("S1"),
            kind,
            keep_alive: Duration::from_secs(5),
            request_limit_bytes: 50_000,
            control_link: None,
        }))
    }

    #[tokio::test]
    async fn test_router_reports_the_first_bind_to_connect() {
        let harness = harness(8);
        assert!(
            harness
                .session_tx
                .send(bound(BindKind::Created))
                .await
                .is_ok()
        );

        match harness.ready.await {
            Ok(Ok(connected)) => {
                assert_eq!(connected.session_id, "S1");
                assert!(matches!(connected.continuity, Continuity::New));
            }
            other => panic!("expected a successful bind, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_router_reports_a_terminal_failure_to_connect() {
        let harness = harness(8);
        let closed = WireClosed::ByServer {
            cause: crate::session::ServerCause {
                code: 1,
                message: "User/password check failed".to_owned(),
            },
        };
        assert!(
            harness
                .session_tx
                .send(WireEvent::Closed(closed))
                .await
                .is_ok()
        );

        match harness.ready.await {
            Ok(Err(Error::Session(cause))) => assert_eq!(cause.code(), 1),
            other => panic!("expected a session refusal, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_session_events_distinguish_the_three_recovery_outcomes() {
        let harness = harness(8);

        // Continuity preserved by a rebind.
        assert!(
            harness
                .session_tx
                .send(bound(BindKind::Rebound))
                .await
                .is_ok()
        );
        // The session was replaced by a new one.
        assert!(
            harness
                .session_tx
                .send(bound(BindKind::Recreated {
                    previous: Some(SessionId::new("S0"))
                }))
                .await
                .is_ok()
        );
        // The session is definitively gone.
        assert!(
            harness
                .session_tx
                .send(WireEvent::Closed(WireClosed::RetriesExhausted {
                    attempts: 8,
                    last: None
                }))
                .await
                .is_ok()
        );

        let mut events = harness.session_events;
        match events.next().await {
            Some(SessionEvent::Connected(connected)) => {
                assert_eq!(connected.continuity.state_validity(), StateValidity::Valid);
                assert!(matches!(connected.continuity, Continuity::Preserved));
            }
            other => panic!("expected a preserved bind, got {other:?}"),
        }
        match events.next().await {
            Some(SessionEvent::Connected(connected)) => {
                assert_eq!(
                    connected.continuity.state_validity(),
                    StateValidity::Invalid
                );
            }
            other => panic!("expected a replaced bind, got {other:?}"),
        }
        match events.next().await {
            Some(SessionEvent::Closed(ClosedReason::ReconnectExhausted {
                attempts: 8, ..
            })) => {}
            other => panic!("expected a definitive loss, got {other:?}"),
        }
        // Terminal: the stream ends.
        assert!(events.next().await.is_none());
    }

    #[tokio::test]
    async fn test_dropping_the_session_event_stream_does_not_stall_the_router() {
        // Capacity of one, and nobody reading: without the drop the router
        // would block on the second event forever.
        let harness = harness(1);
        drop(harness.session_events);

        for _ in 0..16 {
            assert!(
                harness
                    .session_tx
                    .send(bound(BindKind::Rebound))
                    .await
                    .is_ok(),
                "the router stopped consuming session events"
            );
        }
    }

    #[tokio::test]
    async fn test_dropping_an_update_stream_asks_for_an_unsubscription() {
        let mut harness = harness(8);
        let id = SubscriptionId::new(harness.client, a_key().await);
        let (events_tx, events_rx) = mpsc::channel(4);
        assert!(
            harness
                .commands
                .send(RouterCommand::Register {
                    id,
                    events: events_tx,
                })
                .is_ok()
        );

        let updates = Updates::new(id, events_rx, harness.commands.clone());
        drop(updates);

        match harness.unsubscribed.recv().await {
            Some(key) => assert_eq!(key.get(), id.get()),
            None => panic!("dropping the stream did not unsubscribe"),
        }
    }

    #[tokio::test]
    async fn test_updates_are_bounded_and_block_rather_than_drop() {
        let harness = harness(8);
        let id = SubscriptionId::new(harness.client, a_key().await);
        // One slot only: the second update has nowhere to go until the caller
        // reads, and the router must wait rather than discard it.
        let (events_tx, mut events_rx) = mpsc::channel(1);
        assert!(
            harness
                .commands
                .send(RouterCommand::Register {
                    id,
                    events: events_tx,
                })
                .is_ok()
        );

        assert!(
            harness
                .session_tx
                .send(data(
                    id,
                    WireSubscriptionEvent::Activated {
                        item_count: 1,
                        field_count: 1,
                        command_fields: None,
                    }
                ))
                .await
                .is_ok()
        );
        for dropped_count in [1, 2, 3] {
            assert!(
                harness
                    .session_tx
                    .send(data(
                        id,
                        WireSubscriptionEvent::Overflow {
                            item_index: 1,
                            dropped_count,
                        }
                    ))
                    .await
                    .is_ok()
            );
        }

        // Nothing was dropped: activation, then all three events, in order.
        assert!(matches!(
            events_rx.recv().await,
            Some(SubscriptionEvent::Activated { field_count: 1, .. })
        ));
        for expected in [1, 2, 3] {
            match events_rx.recv().await {
                Some(SubscriptionEvent::Overflow { dropped_count, .. }) => {
                    assert_eq!(dropped_count, expected);
                }
                other => panic!("expected an overflow of {expected}, got {other:?}"),
            }
        }
    }

    #[tokio::test]
    async fn test_an_undecodable_notification_is_surfaced_not_swallowed() {
        let harness = harness(8);
        let id = SubscriptionId::new(harness.client, a_key().await);
        let (events_tx, mut events_rx) = mpsc::channel(4);
        assert!(
            harness
                .commands
                .send(RouterCommand::Register {
                    id,
                    events: events_tx,
                })
                .is_ok()
        );

        // An update before the subscription was ever activated: the session
        // had no schema to decode it against, and says so in a typed error.
        assert!(
            harness
                .session_tx
                .send(undecodable(
                    id,
                    SubscriptionError::NotActivated { tag: "U" }
                ))
                .await
                .is_ok()
        );

        assert!(matches!(
            events_rx.recv().await,
            Some(SubscriptionEvent::Undecodable { .. })
        ));
    }

    /// A refused control request, aimed at whatever it was acting on.
    fn refused(target: crate::session::ControlTarget, code: i64, message: &str) -> WireEvent {
        WireEvent::ControlResponse {
            request_id: None,
            target,
            outcome: crate::session::ControlOutcome::Rejected {
                cause: crate::session::ServerCause {
                    code,
                    message: message.to_owned(),
                },
            },
        }
    }

    #[tokio::test]
    async fn test_a_session_wide_rejection_reaches_the_session_stream() {
        let mut harness = harness(8);
        assert!(
            harness
                .session_tx
                .send(refused(
                    crate::session::ControlTarget::Session,
                    23,
                    "Bad Field schema name"
                ))
                .await
                .is_ok()
        );

        match harness.session_events.next().await {
            Some(SessionEvent::RequestRejected(error)) => {
                assert_eq!(error.code(), 23);
                assert_eq!(error.message(), "Bad Field schema name");
            }
            other => panic!("expected a rejection, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_a_request_that_never_left_is_not_dressed_as_a_server_error() {
        let mut harness = harness(8);
        assert!(
            harness
                .session_tx
                .send(WireEvent::ControlResponse {
                    request_id: None,
                    target: crate::session::ControlTarget::Session,
                    outcome: crate::session::ControlOutcome::NotSent {
                        reason: "the stream connection was down".to_owned(),
                    },
                })
                .await
                .is_ok()
        );

        // A fabricated code would be indistinguishable from one a Metadata
        // Adapter supplied, which this crate cannot honestly claim.
        match harness.session_events.next().await {
            Some(SessionEvent::RequestNotSent { reason }) => {
                assert_eq!(reason, "the stream connection was down");
            }
            other => panic!("expected a local failure, got {other:?}"),
        }
    }

    /// Registers a subscription stream and hands back its id and receiver.
    async fn register(
        harness: &Harness,
        capacity: usize,
    ) -> (SubscriptionId, mpsc::Receiver<SubscriptionEvent>) {
        let id = SubscriptionId::new(harness.client, a_key().await);
        let (events_tx, events_rx) = mpsc::channel(capacity);
        assert!(
            harness
                .commands
                .send(RouterCommand::Register {
                    id,
                    events: events_tx,
                })
                .is_ok()
        );
        (id, events_rx)
    }

    #[tokio::test]
    async fn test_a_refused_subscription_is_reported_on_its_own_stream() {
        // The silent-loss case: without the target the caller's stream would
        // simply go quiet forever.
        let harness = harness(8);
        let (id, mut events) = register(&harness, 4).await;

        assert!(
            harness
                .session_tx
                .send(refused(
                    crate::session::ControlTarget::Subscription {
                        key: id.key(),
                        operation: crate::session::SubscriptionOperation::Subscribe,
                    },
                    21,
                    "Bad Item Group name"
                ))
                .await
                .is_ok()
        );

        match events.recv().await {
            Some(SubscriptionEvent::Rejected(error)) => {
                assert_eq!(error.code(), 21);
                assert_eq!(error.message(), "Bad Item Group name");
            }
            other => panic!("expected a rejection on the subscription stream, got {other:?}"),
        }
        // Terminal: the subscription is gone and the stream ends.
        assert!(events.recv().await.is_none());
    }

    #[tokio::test]
    async fn test_an_unsent_subscription_request_is_not_terminal() {
        // The session layer releases the wire binding and re-issues the
        // subscription at the next bind, so ending the stream here would lose
        // a subscription that is still coming.
        let harness = harness(8);
        let (id, mut events) = register(&harness, 4).await;

        assert!(
            harness
                .session_tx
                .send(WireEvent::ControlResponse {
                    request_id: None,
                    target: crate::session::ControlTarget::Subscription {
                        key: id.key(),
                        operation: crate::session::SubscriptionOperation::Subscribe,
                    },
                    outcome: crate::session::ControlOutcome::NotSent {
                        reason: "stream connection lost".to_owned(),
                    },
                })
                .await
                .is_ok()
        );

        match events.recv().await {
            Some(SubscriptionEvent::Deferred { reason }) => {
                assert_eq!(reason, "stream connection lost");
            }
            other => panic!("expected a non-terminal deferral, got {other:?}"),
        }

        // The registration survived: the retry's `SUBOK` still lands here.
        assert!(
            harness
                .session_tx
                .send(data(
                    id,
                    WireSubscriptionEvent::Activated {
                        item_count: 1,
                        field_count: 1,
                        command_fields: None,
                    }
                ))
                .await
                .is_ok()
        );
        assert!(matches!(
            events.recv().await,
            Some(SubscriptionEvent::Activated { .. })
        ));
    }

    #[tokio::test]
    async fn test_an_accepted_control_request_produces_no_event() {
        let mut harness = harness(8);
        let (id, mut events) = register(&harness, 4).await;

        assert!(
            harness
                .session_tx
                .send(WireEvent::ControlResponse {
                    request_id: None,
                    target: crate::session::ControlTarget::Subscription {
                        key: id.key(),
                        operation: crate::session::SubscriptionOperation::Subscribe,
                    },
                    outcome: crate::session::ControlOutcome::Accepted,
                })
                .await
                .is_ok()
        );
        // An acceptance is an acknowledgement, not news: `SUBOK` is what says
        // the subscription is live, and that is what reaches the caller.
        assert!(
            harness
                .session_tx
                .send(data(
                    id,
                    WireSubscriptionEvent::Activated {
                        item_count: 1,
                        field_count: 1,
                        command_fields: None,
                    }
                ))
                .await
                .is_ok()
        );
        assert!(matches!(
            events.recv().await,
            Some(SubscriptionEvent::Activated { .. })
        ));
        assert!(harness.session_events.next().now_or_never().is_none());
    }

    #[tokio::test]
    async fn test_a_refused_unsubscription_leaves_the_subscription_alive() {
        // A refused `delete` means the subscription is still active, so
        // reporting it on that stream would read as "your subscription is
        // gone" — the opposite of the truth.
        let mut harness = harness(8);
        let (id, mut events) = register(&harness, 4).await;

        assert!(
            harness
                .session_tx
                .send(refused(
                    crate::session::ControlTarget::Subscription {
                        key: id.key(),
                        operation: crate::session::SubscriptionOperation::Unsubscribe,
                    },
                    19,
                    "Specified subscription not found"
                ))
                .await
                .is_ok()
        );

        match harness.session_events.next().await {
            Some(SessionEvent::RequestRejected(error)) => assert_eq!(error.code(), 19),
            other => panic!("expected a session-level rejection, got {other:?}"),
        }

        // The stream is still live: an update still reaches it.
        assert!(
            harness
                .session_tx
                .send(data(
                    id,
                    WireSubscriptionEvent::Activated {
                        item_count: 1,
                        field_count: 1,
                        command_fields: None,
                    }
                ))
                .await
                .is_ok()
        );
        assert!(matches!(
            events.recv().await,
            Some(SubscriptionEvent::Activated { .. })
        ));
    }

    #[tokio::test]
    async fn test_an_unparsable_line_is_surfaced_verbatim() {
        let mut harness = harness(8);
        assert!(
            harness
                .session_tx
                .send(WireEvent::Unparsed {
                    line: "FUTURE,1,2".to_owned(),
                    error: crate::session::ProtocolError::UnknownTag {
                        tag: "FUTURE".to_owned(),
                        line: "FUTURE,1,2".to_owned(),
                    },
                })
                .await
                .is_ok()
        );

        match harness.session_events.next().await {
            Some(SessionEvent::Unrecognized { line }) => assert_eq!(line, "FUTURE,1,2"),
            other => panic!("expected the raw line, got {other:?}"),
        }
    }

    /// One subscription event as the session layer would hand it up.
    ///
    /// The session interprets the wire line and routes the *meaning*; these
    /// tests are about the fan-out, so they start where the router does.
    fn data(id: SubscriptionId, event: WireSubscriptionEvent) -> WireEvent {
        WireEvent::Subscription {
            progressive: 1,
            key: id.key(),
            outcome: Box::new(Ok(event)),
        }
    }

    /// A line the session could not reconcile with this subscription's state.
    fn undecodable(id: SubscriptionId, error: SubscriptionError) -> WireEvent {
        WireEvent::Subscription {
            progressive: 1,
            key: id.key(),
            outcome: Box::new(Err(error)),
        }
    }

    /// A transport that connects to nothing and says nothing.
    ///
    /// The router tests need real [`SubscriptionKey`](crate::session::SubscriptionKey)
    /// values, which only the session layer can mint. A session is therefore
    /// created over this transport and its driver is never run: keys are
    /// allocated by `subscribe` before anything reaches the wire.
    #[derive(Debug)]
    struct NullTransport;

    impl session::Transport for NullTransport {
        fn properties(&self) -> session::TransportProperties {
            session::TransportProperties {
                control_shares_stream: true,
                ends_on_content_length: false,
                is_polling: false,
            }
        }

        fn set_control_link(&mut self, _host: Option<&str>) {}

        async fn open_stream(
            &mut self,
            _request: session::StreamOpen,
        ) -> std::result::Result<(), crate::error::TransportError> {
            Ok(())
        }

        async fn next_line(
            &mut self,
        ) -> Option<std::result::Result<String, crate::error::TransportError>> {
            std::future::pending().await
        }

        async fn send_control(
            &mut self,
            _request: session::EncodedRequest,
        ) -> std::result::Result<(), crate::error::TransportError> {
            Ok(())
        }

        async fn close(&mut self) -> std::result::Result<(), crate::error::TransportError> {
            Ok(())
        }
    }

    /// Mints `count` distinct subscription keys.
    async fn mint_keys(count: usize) -> Vec<crate::session::SubscriptionKey> {
        let options = crate::session::options::SessionOptions::default();
        let Ok((driver, handle, events)) = crate::session::connect(NullTransport, options) else {
            panic!("a default session configuration is valid");
        };
        let mut keys = Vec::with_capacity(count);
        for index in 0..count {
            let spec = crate::session::SubscriptionSpec::new(
                format!("item{index}"),
                "price",
                session::SubscriptionMode::Merge,
            );
            match handle.subscribe(spec).await {
                Ok(key) => keys.push(key),
                Err(error) => panic!("the driver's command channel is open: {error}"),
            }
        }
        // The driver is deliberately never run; dropping it closes nothing the
        // tests rely on.
        drop((driver, events));
        keys
    }

    /// One subscription key.
    async fn a_key() -> crate::session::SubscriptionKey {
        match mint_keys(1).await.into_iter().next() {
            Some(key) => key,
            None => panic!("one key was requested"),
        }
    }

    // -----------------------------------------------------------------------
    // Startup, shutdown and registration ordering
    // -----------------------------------------------------------------------

    /// A transport that replays lines and records what it was asked to do.
    ///
    /// The whole client is exercised over this: driver, router and
    /// unsubscriber, with no socket and no real timer
    /// [`docs/adr/0007-transport-port-shape.md`].
    #[derive(Debug, Clone, Default)]
    struct ScriptLog {
        controls: Vec<String>,
        closes: usize,
    }

    /// One thing a scripted connection does when asked for a line.
    #[derive(Debug, Clone, Copy)]
    enum Say {
        /// Yield this line.
        Line(&'static str),
        /// Yield nothing until this many control requests have been sent, so a
        /// script can answer what the client actually did.
        AwaitControls(usize),
    }

    #[derive(Debug)]
    struct ScriptedTransport {
        /// One script per stream connection; `None` for a connection that
        /// cannot be opened at all.
        scripts: std::collections::VecDeque<Option<Vec<Say>>>,
        current: std::collections::VecDeque<Say>,
        log: std::sync::Arc<std::sync::Mutex<ScriptLog>>,
    }

    impl ScriptedTransport {
        fn new(scripts: Vec<Option<Vec<Say>>>) -> Self {
            Self {
                scripts: scripts.into(),
                current: std::collections::VecDeque::new(),
                log: std::sync::Arc::new(std::sync::Mutex::new(ScriptLog::default())),
            }
        }

        fn log(&self) -> std::sync::Arc<std::sync::Mutex<ScriptLog>> {
            std::sync::Arc::clone(&self.log)
        }
    }

    impl session::Transport for ScriptedTransport {
        fn properties(&self) -> session::TransportProperties {
            session::TransportProperties {
                control_shares_stream: true,
                ends_on_content_length: false,
                is_polling: false,
            }
        }

        fn set_control_link(&mut self, _host: Option<&str>) {}

        async fn open_stream(
            &mut self,
            _request: session::StreamOpen,
        ) -> std::result::Result<(), crate::error::TransportError> {
            match self.scripts.pop_front() {
                Some(Some(script)) => {
                    self.current = script.into();
                    Ok(())
                }
                Some(None) => Err(crate::error::TransportError::ConnectionLost {
                    reason: "scripted failure".to_owned(),
                }),
                None => {
                    self.current.clear();
                    Ok(())
                }
            }
        }

        async fn next_line(
            &mut self,
        ) -> Option<std::result::Result<String, crate::error::TransportError>> {
            loop {
                match self.current.front().copied() {
                    Some(Say::Line(line)) => {
                        self.current.pop_front();
                        return Some(Ok(line.to_owned()));
                    }
                    Some(Say::AwaitControls(wanted)) => {
                        let sent = self.log.lock().map_or(0, |log| log.controls.len());
                        if sent >= wanted {
                            self.current.pop_front();
                            continue;
                        }
                        return std::future::pending().await;
                    }
                    None => return std::future::pending().await,
                }
            }
        }

        async fn send_control(
            &mut self,
            request: session::EncodedRequest,
        ) -> std::result::Result<(), crate::error::TransportError> {
            if let Ok(mut log) = self.log.lock() {
                log.controls.push(request.parameters);
            }
            Ok(())
        }

        async fn close(&mut self) -> std::result::Result<(), crate::error::TransportError> {
            if let Ok(mut log) = self.log.lock() {
                log.closes = log.closes.saturating_add(1);
            }
            Ok(())
        }
    }

    const CONOK: &str = "CONOK,S1,50000,5000,*";

    fn capacities(session_event: usize) -> Capacities {
        Capacities {
            update: std::num::NonZeroUsize::new(8).unwrap_or(std::num::NonZeroUsize::MIN),
            session_event: std::num::NonZeroUsize::new(session_event)
                .unwrap_or(std::num::NonZeroUsize::MIN),
            shutdown_timeout: Duration::from_secs(5),
        }
    }

    fn session_options() -> crate::session::options::SessionOptions {
        crate::session::options::SessionOptions::default().with_backoff(
            crate::session::backoff::BackoffPolicy {
                initial: Duration::from_millis(10),
                max: Duration::from_millis(40),
                max_attempts: std::num::NonZeroU32::new(6),
            },
        )
    }

    /// C-01. Readiness must never depend on the caller consuming a stream it
    /// cannot hold yet.
    #[tokio::test(start_paused = true)]
    async fn test_connect_completes_when_retries_fill_a_capacity_one_event_stream() {
        // Three failed attempts before the bind, with a single slot for the
        // session events they produce. Nobody can read that slot until
        // `connect` returns, so a router that blocked on it would never
        // deliver the bind that resolves this call.
        let transport = ScriptedTransport::new(vec![
            None,
            None,
            None,
            Some(vec![Say::Line(CONOK), Say::AwaitControls(usize::MAX)]),
        ]);

        let (client, mut events) = tokio::time::timeout(
            Duration::from_secs(30),
            Client::assemble(
                session::connect(transport, session_options()).expect("a valid session"),
                capacities(1),
            ),
        )
        .await
        .expect("connect must not wait on a stream the caller cannot hold yet")
        .expect("the fourth attempt bound");

        // One slot was configured, so one pre-connection event is retained —
        // the first, in the order it happened — and the rest are discarded by
        // the documented policy rather than blocking the bind.
        assert!(
            matches!(events.next().await, Some(SessionEvent::Disconnected { .. })),
            "the retained startup event comes first"
        );
        assert!(matches!(
            events.next().await,
            Some(SessionEvent::Connected(_))
        ));
        drop(client);
    }

    /// C-01, the retention half: with room for them, every startup event is
    /// kept, in the order it happened.
    #[tokio::test(start_paused = true)]
    async fn test_connect_retains_startup_events_in_order() {
        let transport = ScriptedTransport::new(vec![
            None,
            None,
            None,
            Some(vec![Say::Line(CONOK), Say::AwaitControls(usize::MAX)]),
        ]);

        let (client, events) = tokio::time::timeout(
            Duration::from_secs(30),
            Client::assemble(
                session::connect(transport, session_options()).expect("a valid session"),
                capacities(16),
            ),
        )
        .await
        .expect("connect must not wait on a stream the caller cannot hold yet")
        .expect("the fourth attempt bound");

        let seen: Vec<SessionEvent> = events.take(4).collect().await;
        assert!(
            seen.iter()
                .take(3)
                .all(|event| matches!(event, SessionEvent::Disconnected { .. })),
            "the three failed attempts come first, in order: {seen:?}"
        );
        assert!(
            matches!(seen.get(3), Some(SessionEvent::Connected(_))),
            "then the bind: {seen:?}"
        );
        drop(client);
    }

    /// C-01. The same, when the attempts run out instead of succeeding: the
    /// failure has to reach the caller rather than deadlock behind it.
    #[tokio::test(start_paused = true)]
    async fn test_connect_reports_exhaustion_with_a_capacity_one_event_stream() {
        let transport = ScriptedTransport::new(vec![None, None, None, None, None, None, None]);

        let outcome = tokio::time::timeout(
            Duration::from_secs(30),
            Client::assemble(
                session::connect(transport, session_options()).expect("a valid session"),
                capacities(1),
            ),
        )
        .await
        .expect("connect must not hang once the retries are spent");

        // A-04: exhaustion carries the last thing that went wrong. A count on
        // its own reads the same whether the host did not resolve or the
        // server refused the credentials.
        match outcome {
            Err(Error::ReconnectExhausted {
                last: Some(reason), ..
            }) => assert!(
                matches!(*reason, DisconnectReason::ConnectionFailed { .. }),
                "expected the scripted connection failure, got {reason:?}"
            ),
            other => panic!(
                "expected exhaustion with a cause, got {:?}",
                other.map(|_| ())
            ),
        }
    }

    /// C-09. `disconnect()` must mean the session is over, not that a request
    /// to end it was queued.
    #[tokio::test(start_paused = true)]
    async fn test_disconnect_waits_for_the_destroy_the_socket_and_the_tasks() {
        // The server answers the destroy — and only the destroy — with the
        // `END` that T7 promises
        // [`docs/spec/02-session-lifecycle.md` §2.2, T7].
        let transport = ScriptedTransport::new(vec![Some(vec![
            Say::Line(CONOK),
            Say::AwaitControls(1),
            Say::Line("END,31,session destroyed"),
        ])]);
        let log = transport.log();

        let (client, mut events) = Client::assemble(
            session::connect(transport, session_options()).expect("a valid session"),
            capacities(16),
        )
        .await
        .expect("the session bound");

        tokio::time::timeout(Duration::from_secs(10), client.disconnect())
            .await
            .expect("disconnect must not hang")
            .expect("the session was destroyed cleanly");

        let recorded = log.lock().expect("the log is not poisoned").clone();
        assert!(
            recorded
                .controls
                .iter()
                .any(|parameters| parameters.contains("LS_op=destroy")),
            "the destroy reached the wire: {:?}",
            recorded.controls
        );
        assert!(recorded.closes >= 1, "the transport was closed");
        // Every task has finished, so the streams they feed have ended.
        let remaining: Vec<SessionEvent> = std::iter::from_fn(|| events.next().now_or_never())
            .flatten()
            .collect();
        assert!(
            matches!(remaining.last(), Some(SessionEvent::Closed(_))),
            "the last event is terminal: {remaining:?}"
        );
    }

    /// C-02 and C-09 together: a caller that stopped reading must not be able
    /// to make `disconnect` hang.
    #[tokio::test(start_paused = true)]
    async fn test_disconnect_completes_even_with_an_unread_event_stream() {
        // Three session-level notifications and one slot to put them in. The
        // stream is held and never read, so the router wedges on the second and
        // stops taking anything from the driver — which is exactly the state in
        // which a shutdown used to be impossible.
        let transport = ScriptedTransport::new(vec![Some(vec![
            Say::Line(CONOK),
            Say::Line("SERVNAME,Lightstreamer HTTP Server"),
            Say::Line("CLIENTIP,127.0.0.1"),
            Say::Line("CONS,unlimited"),
        ])]);
        let log = transport.log();

        let (client, events) = Client::assemble(
            session::connect(transport, session_options()).expect("a valid session"),
            capacities(1),
        )
        .await
        .expect("the session bound");
        // Let the router reach the wedge before asking it to stop.
        tokio::time::sleep(Duration::from_millis(20)).await;

        let disconnected = tokio::time::timeout(Duration::from_secs(60), client.disconnect()).await;
        assert!(
            disconnected.is_ok(),
            "a stream nobody reads must not hold the shutdown open"
        );
        assert!(
            log.lock().expect("the log is not poisoned").closes >= 1,
            "the socket is closed on the forced path too"
        );
        drop(events);
    }

    /// C-04. A subscription's stream must be registered *before* the request
    /// that could produce an event for it is queued, and the registration must
    /// be undone if that request cannot be queued at all.
    ///
    /// The window is made observable by giving the session a single command
    /// slot, filling it, and never running the driver: the submission is then
    /// stuck, and the registration either already happened or never will.
    #[tokio::test(start_paused = true)]
    async fn test_subscribe_registers_the_stream_before_it_submits_the_request() {
        let options = crate::session::options::SessionOptions {
            command_capacity: std::num::NonZeroUsize::MIN,
            ..Default::default()
        };
        let Ok((driver, handle, session_events)) = crate::session::connect(NullTransport, options)
        else {
            panic!("a default session configuration is valid");
        };

        // Occupy the one command slot. The driver is deliberately never run,
        // so nothing will ever free it.
        let filler = handle
            .allocate_key()
            .expect("the key space is not exhausted");
        assert!(handle.unsubscribe(filler).await.is_ok());

        let (router_tx, mut router_rx) = mpsc::unbounded_channel();
        let (alive_tx, _alive_rx) = mpsc::channel(1);
        let client = Client {
            id: a_client_id(),
            handle: handle.clone(),
            router: router_tx,
            update_capacity: std::num::NonZeroUsize::new(8).unwrap_or(std::num::NonZeroUsize::MIN),
            shutdown_timeout: Duration::from_secs(5),
            tasks: Vec::new(),
            stop_on_drop: false,
            _alive: alive_tx,
        };

        let subscribing = tokio::spawn(async move {
            client
                .subscribe(Subscription::new(
                    SubscriptionMode::Merge,
                    ItemGroup::from_items(["item1"]).expect("a valid group"),
                    FieldSchema::from_fields(["price"]).expect("a valid schema"),
                ))
                .await
                .is_ok()
        });
        tokio::time::sleep(Duration::from_millis(20)).await;

        assert!(
            !subscribing.is_finished(),
            "the submission is blocked, which is the window under test"
        );
        assert!(
            matches!(router_rx.try_recv(), Ok(RouterCommand::Register { .. })),
            "the stream is registered before the request is submitted"
        );

        // Now make the submission fail outright: the registration must be
        // rolled back, and no unsubscription asked for, because nothing was
        // ever subscribed.
        drop((driver, session_events, handle));
        assert!(
            !tokio::time::timeout(Duration::from_secs(5), subscribing)
                .await
                .expect("the submission fails once the driver is gone")
                .expect("the task did not panic"),
            "a subscription that could not be submitted is an error"
        );
        assert!(
            matches!(router_rx.try_recv(), Ok(RouterCommand::Unregister { .. })),
            "the registration is rolled back, not left behind"
        );
    }

    /// A-07. Two clients number their subscriptions from one, so their handles
    /// carry the same number. Spending one on the other must fail with a typed
    /// error and must not reach the wire.
    #[tokio::test(start_paused = true)]
    async fn test_a_handle_from_one_client_cannot_unsubscribe_another() {
        async fn a_client() -> (Client, Updates, std::sync::Arc<std::sync::Mutex<ScriptLog>>) {
            let transport = ScriptedTransport::new(vec![Some(vec![
                Say::Line(CONOK),
                Say::AwaitControls(usize::MAX),
            ])]);
            let log = transport.log();
            let (client, events) = Client::assemble(
                session::connect(transport, session_options()).expect("a valid session"),
                capacities(16),
            )
            .await
            .expect("the session bound");
            drop(events);
            let updates = client
                .subscribe(Subscription::new(
                    SubscriptionMode::Merge,
                    ItemGroup::from_items(["item1"]).expect("a valid group"),
                    FieldSchema::from_fields(["price"]).expect("a valid schema"),
                ))
                .await
                .expect("the subscription was queued");
            (client, updates, log)
        }

        // The `Updates` streams are held, not dropped: dropping one
        // unsubscribes, which is the thing a cross-client unsubscribe must not
        // be confused with.
        let (first, first_updates, _) = a_client().await;
        let (second, second_updates, second_log) = a_client().await;
        let (first_id, second_id) = (first_updates.id(), second_updates.id());

        // The premise: the numbers really do collide.
        assert_eq!(
            first_id.get(),
            second_id.get(),
            "each client numbers its subscriptions from one"
        );
        assert_ne!(first_id, second_id, "the handles are still distinct");

        // The subscription each client did ask for reached the wire, so the
        // absence of a `delete` below is not the transport being idle.
        tokio::time::sleep(Duration::from_millis(20)).await;
        let before = second_log.lock().expect("the log is not poisoned").clone();
        assert!(
            before
                .controls
                .iter()
                .any(|parameters| parameters.contains("LS_op=add")),
            "the second client's own subscription was sent: {:?}",
            before.controls
        );

        match second.unsubscribe(first_id).await {
            Err(Error::ForeignSubscription { id }) => assert_eq!(id, first_id.get()),
            other => panic!("a foreign handle was accepted: {other:?}"),
        }

        tokio::time::sleep(Duration::from_millis(20)).await;
        let after = second_log.lock().expect("the log is not poisoned").clone();
        assert!(
            !after
                .controls
                .iter()
                .any(|parameters| parameters.contains("LS_op=delete")),
            "nothing was sent for a handle this client does not own: {:?}",
            after.controls
        );

        // Its own handle still works.
        assert!(second.unsubscribe(second_id).await.is_ok());
        drop((first, second, first_updates, second_updates));
    }

    /// C-05. A blocked delivery that discovers a dropped receiver must not
    /// swallow the unsubscription the drop itself asked for.
    #[tokio::test]
    async fn test_a_stream_dropped_while_the_router_is_blocked_still_unsubscribes() {
        let mut harness = harness(8);
        let id = SubscriptionId::new(harness.client, a_key().await);
        // One slot: the second event has nowhere to go, so the router blocks
        // inside the send — the state in which the receiver then disappears.
        let (events_tx, events_rx) = mpsc::channel(1);
        assert!(
            harness
                .commands
                .send(RouterCommand::Register {
                    id,
                    events: events_tx,
                })
                .is_ok()
        );
        let updates = Updates::new(id, events_rx, harness.commands.clone());

        assert!(
            harness
                .session_tx
                .send(data(
                    id,
                    WireSubscriptionEvent::Activated {
                        item_count: 1,
                        field_count: 1,
                        command_fields: None,
                    }
                ))
                .await
                .is_ok()
        );
        for dropped_count in [1, 2] {
            assert!(
                harness
                    .session_tx
                    .send(data(
                        id,
                        WireSubscriptionEvent::Overflow {
                            item_index: 1,
                            dropped_count,
                        }
                    ))
                    .await
                    .is_ok()
            );
        }
        // The router is now blocked on a full stream. Dropping it is what the
        // caller does when it walks away.
        tokio::task::yield_now().await;
        drop(updates);

        match tokio::time::timeout(Duration::from_secs(5), harness.unsubscribed.recv()).await {
            Ok(Some(key)) => assert_eq!(key.get(), id.get()),
            other => panic!("the server was left subscribed: {other:?}"),
        }
        // Exactly one: cleanup is not duplicated either.
        assert!(harness.unsubscribed.recv().now_or_never().is_none());
    }

    /// C-02. The router honours a stop while a caller's stream is full, so a
    /// client being torn down is not held open by a consumer that walked away.
    #[tokio::test]
    async fn test_the_router_stops_while_a_callers_stream_is_full() {
        let harness = harness(1);
        let id = SubscriptionId::new(harness.client, a_key().await);
        let (events_tx, _events_rx) = mpsc::channel(1);
        assert!(
            harness
                .commands
                .send(RouterCommand::Register {
                    id,
                    events: events_tx,
                })
                .is_ok()
        );
        for _ in 0..4 {
            let _ = harness
                .session_tx
                .send(data(
                    id,
                    WireSubscriptionEvent::Overflow {
                        item_index: 1,
                        dropped_count: 1,
                    },
                ))
                .await;
        }

        assert!(harness.stop.send(true).is_ok());
        // The router drops every sender as it stops, which is what ends the
        // caller's streams.
        match tokio::time::timeout(Duration::from_secs(5), harness.session_tx.closed()).await {
            Ok(()) => {}
            Err(_) => panic!("the router did not stop while a stream was full"),
        }
    }
}