From 039854715515f80d177b06bb7b1664722d90d17e Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Tue, 26 Jul 2022 17:03:47 +0200 Subject: [PATCH] test: introduce publish-subscribe example & test --- .github/workflows/ci.yml | 18 +++++++++++--- examples/example.rs | 51 ++++++++++++++++++++++++++++++++++++++-- manifest.toml | 6 ++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2de6dac..bc688f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -114,7 +114,7 @@ jobs: testground healthcheck --runner local:docker --fix; shell: bash - - name: Run testground plan (sdk-rust) + - name: Run testground plan (case=example) run: | testground run single \ --plan=sdk-rust \ @@ -124,7 +124,19 @@ jobs: --instances=1 \ --wait \ --collect \ - --collect-file ./result.tgz + --collect-file ./result_example.tgz + + - name: Run testground plan (case=publish-subscribe) + run: | + testground run single \ + --plan=sdk-rust \ + --testcase=publish-subscribe \ + --builder=docker:generic \ + --runner=local:docker \ + --instances=1 \ + --wait \ + --collect \ + --collect-file ./result_publish_subscribe.tgz - uses: actions/upload-artifact@v3 if: ${{ always() }} @@ -132,4 +144,4 @@ jobs: name: testground-output path: | testground/daemon.* - result.tgz + result*.tgz diff --git a/examples/example.rs b/examples/example.rs index e45b40e..b996a29 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -1,9 +1,19 @@ use std::borrow::Cow; +use tokio_stream::StreamExt; + #[tokio::main] async fn main() -> Result<(), Box> { let client = testground::client::Client::new_and_init().await?; + match client.run_parameters().test_case.as_str() { + "example" => example(client).await, + "publish-subscribe" => publish_subscribe(client).await, + _ => panic!("Unknown test case: {}", client.run_parameters().test_case), + } +} + +async fn example(client: testground::client::Client) -> Result<(), Box> { client.record_message(format!( "{}, sdk-rust!", client @@ -14,10 +24,47 @@ async fn main() -> Result<(), Box> { )); let json = serde_json::json!({"foo": "bar"}); - client.publish("demonstration", Cow::Owned(json)).await?; - client.record_success().await?; Ok(()) } + +async fn publish_subscribe( + client: testground::client::Client, +) -> Result<(), Box> { + client.record_message("running the publish_subscribe test"); + + match client.global_seq() { + 1 => { + client.record_message("I am instance 1: acting as the leader"); + + let json = serde_json::json!({"foo": "bar"}); + client.publish("demonstration", Cow::Owned(json)).await?; + client.record_success().await?; + } + _ => { + client.record_message(format!("I am instance {}: acting as a follower", client.global_seq())); + + let payload = client + .subscribe("demonstration") + .await + .take(1) + .map(|x| x.unwrap()) + .next() + .await + .unwrap(); + + client.record_message(format!("I received the payload: {}", payload)); + + if payload["foo"].as_str() == Some("bar") { + client.record_success().await?; + } else { + client + .record_failure(format!("invalid payload: {}", payload)) + .await?; + } + } + } + Ok(()) +} diff --git a/manifest.toml b/manifest.toml index 3e28d39..a6052d1 100644 --- a/manifest.toml +++ b/manifest.toml @@ -15,4 +15,8 @@ name = "example" instances = { min = 1, max = 1, default = 1 } [testcases.params] - greeting = { type = "string", desc = "greeting", default = "Hello" } \ No newline at end of file + greeting = { type = "string", desc = "greeting", default = "Hello" } + +[[testcases]] +name = "publish-subscribe" +instances = { min = 2, max = 10, default = 2 } \ No newline at end of file