Function tonic_mock::process_streaming_response [−][src]
pub async fn process_streaming_response<T, F>(response: StreamResponse<T>, f: F) where
T: Message + Default + 'static,
F: Fn(Result<T, Status>, usize),
a simple wrapper to process and validate streaming response
Usage:
use tonic::{Response, Status}; use futures::Stream; use std::pin::Pin; #[derive(Clone, PartialEq, ::prost::Message)] pub struct ResponsePush { #[prost(int32, tag = "1")] pub code: i32, } // below code is to mimic a stream response from a GRPC service let output = async_stream::try_stream! { yield ResponsePush { code: 0 }; yield ResponsePush { code: 1 }; yield ResponsePush { code: 2 }; }; let response = Response::new(Box::pin(output) as tonic_mock::StreamResponseInner<ResponsePush>); let rt = tokio::runtime::Runtime::new().unwrap(); // now we process the events rt.block_on(async { tonic_mock::process_streaming_response(response, |msg, i| { assert!(msg.is_ok()); assert_eq!(msg.as_ref().unwrap().code, i as i32); }).await; });