|
| 1 | +#[path = "h1_server/mod.rs"] |
| 2 | +mod h1_server; |
| 3 | + |
| 4 | +use std::future::Future; |
| 5 | +use h1_server::{init_tracing, fixture, StreamReadHalf}; |
| 6 | +use hyper::rt::{Read, ReadBufCursor, Write}; |
| 7 | +use pin_project_lite::pin_project; |
| 8 | +use std::io; |
| 9 | +use std::pin::Pin; |
| 10 | +use std::task::{ready, Context, Poll}; |
| 11 | +use std::time::Duration; |
| 12 | +use tokio::sync::mpsc; |
| 13 | +use tokio::time::Sleep; |
| 14 | +use tracing::error; |
| 15 | + |
| 16 | +pin_project! { |
| 17 | + #[derive(Debug)] |
| 18 | + pub struct ReadyOnPollStream { |
| 19 | + #[pin] |
| 20 | + read_half: StreamReadHalf, |
| 21 | + write_tx: mpsc::UnboundedSender<Vec<u8>>, |
| 22 | + #[pin] |
| 23 | + pending_write: Option<Pin<Box<Sleep>>>, |
| 24 | + poll_since_write: bool, |
| 25 | + flush_count: usize, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl ReadyOnPollStream { |
| 30 | + fn new( |
| 31 | + read_rx: mpsc::UnboundedReceiver<Vec<u8>>, |
| 32 | + write_tx: mpsc::UnboundedSender<Vec<u8>>, |
| 33 | + ) -> Self { |
| 34 | + Self { |
| 35 | + read_half: StreamReadHalf::new(read_rx), |
| 36 | + write_tx, |
| 37 | + poll_since_write: true, |
| 38 | + flush_count: 0, |
| 39 | + pending_write: None, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Create a new server stream and client pair. |
| 44 | + /// Returns a server stream (Read+Write) and a client (rx/tx channels). |
| 45 | + pub fn new_pair() -> (Self, fixture::Client) { |
| 46 | + let (client_tx, server_rx) = mpsc::unbounded_channel(); |
| 47 | + let (server_tx, client_rx) = mpsc::unbounded_channel(); |
| 48 | + let server = Self::new(server_rx, server_tx); |
| 49 | + let client = fixture::Client { |
| 50 | + rx: client_rx, |
| 51 | + tx: client_tx, |
| 52 | + }; |
| 53 | + (server, client) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl Read for ReadyOnPollStream { |
| 58 | + fn poll_read( |
| 59 | + mut self: Pin<&mut Self>, |
| 60 | + cx: &mut Context<'_>, |
| 61 | + buf: ReadBufCursor<'_>, |
| 62 | + ) -> Poll<io::Result<()>> { |
| 63 | + self.as_mut().project().read_half.poll_read(cx, buf) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const WRITE_DELAY: Duration = Duration::from_millis(100); |
| 68 | + |
| 69 | +impl Write for ReadyOnPollStream { |
| 70 | + fn poll_write( |
| 71 | + mut self: Pin<&mut Self>, |
| 72 | + cx: &mut Context<'_>, |
| 73 | + buf: &[u8], |
| 74 | + ) -> Poll<io::Result<usize>> { |
| 75 | + if let Some(sleep) = self.pending_write.as_mut() { |
| 76 | + let sleep = sleep.as_mut(); |
| 77 | + ready!(Future::poll(sleep, cx)); |
| 78 | + } |
| 79 | + { |
| 80 | + let mut this = self.as_mut().project(); |
| 81 | + this.pending_write.set(Some(Box::pin(tokio::time::sleep(WRITE_DELAY)))); |
| 82 | + } |
| 83 | + let Some(sleep) = self.pending_write.as_mut() else { |
| 84 | + panic!("Sleep should have just been set"); |
| 85 | + }; |
| 86 | + // poll the future so that we can woken |
| 87 | + let sleep = sleep.as_mut(); |
| 88 | + let Poll::Pending = Future::poll(sleep, cx) else { |
| 89 | + panic!("Sleep always be pending on first poll") |
| 90 | + }; |
| 91 | + |
| 92 | + let this = self.project(); |
| 93 | + let buf = Vec::from(&buf[..buf.len()]); |
| 94 | + let len = buf.len(); |
| 95 | + |
| 96 | + // Send data through the channel - this should always be ready for unbounded channels |
| 97 | + match this.write_tx.send(buf) { |
| 98 | + Ok(_) => Poll::Ready(Ok(len)), |
| 99 | + Err(_) => { |
| 100 | + error!("ReadyStream::poll_write failed - channel closed"); |
| 101 | + Poll::Ready(Err(io::Error::new( |
| 102 | + io::ErrorKind::BrokenPipe, |
| 103 | + "Write channel closed", |
| 104 | + ))) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 110 | + self.flush_count += 1; |
| 111 | + // We require two flushes to complete each chunk, simulating a success at the end of the old |
| 112 | + // poll loop. After all chunks are written, we always succeed on flush to allow for finish. |
| 113 | + const TOTAL_CHUNKS: usize = 16; |
| 114 | + if self.flush_count % 2 != 0 && self.flush_count < TOTAL_CHUNKS * 2 { |
| 115 | + if let Some(sleep) = self.pending_write.as_mut() { |
| 116 | + let sleep = sleep.as_mut(); |
| 117 | + ready!(Future::poll(sleep, cx)); |
| 118 | + } else { |
| 119 | + return Poll::Pending; |
| 120 | + } |
| 121 | + } |
| 122 | + let mut this = self.as_mut().project(); |
| 123 | + this.pending_write.set(None); |
| 124 | + Poll::Ready(Ok(())) |
| 125 | + } |
| 126 | + |
| 127 | + fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 128 | + Poll::Ready(Ok(())) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 133 | +async fn body_test() { |
| 134 | + init_tracing(); |
| 135 | + let (server, client) = ReadyOnPollStream::new_pair(); |
| 136 | + let config = fixture::TestConfig::with_timeout(WRITE_DELAY*2); |
| 137 | + fixture::run(server, client, config).await; |
| 138 | +} |
0 commit comments