Skip to content

Commit d842272

Browse files
committed
test: add resiliency test case
Signed-off-by: mikeee <[email protected]>
1 parent 68e3238 commit d842272

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ path = "examples/query_state/query1.rs"
9090
name = "query_state_q2"
9191
path = "examples/query_state/query2.rs"
9292

93+
[[example]]
94+
name = "resiliency"
95+
path = "examples/resiliency/main.rs"
96+
9397
[[example]]
9498
name = "secrets-bulk"
9599
path = "examples/secrets-bulk/app.rs"

examples/resiliency/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
This example validates the resiliency and does not demonstrate any extra
2+
functionality. It is based off the configuration example to connect to the
3+
sidecar and make a call for a configuration item stored in redis.
4+
5+
1. Insert a key with the value `hello` to redis using the following command:
6+
7+
8+
<!-- STEP
9+
name: Insert test configuration item
10+
output_match_mode: substring
11+
expected_stdout_lines:
12+
- 'OK'
13+
background: false
14+
sleep: 5
15+
timeout_seconds: 5
16+
-->
17+
18+
```bash
19+
docker exec dapr_redis redis-cli MSET hello "world"
20+
```
21+
22+
<!-- END_STEP -->
23+
24+
2. Run the example without the sidecar using the following command:
25+
26+
<!-- STEP
27+
name: Run configuration app (expecting a fail)
28+
env:
29+
DAPR_GRPC_PORT: "3500"
30+
DAPR_API_MAX_RETRIES: "10"
31+
DAPR_API_TIMEOUT_MILLISECONDS: "10000"
32+
output_match_mode: substring
33+
expected_stdout_lines:
34+
- ''
35+
expected_stderr_lines:
36+
- 'TransportError'
37+
expected_return_code: 101
38+
background: false
39+
sleep: 30
40+
timeout_seconds: 30
41+
-->
42+
43+
```bash
44+
cargo run --example resiliency
45+
```
46+
47+
<!-- END_STEP -->
48+
49+
The result should be that the request will fail.
50+
51+
3. Run the example without the sidecar (this time in the background)
52+
53+
<!-- STEP
54+
name: Run configuration app (expecting a success eventually)
55+
env:
56+
DAPR_GRPC_PORT: "3500"
57+
DAPR_API_MAX_RETRIES: "10"
58+
DAPR_API_TIMEOUT_MILLISECONDS: "10000"
59+
output_match_mode: substring
60+
expected_stdout_lines:
61+
- '== APP == Configuration value: ConfigurationItem { value: "world"'
62+
background: true
63+
sleep: 30
64+
timeout_seconds: 30
65+
-->
66+
67+
```bash
68+
cargo run --example resiliency
69+
```
70+
71+
<!-- END_STEP -->
72+
73+
74+
75+
4. Run the Dapr sidecar
76+
77+
<!-- STEP
78+
name: Run Dapr sidecar
79+
output_match_mode: substring
80+
expected_stdout_lines:
81+
- ''
82+
background: true
83+
sleep: 10
84+
timeout_seconds: 10
85+
-->
86+
87+
```bash
88+
dapr run --app-id=rustapp --resources-path ../components --dapr-grpc-port 3500
89+
```
90+
91+
<!-- END_STEP -->
92+
93+
The example app should make contact with the Dapr sidecar and the result should
94+
be returned from the configuration request successfully.
95+
96+
```
97+
Configuration value: ConfigurationItem { value: "world", version: "", metadata: {} }
98+
```

examples/resiliency/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const CONFIGSTORE_NAME: &str = "configstore";
2+
type DaprClient = dapr::Client<dapr::client::TonicClient>;
3+
4+
#[tokio::main]
5+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6+
// Set the Dapr address
7+
let addr = "https://127.0.0.1".to_string();
8+
9+
// Create the client
10+
let mut client = match DaprClient::connect(addr).await {
11+
Ok(client) => {
12+
println!("connected to dapr sidecar");
13+
client
14+
}
15+
Err(error) => {
16+
panic!("failed to connect to dapr sidecar: {:?}", error)
17+
}
18+
};
19+
println!("debug");
20+
21+
let key = String::from("hello");
22+
23+
// get key-value pair in the state store
24+
let response = client
25+
.get_configuration(CONFIGSTORE_NAME, vec![(&key)], None)
26+
.await?;
27+
let val = response.items.get("hello").unwrap();
28+
println!("Configuration value: {val:?}");
29+
30+
Ok(())
31+
}

0 commit comments

Comments
 (0)