-
Notifications
You must be signed in to change notification settings - Fork 139
test(gax): add tests for bidi_stream and bidi_stream_with_status #6313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
joshuatants
wants to merge
1
commit into
googleapis:grpc_rust
Choose a base branch
from
joshuatants:grpc_rust_invoker_cleanup
base: grpc_rust
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+358
−142
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,14 +229,14 @@ where | |
| } | ||
| } | ||
|
|
||
| // TODO(#5991): Add tests for GrpcRustStreaming in an upcoming PR. | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use bytes::Bytes; | ||
| use grpc::client::{RecvStream, ResponseStreamItem, SendOptions, SendStream}; | ||
| use grpc::core::{RecvMessage, ResponseHeaders, SendMessage, Trailers}; | ||
| use grpc::metadata::MetadataValue; | ||
| use grpc::{StatusCodeError, StatusError}; | ||
| use pretty_assertions::assert_eq; | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
|
|
@@ -246,7 +246,38 @@ mod tests { | |
| value: String, | ||
| } | ||
|
|
||
| // TODO(#5991): Add tests for failure paths. | ||
| struct TestSendStream { | ||
| observed_messages: Arc<Mutex<Vec<TestMessage>>>, | ||
| notify: Arc<tokio::sync::Notify>, | ||
| } | ||
|
|
||
| impl SendStream for TestSendStream { | ||
| async fn send( | ||
| &mut self, | ||
| message: &dyn SendMessage, | ||
| _options: SendOptions, | ||
| ) -> Result<(), ()> { | ||
| let mut encoded = message.encode().map_err(|_| ())?; | ||
| let decoded = TestMessage::decode(&mut encoded).map_err(|_| ())?; | ||
| self.observed_messages | ||
| .lock() | ||
| .expect("lock observed messages") | ||
| .push(decoded); | ||
| self.notify.notify_one(); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| // TODO(#5991): Refactor common stream state test mocks across grpc_rust tests. | ||
| #[derive(Default)] | ||
| enum StreamState { | ||
| #[default] | ||
| Initial, | ||
| HeadersSent, | ||
| MessageSent, | ||
| Done, | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn bidi_call_yields_response_messages() -> anyhow::Result<()> { | ||
| // Arrange | ||
|
|
@@ -286,38 +317,6 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| struct TestSendStream { | ||
| observed_messages: Arc<Mutex<Vec<TestMessage>>>, | ||
| notify: Arc<tokio::sync::Notify>, | ||
| } | ||
|
|
||
| impl SendStream for TestSendStream { | ||
| async fn send( | ||
| &mut self, | ||
| message: &dyn SendMessage, | ||
| _options: SendOptions, | ||
| ) -> Result<(), ()> { | ||
| let mut encoded = message.encode().map_err(|_| ())?; | ||
| let decoded = TestMessage::decode(&mut encoded).map_err(|_| ())?; | ||
| self.observed_messages | ||
| .lock() | ||
| .expect("lock observed messages") | ||
| .push(decoded); | ||
| self.notify.notify_one(); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| // TODO(#5991): Refactor common stream state test mocks across grpc_rust tests. | ||
| #[derive(Default)] | ||
| enum StreamState { | ||
| #[default] | ||
| Initial, | ||
| HeadersSent, | ||
| MessageSent, | ||
| Done, | ||
| } | ||
|
|
||
| /// A mock [`RecvStream`] that simulates a gRPC response stream sequence: | ||
| /// | ||
| /// 1. Waits until at least one request message is sent by the client, then returns response headers. | ||
|
|
@@ -420,4 +419,74 @@ mod tests { | |
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn bidi_call_yields_error_on_server_error_status() -> anyhow::Result<()> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick check of my understanding: So this doesn't test the immediate error check in Do you want to test the case where invoke_bidi returns an error immediately? |
||
| // Arrange | ||
| const METHOD_NAME: &str = "/google.test.v1.Test/Bidi"; | ||
| const ERROR_MESSAGE: &str = "stream aborted"; | ||
|
|
||
| struct TestErrorInvoker; | ||
|
|
||
| impl Invoke for TestErrorInvoker { | ||
| type SendStream = TestSendStream; | ||
| type RecvStream = TestErrorRecvStream; | ||
|
|
||
| async fn invoke( | ||
| &self, | ||
| _headers: RequestHeaders, | ||
| _options: CallOptions, | ||
| ) -> (Self::SendStream, Self::RecvStream) { | ||
| ( | ||
| TestSendStream { | ||
| observed_messages: Arc::new(Mutex::new(Vec::new())), | ||
| notify: Arc::new(tokio::sync::Notify::new()), | ||
| }, | ||
| TestErrorRecvStream { | ||
| state: StreamState::default(), | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| struct TestErrorRecvStream { | ||
| state: StreamState, | ||
| } | ||
|
|
||
| impl RecvStream for TestErrorRecvStream { | ||
| async fn recv(&mut self, _message: &mut dyn RecvMessage) -> ResponseStreamItem { | ||
| match self.state { | ||
| StreamState::Initial => { | ||
| self.state = StreamState::HeadersSent; | ||
| ResponseStreamItem::Headers(ResponseHeaders::new()) | ||
| } | ||
| _ => { | ||
| self.state = StreamState::Done; | ||
| let err = StatusError::new(StatusCodeError::Aborted, ERROR_MESSAGE); | ||
| ResponseStreamItem::Trailers(Trailers::new(Err(err))) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let invoker = TestErrorInvoker; | ||
| let headers = RequestHeaders::new().with_method_name(METHOD_NAME); | ||
|
|
||
| // Act | ||
| let response = | ||
| invoke_bidi::<TestMessage, TestMessage, _>(&invoker, headers, tokio_stream::empty()) | ||
| .await?; | ||
|
|
||
| // Assert | ||
| let mut stream = response.into_inner(); | ||
| let err = stream | ||
| .message() | ||
| .await | ||
| .expect_err("should return status error from trailers"); | ||
| assert_eq!(err.code(), tonic::Code::Aborted); | ||
| assert_eq!(err.message(), ERROR_MESSAGE); | ||
| assert_eq!(stream.message().await?, None); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the unit test cases do not cover the case of request stream failure? Do we need to test this case?