From d864d0b361b2deda9ab429710a1163589a36c118 Mon Sep 17 00:00:00 2001 From: mikeee Date: Sat, 20 Apr 2024 17:42:31 +0100 Subject: [PATCH] test: implement instance resiliency test case Signed-off-by: mikeee --- .github/workflows/validate-examples.yml | 2 +- Cargo.toml | 8 +- examples/resiliency/instance/README.md | 107 +++++++++++++++++++++ examples/resiliency/instance/main.rs | 47 +++++++++ examples/resiliency/{ => simple}/README.md | 4 +- examples/resiliency/{ => simple}/main.rs | 2 +- 6 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 examples/resiliency/instance/README.md create mode 100644 examples/resiliency/instance/main.rs rename examples/resiliency/{ => simple}/README.md (96%) rename examples/resiliency/{ => simple}/main.rs (93%) diff --git a/.github/workflows/validate-examples.yml b/.github/workflows/validate-examples.yml index 49a64669..9ecbf16b 100644 --- a/.github/workflows/validate-examples.yml +++ b/.github/workflows/validate-examples.yml @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 2f0827e4..aa742256 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/resiliency/instance/README.md b/examples/resiliency/instance/README.md new file mode 100644 index 00000000..b6d30365 --- /dev/null +++ b/examples/resiliency/instance/README.md @@ -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: + + + + +```bash +docker exec dapr_redis redis-cli MSET hello "world" +``` + + + +2. Run the example without the sidecar + + + +```bash +cargo run --example resiliency-instance +``` + + + +3. Run the Dapr sidecar + + + +```bash +dapr run --app-id=rustapp --resources-path ../components --dapr-grpc-port 3500 +``` + + + +4. Update the hello key with the value `world2` to redis using the following command: + + + + +```bash +docker exec dapr_redis redis-cli MSET hello "world2" +``` + + + +5. Run the Dapr sidecar (for the second time) + + + +```bash +dapr run --app-id=rustapp --resources-path ../components --dapr-grpc-port 3500 +``` + + +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: {} } +``` diff --git a/examples/resiliency/instance/main.rs b/examples/resiliency/instance/main.rs new file mode 100644 index 00000000..3cdf60b5 --- /dev/null +++ b/examples/resiliency/instance/main.rs @@ -0,0 +1,47 @@ +use std::{ + thread, + time::{Duration, Instant}, +}; + +const CONFIGSTORE_NAME: &str = "configstore"; +type DaprClient = dapr::Client; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // 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(()) +} diff --git a/examples/resiliency/README.md b/examples/resiliency/simple/README.md similarity index 96% rename from examples/resiliency/README.md rename to examples/resiliency/simple/README.md index 619fccd1..15f9e23b 100644 --- a/examples/resiliency/README.md +++ b/examples/resiliency/simple/README.md @@ -41,7 +41,7 @@ timeout_seconds: 30 --> ```bash -cargo run --example resiliency +cargo run --example resiliency-simple ``` @@ -65,7 +65,7 @@ timeout_seconds: 30 --> ```bash -cargo run --example resiliency +cargo run --example resiliency-simple ``` diff --git a/examples/resiliency/main.rs b/examples/resiliency/simple/main.rs similarity index 93% rename from examples/resiliency/main.rs rename to examples/resiliency/simple/main.rs index 34d335b6..c3e2a2f6 100644 --- a/examples/resiliency/main.rs +++ b/examples/resiliency/simple/main.rs @@ -20,7 +20,7 @@ async fn main() -> Result<(), Box> { } }; 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");