google_cloud_pubsub/publisher/
base_publisher.rs1use crate::publisher::builder::PublisherPartialBuilder;
16use std::time::Duration;
17
18#[derive(Clone, Debug)]
45pub struct BasePublisher {
46 pub(crate) inner: crate::generated::gapic_dataplane::client::Publisher,
47 pub(crate) total_timeout: Option<Duration>,
48}
49
50pub use super::client_builder::BasePublisherBuilder;
51
52impl BasePublisher {
53 pub fn builder() -> BasePublisherBuilder {
62 BasePublisherBuilder::new()
63 }
64
65 pub(crate) async fn new(builder: BasePublisherBuilder) -> crate::ClientBuilderResult<Self> {
67 let total_timeout = builder.config.retry_policy.as_ref().and_then(|p| {
68 p.remaining_time(
69 &google_cloud_gax::retry_state::RetryState::new(false)
70 .set_start(tokio::time::Instant::now().into_std()),
71 )
72 });
73 let inner =
74 crate::generated::gapic_dataplane::client::Publisher::new(builder.config).await?;
75 std::result::Result::Ok(Self {
76 inner,
77 total_timeout,
78 })
79 }
80
81 pub fn publisher<T>(&self, topic: T) -> PublisherPartialBuilder
95 where
96 T: Into<String>,
97 {
98 PublisherPartialBuilder::new(self.inner.clone(), topic.into())
99 .with_total_timeout(self.total_timeout)
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::BasePublisher;
106 use google_cloud_auth::credentials::anonymous::Builder as Anonymous;
107 use google_cloud_gax::retry_policy::{AlwaysRetry, RetryPolicyExt};
108 use std::time::Duration;
109
110 #[tokio::test]
111 async fn builder() -> anyhow::Result<()> {
112 let client = BasePublisher::builder()
113 .with_credentials(Anonymous::new().build())
114 .build()
115 .await?;
116 let _ = client.publisher("projects/my-project/topics/my-topic".to_string());
117 Ok(())
118 }
119
120 #[tokio::test(start_paused = true)]
121 async fn default_total_timeout() -> anyhow::Result<()> {
122 let client = BasePublisher::builder()
123 .with_credentials(Anonymous::new().build())
124 .build()
125 .await?;
126 let timeout = client
127 .total_timeout
128 .expect("default total_timeout should be present");
129 assert_eq!(timeout, Duration::from_secs(600));
130
131 let partial_builder = client.publisher("projects/my-project/topics/my-topic");
132 assert_eq!(partial_builder.total_timeout, client.total_timeout);
133 Ok(())
134 }
135
136 #[tokio::test(start_paused = true)]
137 async fn custom_total_timeout() -> anyhow::Result<()> {
138 let client = BasePublisher::builder()
139 .with_credentials(Anonymous::new().build())
140 .with_retry_policy(AlwaysRetry.with_time_limit(Duration::from_secs(45)))
141 .build()
142 .await?;
143 let timeout = client
144 .total_timeout
145 .expect("custom total_timeout should be present");
146 assert_eq!(timeout, Duration::from_secs(45));
147
148 let partial_builder = client.publisher("projects/my-project/topics/my-topic");
149 assert_eq!(partial_builder.total_timeout, client.total_timeout);
150 Ok(())
151 }
152
153 #[tokio::test]
154 async fn attempt_limit_only_has_no_total_timeout() -> anyhow::Result<()> {
155 let client = BasePublisher::builder()
156 .with_credentials(Anonymous::new().build())
157 .with_retry_policy(AlwaysRetry.with_attempt_limit(3))
158 .build()
159 .await?;
160 assert_eq!(client.total_timeout, None);
161 let partial_builder = client.publisher("projects/my-project/topics/my-topic");
162 assert_eq!(partial_builder.total_timeout, None);
163 Ok(())
164 }
165}