use super::connector::Connector;
use super::tests::test_options;
use super::{Client, Receiver, RequestOptions, TonicStreaming};
use crate::google::storage::v2::{BidiWriteObjectRequest, BidiWriteObjectResponse};
use gaxi::grpc::tonic::{Extensions, Response as TonicResponse, Result as TonicResult};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct SharedMockClient(pub(crate) Arc<MockTestClient>);
impl SharedMockClient {
pub fn new(mock: MockTestClient) -> Self {
Self(Arc::new(mock))
}
}
impl Client for SharedMockClient {
type Stream = MockStream;
async fn start(
&self,
extensions: Extensions,
path: http::uri::PathAndQuery,
rx: Receiver<BidiWriteObjectRequest>,
options: &RequestOptions,
api_client_header: &'static str,
request_params: &str,
) -> crate::Result<TonicResult<TonicResponse<Self::Stream>>> {
self.0.start(
extensions,
path,
rx,
options,
api_client_header,
request_params,
)
}
}
impl TonicStreaming for Receiver<TonicResult<BidiWriteObjectResponse>> {
async fn next_message(&mut self) -> TonicResult<Option<BidiWriteObjectResponse>> {
self.recv().await.transpose()
}
}
#[mockall::automock]
pub trait TestClient: std::fmt::Debug {
fn start(
&self,
extensions: Extensions,
path: http::uri::PathAndQuery,
rx: Receiver<BidiWriteObjectRequest>,
options: &RequestOptions,
api_client_header: &'static str,
request_params: &str,
) -> crate::Result<TonicResult<TonicResponse<MockStream>>>;
}
pub type MockStream = Receiver<TonicResult<BidiWriteObjectResponse>>;
#[allow(dead_code)]
pub fn mock_connector(mock: MockTestClient) -> Connector<SharedMockClient> {
let client = SharedMockClient::new(mock);
Connector::new(test_options(), client.clone())
}