forked from testground/sdk-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rs
73 lines (61 loc) · 2.13 KB
/
example.rs
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
use std::borrow::Cow;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = testground::client::Client::new_and_init().await?;
match client.run_parameters().test_case.as_str() {
"example" => example(client).await,
"publish-subscribe" => publish_subscribe(client).await,
_ => panic!("Unknown test case: {}", client.run_parameters().test_case),
}
}
async fn example(client: testground::client::Client) -> Result<(), Box<dyn std::error::Error>> {
client.record_message(format!(
"{}, sdk-rust!",
client
.run_parameters()
.test_instance_params
.get("greeting")
.unwrap()
));
let json = serde_json::json!({"foo": "bar"});
client.publish("demonstration", Cow::Owned(json)).await?;
client.record_success().await?;
Ok(())
}
async fn publish_subscribe(
client: testground::client::Client,
) -> Result<(), Box<dyn std::error::Error>> {
client.record_message("running the publish_subscribe test");
match client.global_seq() {
1 => {
client.record_message("I am instance 1: acting as the leader");
let json = serde_json::json!({"foo": "bar"});
client.publish("demonstration", Cow::Owned(json)).await?;
client.record_success().await?;
}
_ => {
client.record_message(format!(
"I am instance {}: acting as a follower",
client.global_seq()
));
let payload = client
.subscribe("demonstration")
.await
.take(1)
.map(|x| x.unwrap())
.next()
.await
.unwrap();
client.record_message(format!("I received the payload: {}", payload));
if payload["foo"].as_str() == Some("bar") {
client.record_success().await?;
} else {
client
.record_failure(format!("invalid payload: {}", payload))
.await?;
}
}
}
Ok(())
}