From b1c14c8ffb411a93e9fe96012e47ca8f032f8749 Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Fri, 9 Feb 2024 07:10:26 +0800 Subject: [PATCH] test: add (#300) * test: add * test: add * chore: clippy * chore: clippy --- Cargo.lock | 1 + libs/client-api-test-util/Cargo.toml | 1 + libs/client-api-test-util/src/test_client.rs | 2 +- tests/collab/single_device_edit.rs | 53 ++++++++++++++++++-- tests/user/sign_up.rs | 18 +++++++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7718c59e..aee74844 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1312,6 +1312,7 @@ dependencies = [ "collab-folder", "database-entity", "dotenv", + "futures", "gotrue", "image", "lazy_static", diff --git a/libs/client-api-test-util/Cargo.toml b/libs/client-api-test-util/Cargo.toml index 549dae2d..c543d498 100644 --- a/libs/client-api-test-util/Cargo.toml +++ b/libs/client-api-test-util/Cargo.toml @@ -31,6 +31,7 @@ dotenv = "0.15.0" reqwest = "0.11.23" gotrue.workspace = true websocket.workspace = true +futures = "0.3.30" [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3", features = ["console"] } diff --git a/libs/client-api-test-util/src/test_client.rs b/libs/client-api-test-util/src/test_client.rs index 3e85d7c3..d076e034 100644 --- a/libs/client-api-test-util/src/test_client.rs +++ b/libs/client-api-test-util/src/test_client.rs @@ -500,7 +500,7 @@ impl TestClient { ); collab.lock().add_plugin(Arc::new(sync_plugin)); - collab.lock().initialize().await; + futures::executor::block_on(collab.lock().initialize()); let test_collab = TestCollab { origin, collab }; self .collab_by_object_id diff --git a/tests/collab/single_device_edit.rs b/tests/collab/single_device_edit.rs index 0ae8f157..5a0ed92d 100644 --- a/tests/collab/single_device_edit.rs +++ b/tests/collab/single_device_edit.rs @@ -1,9 +1,8 @@ -use collab_entity::CollabType; - use crate::collab::util::{generate_random_string, make_big_collab_doc_state}; +use assert_json_diff::assert_json_eq; use client_api_test_util::*; +use collab_entity::CollabType; use database_entity::dto::AFAccessLevel; - use serde_json::json; use uuid::Uuid; @@ -406,3 +405,51 @@ async fn multiple_collab_edit_test() { ) .await; } + +#[tokio::test] +async fn concurrent_device_edit_test() { + let mut tasks = Vec::new(); + for _i in 0..20 { + let task = tokio::spawn(async move { + let collab_type = CollabType::Document; + let mut test_client = TestClient::new_user().await; + + let workspace_id = test_client.workspace_id().await; + let object_id = Uuid::new_v4().to_string(); + + test_client + .open_collab(&workspace_id, &object_id, collab_type.clone()) + .await; + + let random_str = generate_random_string(200); + test_client + .collab_by_object_id + .get_mut(&object_id) + .unwrap() + .collab + .lock() + .insert("string", random_str.clone()); + let expected_json = json!({ + "string": random_str + }); + + test_client.wait_object_sync_complete(&object_id).await; + ( + expected_json, + test_client + .collab_by_object_id + .get(&object_id) + .unwrap() + .collab + .to_json_value(), + ) + }); + tasks.push(task); + } + + let results = futures::future::join_all(tasks).await; + for result in results { + let (expected_json, json) = result.unwrap(); + assert_json_eq!(expected_json, json); + } +} diff --git a/tests/user/sign_up.rs b/tests/user/sign_up.rs index 4c4247c0..393c6603 100644 --- a/tests/user/sign_up.rs +++ b/tests/user/sign_up.rs @@ -1,6 +1,7 @@ use app_error::ErrorCode; use client_api_test_util::*; use gotrue_entity::dto::AuthProvider; +use std::time::Duration; #[tokio::test] async fn sign_up_success() { @@ -59,3 +60,20 @@ async fn sign_up_oauth_not_available() { ErrorCode::InvalidOAuthProvider ); } + +#[tokio::test] +async fn concurrent_user_sign_up_test() { + let mut tasks = Vec::new(); + for _i in 0..50 { + let task = tokio::spawn(async move { + let _ = TestClient::new_user().await; + tokio::time::sleep(Duration::from_millis(300)).await; + }); + tasks.push(task); + } + + let results = futures::future::join_all(tasks).await; + for result in results { + assert!(result.is_ok(), "Task completed successfully"); + } +}