|
| 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 | +} |
0 commit comments