Skip to content

Commit

Permalink
test: implement instance resiliency test case
Browse files Browse the repository at this point in the history
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee committed Apr 20, 2024
1 parent e14f341 commit d864d0b
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/validate-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ jobs:
fail-fast: false
matrix:
examples:
[ "actors", "client", "configuration", "crypto", "invoke/grpc", "invoke/grpc-proxying", "pubsub", "query_state", "resiliency", "secrets-bulk" ]
[ "actors", "client", "configuration", "crypto", "invoke/grpc", "invoke/grpc-proxying", "pubsub", "query_state", "resiliency/instance", "resiliency/simple", "secrets-bulk" ]
steps:
- name: Check out code
uses: actions/checkout@v4
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ name = "query_state_q2"
path = "examples/query_state/query2.rs"

[[example]]
name = "resiliency"
path = "examples/resiliency/main.rs"
name = "resiliency-instance"
path = "examples/resiliency/instance/main.rs"

[[example]]
name = "resiliency-simple"
path = "examples/resiliency/simple/main.rs"

[[example]]
name = "secrets-bulk"
Expand Down
107 changes: 107 additions & 0 deletions examples/resiliency/instance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
This example validates the resiliency of the instantiated client and does not
demonstrate any extra functionality. It is based off the configuration example
to connect to the sidecar and make a call for a configuration item stored in
redis.

1. Insert a key with the value `hello` to redis using the following command:


<!-- STEP
name: Insert test configuration item
output_match_mode: substring
expected_stdout_lines:
- 'OK'
background: false
sleep: 5
timeout_seconds: 5
-->

```bash
docker exec dapr_redis redis-cli MSET hello "world"
```

<!-- END_STEP -->

2. Run the example without the sidecar

<!-- STEP
name: Run configuration app
env:
DAPR_GRPC_PORT: "3500"
DAPR_API_MAX_RETRIES: "10"
DAPR_API_TIMEOUT_MILLISECONDS: "10000"
output_match_mode: substring
expected_stdout_lines:
- 'Configuration value: ConfigurationItem { value: "world"'
- 'Configuration value: ConfigurationItem { value: "world2"'
background: true
sleep: 30
timeout_seconds: 30
-->

```bash
cargo run --example resiliency-instance
```

<!-- END_STEP -->

3. Run the Dapr sidecar

<!-- STEP
name: Run Dapr sidecar
output_match_mode: substring
expected_stdout_lines:
- ''
background: true
sleep: 10
timeout_seconds: 10
-->

```bash
dapr run --app-id=rustapp --resources-path ../components --dapr-grpc-port 3500
```

<!-- END_STEP -->

4. Update the hello key with the value `world2` to redis using the following command:


<!-- STEP
name: Update test configuration item
output_match_mode: substring
expected_stdout_lines:
- 'OK'
background: false
sleep: 5
timeout_seconds: 5
-->

```bash
docker exec dapr_redis redis-cli MSET hello "world2"
```

<!-- END_STEP -->

5. Run the Dapr sidecar (for the second time)

<!-- STEP
name: Run Dapr sidecar
output_match_mode: substring
expected_stdout_lines:
- ''
background: true
sleep: 10
timeout_seconds: 10
-->

```bash
dapr run --app-id=rustapp --resources-path ../components --dapr-grpc-port 3500
```

<!-- END_STEP -->
The example app should make contact with the Dapr sidecar and the result should
be returned from the configuration request successfully.

```
Configuration value: ConfigurationItem { value: "world", version: "", metadata: {} }
```
47 changes: 47 additions & 0 deletions examples/resiliency/instance/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{
thread,
time::{Duration, Instant},
};

const CONFIGSTORE_NAME: &str = "configstore";
type DaprClient = dapr::Client<dapr::client::TonicClient>;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set the Dapr address
let addr = "https://127.0.0.1".to_string();

// Create the client
let start_time = Instant::now();
let mut client = match DaprClient::connect(addr).await {
Ok(client) => {
println!("connected to dapr sidecar");
client
}
Err(error) => {
panic!("failed to connect to dapr sidecar: {:?}", error)
}
};
let client_start_duration = start_time.elapsed();
println!("Client connection took: {:?}", client_start_duration);

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

// get key-value pair in the state store
let response = client
.get_configuration(CONFIGSTORE_NAME, vec![(&key)], None)
.await?;
let val = response.items.get("hello").unwrap();
println!("Configuration value: {val:?}");

thread::sleep(Duration::from_secs(10));
println!("app slept for 15 seconds");

let response = client
.get_configuration(CONFIGSTORE_NAME, vec![(&key)], None)
.await?;
let val = response.items.get("hello").unwrap();
println!("Configuration value: {val:?}");

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ timeout_seconds: 30
-->

```bash
cargo run --example resiliency
cargo run --example resiliency-simple
```

<!-- END_STEP -->
Expand All @@ -65,7 +65,7 @@ timeout_seconds: 30
-->

```bash
cargo run --example resiliency
cargo run --example resiliency-simple
```

<!-- END_STEP -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};
let client_start_duration = start_time.elapsed();
println!("Client connection took: {:?}", client_start_duration);
println!("Client connection took: {client_start_duration:?}");

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

Expand Down

0 comments on commit d864d0b

Please sign in to comment.