Skip to content

Commit 51f8067

Browse files
committed
test(pubsub): add simple integration test
1 parent 0431b38 commit 51f8067

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/integration-tests/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ path = "../../src/generated/cloud/bigquery/v2"
6060
package = "google-cloud-firestore"
6161
path = "../../src/firestore"
6262

63+
[dependencies.pubsub]
64+
package = "google-cloud-pubsub"
65+
path = "../../src/pubsub"
66+
6367
[dependencies.showcase]
6468
package = "google-cloud-showcase-v1beta1"
6569
path = "../../src/generated/showcase"

src/integration-tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub type Result<T> = anyhow::Result<T>;
1919
pub mod bigquery;
2020
pub mod error_details;
2121
pub mod firestore;
22+
pub mod pubsub;
2223
pub mod secret_manager;
2324
pub mod showcase;
2425
pub mod sql;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use pubsub::{client::TopicAdmin, model::Topic};
16+
use rand::{Rng, distr::Alphanumeric};
17+
18+
use crate::Result;
19+
20+
pub async fn basic_topic(builder: pubsub::builder::topic_admin::ClientBuilder) -> Result<()> {
21+
// Enable a basic subscriber. Useful to troubleshoot problems and visually
22+
// verify tracing is doing something.
23+
#[cfg(feature = "log-integration-tests")]
24+
let _guard = {
25+
use tracing_subscriber::fmt::format::FmtSpan;
26+
let subscriber = tracing_subscriber::fmt()
27+
.with_level(true)
28+
.with_thread_ids(true)
29+
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
30+
.finish();
31+
32+
tracing::subscriber::set_default(subscriber)
33+
};
34+
35+
let project = crate::project_id()?;
36+
let client = builder.build().await?;
37+
let (topic_admin, topic) = create_test_topic().await?;
38+
39+
tracing::info!("testing list_topics()");
40+
let topics = client
41+
.list_topics()
42+
.set_project(format!("projects/{project}"))
43+
.send()
44+
.await?;
45+
tracing::info!("success with list_topics={topics:?}");
46+
assert!(topics.topics.iter().any(|x| x.name == topic.name.clone()));
47+
48+
cleanup_test_topic(topic_admin, topic.name.clone()).await?;
49+
50+
Ok(())
51+
}
52+
53+
pub const TOPIC_ID_LENGTH: usize = 255;
54+
55+
fn random_topic_id(project: String) -> String {
56+
let prefix = "topic-";
57+
let topic_id: String = rand::rng()
58+
.sample_iter(&Alphanumeric)
59+
.take(TOPIC_ID_LENGTH - prefix.len())
60+
.map(char::from)
61+
.collect();
62+
format!("projects/{project}/topics/{prefix}{topic_id}")
63+
}
64+
65+
pub async fn create_test_topic() -> Result<(TopicAdmin, Topic)> {
66+
let project = crate::project_id()?;
67+
let client = pubsub::client::TopicAdmin::builder()
68+
.with_tracing()
69+
.build()
70+
.await?;
71+
let topic_id = random_topic_id(project.clone());
72+
73+
tracing::info!("testing create_topic()");
74+
let topic = client
75+
.create_topic()
76+
.set_name(topic_id.clone())
77+
.set_labels([("integration-test", "true")])
78+
.send()
79+
.await?;
80+
tracing::info!("success on create_topic: {topic:?}");
81+
82+
Ok((client, topic))
83+
}
84+
85+
pub async fn cleanup_test_topic(client: TopicAdmin, topic_name: String) -> Result<()> {
86+
tracing::info!("testing delete_topic()");
87+
client.delete_topic().set_topic(topic_name).send().await?;
88+
tracing::info!("success on delete_topic");
89+
Ok(())
90+
}

src/integration-tests/tests/driver.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ mod driver {
4747
.map_err(integration_tests::report_error)
4848
}
4949

50+
#[test_case(pubsub::client::TopicAdmin::builder(); "default")]
51+
#[test_case(pubsub::client::TopicAdmin::builder().with_tracing(); "with tracing enabled")]
52+
#[test_case(pubsub::client::TopicAdmin::builder().with_retry_policy(retry_policy()); "with retry enabled")]
53+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
54+
async fn run_pubsub_basic_topic(
55+
builder: pubsub::builder::topic_admin::ClientBuilder,
56+
) -> integration_tests::Result<()> {
57+
integration_tests::pubsub::basic_topic(builder)
58+
.await
59+
.map_err(integration_tests::report_error)
60+
}
61+
5062
#[test_case(sm::client::SecretManagerService::builder(); "default")]
5163
#[test_case(sm::client::SecretManagerService::builder().with_tracing(); "with tracing enabled")]
5264
#[test_case(sm::client::SecretManagerService::builder().with_retry_policy(retry_policy()); "with retry enabled")]

0 commit comments

Comments
 (0)