|
| 1 | +use super::{ |
| 2 | + encode_server, |
| 3 | + prost::{ProstDecoder, ProstEncoder}, |
| 4 | + Streaming, |
| 5 | +}; |
| 6 | +use crate::Status; |
| 7 | +use bytes::{Buf, BufMut, Bytes, BytesMut, IntoBuf}; |
| 8 | +use http_body::Body; |
| 9 | +use prost::Message; |
| 10 | +use std::{ |
| 11 | + io::Cursor, |
| 12 | + pin::Pin, |
| 13 | + task::{Context, Poll}, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Clone, PartialEq, prost::Message)] |
| 17 | +struct Msg { |
| 18 | + #[prost(bytes, tag = "1")] |
| 19 | + data: Vec<u8>, |
| 20 | +} |
| 21 | + |
| 22 | +#[tokio::test] |
| 23 | +async fn decode() { |
| 24 | + let decoder = ProstDecoder::<Msg>::default(); |
| 25 | + |
| 26 | + let data = Vec::from(&[0u8; 1024][..]); |
| 27 | + let msg = Msg { data }; |
| 28 | + |
| 29 | + let mut buf = BytesMut::new(); |
| 30 | + let len = msg.encoded_len(); |
| 31 | + |
| 32 | + buf.reserve(len + 5); |
| 33 | + buf.put_u8(0); |
| 34 | + buf.put_u32_be(len as u32); |
| 35 | + msg.encode(&mut buf).unwrap(); |
| 36 | + |
| 37 | + let body = MockBody(buf.freeze(), 0, 100); |
| 38 | + |
| 39 | + let mut stream = Streaming::new_request(decoder, body); |
| 40 | + |
| 41 | + while let Some(_) = stream.message().await.unwrap() {} |
| 42 | +} |
| 43 | + |
| 44 | +#[tokio::test] |
| 45 | +async fn encode() { |
| 46 | + let encoder = ProstEncoder::<Msg>::default(); |
| 47 | + |
| 48 | + let data = Vec::from(&[0u8; 1024][..]); |
| 49 | + let msg = Msg { data }; |
| 50 | + |
| 51 | + let messages = std::iter::repeat(Ok::<_, Status>(msg)).take(10000); |
| 52 | + let source = futures_util::stream::iter(messages); |
| 53 | + |
| 54 | + let body = encode_server(encoder, source); |
| 55 | + |
| 56 | + futures_util::pin_mut!(body); |
| 57 | + |
| 58 | + while let Some(r) = body.next().await { |
| 59 | + r.unwrap(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Debug)] |
| 64 | +struct MockBody(Bytes, usize, usize); |
| 65 | + |
| 66 | +impl Body for MockBody { |
| 67 | + type Data = Data; |
| 68 | + type Error = Status; |
| 69 | + |
| 70 | + fn poll_data( |
| 71 | + mut self: Pin<&mut Self>, |
| 72 | + _cx: &mut Context<'_>, |
| 73 | + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { |
| 74 | + if self.1 > self.2 { |
| 75 | + self.1 += 1; |
| 76 | + let data = Data(self.0.clone().into_buf()); |
| 77 | + Poll::Ready(Some(Ok(data))) |
| 78 | + } else { |
| 79 | + Poll::Ready(None) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn poll_trailers( |
| 84 | + self: Pin<&mut Self>, |
| 85 | + cx: &mut Context<'_>, |
| 86 | + ) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> { |
| 87 | + drop(cx); |
| 88 | + Poll::Ready(Ok(None)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +struct Data(Cursor<Bytes>); |
| 93 | + |
| 94 | +impl Into<Bytes> for Data { |
| 95 | + fn into(self) -> Bytes { |
| 96 | + self.0.into_inner() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl Buf for Data { |
| 101 | + fn remaining(&self) -> usize { |
| 102 | + self.0.remaining() |
| 103 | + } |
| 104 | + |
| 105 | + fn bytes(&self) -> &[u8] { |
| 106 | + self.0.bytes() |
| 107 | + } |
| 108 | + |
| 109 | + fn advance(&mut self, cnt: usize) { |
| 110 | + self.0.advance(cnt) |
| 111 | + } |
| 112 | +} |
0 commit comments