-
Notifications
You must be signed in to change notification settings - Fork 763
Expand file tree
/
Copy pathprepare-data.rs
More file actions
executable file
·81 lines (71 loc) · 2.42 KB
/
prepare-data.rs
File metadata and controls
executable file
·81 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env -S cargo -Zscript
---cargo
[dependencies]
anyhow = "1"
google-cloud-googleapis = { version = "0.13", features = ["pubsub"] }
google-cloud-pubsub = "0.25"
tokio = { version = "0.2", package = "madsim-tokio", features = [
"rt",
"rt-multi-thread",
"sync",
"macros",
"time",
"signal",
"fs",
] }
---
use google_cloud_googleapis::pubsub::v1::PubsubMessage;
use google_cloud_pubsub::client::{Client, ClientConfig};
use google_cloud_pubsub::subscription::SubscriptionConfig;
const TOPIC: &str = "test-topic";
const SUBSCRIPTION_COUNT: usize = 50;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args: Vec<String> = std::env::args().collect();
let command = args[1].as_str();
let use_emulator = std::env::var("PUBSUB_EMULATOR_HOST").is_ok();
let use_cloud = std::env::var("GOOGLE_APPLICATION_CREDENTIALS_JSON").is_ok();
if !use_emulator && !use_cloud {
panic!("either PUBSUB_EMULATOR_HOST or GOOGLE_APPLICATION_CREDENTIALS_JSON must be set");
}
let config = ClientConfig::default().with_auth().await?;
let client = Client::new(config).await?;
let topic = client.topic(TOPIC);
if command == "create" {
// delete and create "test-topic"
for subscription in topic.subscriptions(None).await? {
subscription.delete(None).await?;
}
let _ = topic.delete(None).await;
topic.create(Some(Default::default()), None).await?;
for i in 0..SUBSCRIPTION_COUNT {
let _ = client
.create_subscription(
format!("test-subscription-{}", i).as_str(),
TOPIC,
SubscriptionConfig {
retain_acked_messages: false,
..Default::default()
},
None,
)
.await?;
}
} else if command == "publish" {
let publisher = topic.new_publisher(Default::default());
for i in 0..10 {
let data = format!("{{\"v1\":{i},\"v2\":\"name{i}\"}}");
let a = publisher
.publish(PubsubMessage {
data: data.to_string().into_bytes(),
..Default::default()
})
.await;
a.get().await?;
println!("published {}", data);
}
} else {
panic!("unknown command {command}");
}
Ok(())
}