|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use tokio::sync::oneshot; |
| 4 | +use tracing::warn; |
| 5 | + |
1 | 6 | use crate::transport::reader::ReaderRef; |
2 | 7 | use crate::transport::writer::WriterRef; |
3 | 8 |
|
| 9 | +const FORCE_CLOSE_TIMEOUT: Duration = Duration::from_secs(10); |
| 10 | + |
4 | 11 | pub struct FixConnection { |
5 | 12 | writer: WriterRef, |
6 | 13 | reader: ReaderRef, |
| 14 | + writer_exit: oneshot::Receiver<()>, |
7 | 15 | } |
8 | 16 |
|
9 | 17 | impl FixConnection { |
10 | | - pub fn new(writer: WriterRef, reader: ReaderRef) -> Self { |
11 | | - Self { writer, reader } |
| 18 | + pub fn new(writer: WriterRef, reader: ReaderRef, writer_exit: oneshot::Receiver<()>) -> Self { |
| 19 | + Self { |
| 20 | + writer, |
| 21 | + reader, |
| 22 | + writer_exit, |
| 23 | + } |
12 | 24 | } |
| 25 | + |
13 | 26 | pub fn get_writer(&self) -> WriterRef { |
14 | 27 | self.writer.clone() |
15 | 28 | } |
16 | 29 |
|
17 | 30 | pub async fn run_until_disconnect(self) { |
18 | | - self.reader.wait_for_disconnect().await |
| 31 | + let Self { |
| 32 | + reader, |
| 33 | + mut writer_exit, |
| 34 | + .. |
| 35 | + } = self; |
| 36 | + let ReaderRef { |
| 37 | + mut disconnect_signal, |
| 38 | + kill, |
| 39 | + } = reader; |
| 40 | + |
| 41 | + tokio::select! { |
| 42 | + _ = &mut disconnect_signal => return, |
| 43 | + _ = &mut writer_exit => {} |
| 44 | + } |
| 45 | + |
| 46 | + match tokio::time::timeout(FORCE_CLOSE_TIMEOUT, &mut disconnect_signal).await { |
| 47 | + Ok(_) => {} |
| 48 | + Err(_) => { |
| 49 | + warn!( |
| 50 | + "reader did not observe EOF within {:?}, forcing close", |
| 51 | + FORCE_CLOSE_TIMEOUT |
| 52 | + ); |
| 53 | + let _ = kill.send(()); |
| 54 | + let _ = disconnect_signal.await; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use super::*; |
| 63 | + use crate::transport::writer::WriterMessage; |
| 64 | + use tokio::sync::mpsc; |
| 65 | + |
| 66 | + /// Build a `FixConnection` and return the ends the test controls: |
| 67 | + /// dc_sender to fire from the "reader", writer_exit_tx to fire from the "writer", |
| 68 | + /// and kill_rx so the test can observe or simulate the reader being killed. |
| 69 | + fn test_connection() -> ( |
| 70 | + FixConnection, |
| 71 | + oneshot::Sender<()>, |
| 72 | + oneshot::Sender<()>, |
| 73 | + oneshot::Receiver<()>, |
| 74 | + ) { |
| 75 | + let (dc_tx, dc_rx) = oneshot::channel::<()>(); |
| 76 | + let (kill_tx, kill_rx) = oneshot::channel::<()>(); |
| 77 | + let reader_ref = ReaderRef::new(dc_rx, kill_tx); |
| 78 | + |
| 79 | + let (writer_mpsc_tx, _writer_mpsc_rx) = mpsc::channel::<WriterMessage>(1); |
| 80 | + let writer_ref = WriterRef::new(writer_mpsc_tx); |
| 81 | + |
| 82 | + let (writer_exit_tx, writer_exit_rx) = oneshot::channel::<()>(); |
| 83 | + |
| 84 | + let conn = FixConnection::new(writer_ref, reader_ref, writer_exit_rx); |
| 85 | + (conn, dc_tx, writer_exit_tx, kill_rx) |
| 86 | + } |
| 87 | + |
| 88 | + /// Reader signals disconnect first — return immediately, kill is never sent. |
| 89 | + #[tokio::test(start_paused = true)] |
| 90 | + async fn returns_on_reader_disconnect_before_writer_exit() { |
| 91 | + let (conn, dc_tx, _writer_exit_tx, mut kill_rx) = test_connection(); |
| 92 | + |
| 93 | + dc_tx.send(()).expect("dc receiver dropped"); |
| 94 | + |
| 95 | + conn.run_until_disconnect().await; |
| 96 | + |
| 97 | + // Kill should not have been sent. The sender has been dropped by now |
| 98 | + // (scope ended inside run_until_disconnect), so try_recv returns Closed |
| 99 | + // rather than Empty. Either way, an Ok(()) would mean kill was sent. |
| 100 | + assert!( |
| 101 | + !matches!(kill_rx.try_recv(), Ok(())), |
| 102 | + "kill signal should not have been sent" |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /// Writer exits first, reader disconnects within the watchdog window — no kill. |
| 107 | + #[tokio::test(start_paused = true)] |
| 108 | + async fn returns_when_reader_disconnects_after_writer_exit_within_timeout() { |
| 109 | + let (conn, dc_tx, writer_exit_tx, mut kill_rx) = test_connection(); |
| 110 | + |
| 111 | + writer_exit_tx |
| 112 | + .send(()) |
| 113 | + .expect("writer_exit receiver dropped"); |
| 114 | + |
| 115 | + // Fire the reader disconnect from a task that runs on the same paused clock. |
| 116 | + tokio::spawn(async move { |
| 117 | + tokio::time::sleep(Duration::from_secs(1)).await; |
| 118 | + let _ = dc_tx.send(()); |
| 119 | + }); |
| 120 | + |
| 121 | + conn.run_until_disconnect().await; |
| 122 | + |
| 123 | + assert!( |
| 124 | + !matches!(kill_rx.try_recv(), Ok(())), |
| 125 | + "kill signal should not have been sent when reader disconnected within timeout" |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /// Writer exits first, reader stays blocked past the watchdog — kill fires, |
| 130 | + /// and a simulated reader fires dc once it sees the kill. |
| 131 | + #[tokio::test(start_paused = true)] |
| 132 | + async fn watchdog_fires_kill_when_reader_stuck() { |
| 133 | + let (conn, dc_tx, writer_exit_tx, kill_rx) = test_connection(); |
| 134 | + |
| 135 | + writer_exit_tx |
| 136 | + .send(()) |
| 137 | + .expect("writer_exit receiver dropped"); |
| 138 | + |
| 139 | + // Stand in for the reader: when the watchdog kills us, we publish dc. |
| 140 | + tokio::spawn(async move { |
| 141 | + if kill_rx.await.is_ok() { |
| 142 | + let _ = dc_tx.send(()); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + let start = tokio::time::Instant::now(); |
| 147 | + conn.run_until_disconnect().await; |
| 148 | + let elapsed = start.elapsed(); |
| 149 | + |
| 150 | + assert!( |
| 151 | + elapsed >= FORCE_CLOSE_TIMEOUT, |
| 152 | + "expected watchdog to take at least {:?}, took {:?}", |
| 153 | + FORCE_CLOSE_TIMEOUT, |
| 154 | + elapsed |
| 155 | + ); |
19 | 156 | } |
20 | 157 | } |
0 commit comments