Skip to content

Commit 3a42fbc

Browse files
committed
test: implement instance resiliency test case
Signed-off-by: mikeee <[email protected]>
1 parent e14f341 commit 3a42fbc

File tree

6 files changed

+165
-7
lines changed

6 files changed

+165
-7
lines changed

.github/workflows/validate-examples.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
fail-fast: false
145145
matrix:
146146
examples:
147-
[ "actors", "client", "configuration", "crypto", "invoke/grpc", "invoke/grpc-proxying", "pubsub", "query_state", "resiliency", "secrets-bulk" ]
147+
[ "actors", "client", "configuration", "crypto", "invoke/grpc", "invoke/grpc-proxying", "pubsub", "query_state", "resiliency/instance", "resiliency/simple", "secrets-bulk" ]
148148
steps:
149149
- name: Check out code
150150
uses: actions/checkout@v4

Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ name = "query_state_q2"
9292
path = "examples/query_state/query2.rs"
9393

9494
[[example]]
95-
name = "resiliency"
96-
path = "examples/resiliency/main.rs"
95+
name = "resiliency-instance"
96+
path = "examples/resiliency/instance/main.rs"
97+
98+
[[example]]
99+
name = "resiliency-simple"
100+
path = "examples/resiliency/simple/main.rs"
97101

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

examples/resiliency/instance/main.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::{
2+
thread,
3+
time::{Duration, Instant},
4+
};
5+
6+
const CONFIGSTORE_NAME: &str = "configstore";
7+
type DaprClient = dapr::Client<dapr::client::TonicClient>;
8+
9+
#[tokio::main]
10+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
11+
// Set the Dapr address
12+
let addr = "https://127.0.0.1".to_string();
13+
14+
// Create the client
15+
let start_time = Instant::now();
16+
let mut client = match DaprClient::connect(addr).await {
17+
Ok(client) => {
18+
println!("connected to dapr sidecar");
19+
client
20+
}
21+
Err(error) => {
22+
panic!("failed to connect to dapr sidecar: {:?}", error)
23+
}
24+
};
25+
let client_start_duration = start_time.elapsed();
26+
println!("Client connection took: {:?}", client_start_duration);
27+
28+
let key = String::from("hello");
29+
30+
// get key-value pair in the state store
31+
let response = client
32+
.get_configuration(CONFIGSTORE_NAME, vec![(&key)], None)
33+
.await?;
34+
let val = response.items.get("hello").unwrap();
35+
println!("Configuration value: {val:?}");
36+
37+
thread::sleep(Duration::from_secs(10));
38+
println!("app slept for 15 seconds");
39+
40+
let response = client
41+
.get_configuration(CONFIGSTORE_NAME, vec![(&key)], None)
42+
.await?;
43+
let val = response.items.get("hello").unwrap();
44+
println!("Configuration value: {val:?}");
45+
46+
Ok(())
47+
}

examples/resiliency/README.md renamed to examples/resiliency/simple/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ timeout_seconds: 30
4141
-->
4242

4343
```bash
44-
cargo run --example resiliency
44+
cargo run --example resiliency-simple
4545
```
4646

4747
<!-- END_STEP -->
@@ -65,7 +65,7 @@ timeout_seconds: 30
6565
-->
6666

6767
```bash
68-
cargo run --example resiliency
68+
cargo run --example resiliency-simple
6969
```
7070

7171
<!-- END_STEP -->
@@ -85,7 +85,7 @@ timeout_seconds: 15
8585
-->
8686

8787
```bash
88-
dapr run --app-id=rustapp --resources-path ../components --dapr-grpc-port 3500
88+
dapr run --app-id=rustapp --resources-path .../components --dapr-grpc-port 3500
8989
```
9090

9191
<!-- END_STEP -->

examples/resiliency/main.rs renamed to examples/resiliency/simple/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2020
}
2121
};
2222
let client_start_duration = start_time.elapsed();
23-
println!("Client connection took: {:?}", client_start_duration);
23+
println!("Client connection took: {client_start_duration:?}");
2424

2525
let key = String::from("hello");
2626

0 commit comments

Comments
 (0)