From d8422721536d1d7a3533f77586fab4e9cc82d0e5 Mon Sep 17 00:00:00 2001 From: mikeee Date: Fri, 19 Apr 2024 12:06:14 +0100 Subject: [PATCH] test: add resiliency test case Signed-off-by: mikeee --- Cargo.toml | 4 ++ examples/resiliency/README.md | 98 +++++++++++++++++++++++++++++++++++ examples/resiliency/main.rs | 31 +++++++++++ 3 files changed, 133 insertions(+) create mode 100644 examples/resiliency/README.md create mode 100644 examples/resiliency/main.rs diff --git a/Cargo.toml b/Cargo.toml index adb21d5..07ebc95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,6 +90,10 @@ path = "examples/query_state/query1.rs" name = "query_state_q2" path = "examples/query_state/query2.rs" +[[example]] +name = "resiliency" +path = "examples/resiliency/main.rs" + [[example]] name = "secrets-bulk" path = "examples/secrets-bulk/app.rs" diff --git a/examples/resiliency/README.md b/examples/resiliency/README.md new file mode 100644 index 0000000..645a339 --- /dev/null +++ b/examples/resiliency/README.md @@ -0,0 +1,98 @@ +This example validates the resiliency 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 using the following command: + + + +```bash +cargo run --example resiliency +``` + + + +The result should be that the request will fail. + +3. Run the example without the sidecar (this time in the background) + + + +```bash +cargo run --example resiliency +``` + + + + + +4. Run the Dapr sidecar + + + +```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/main.rs b/examples/resiliency/main.rs new file mode 100644 index 0000000..ff54d1c --- /dev/null +++ b/examples/resiliency/main.rs @@ -0,0 +1,31 @@ +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 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) + } + }; + println!("debug"); + + 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:?}"); + + Ok(()) +}