From 74b583bc624e5383977f48d03ccf496fcc391ca0 Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:57:15 +0800 Subject: [PATCH] refactor: test directory (#88) * refactor: test directory * chore: rename --- libs/client-api/src/http.rs | 6 +++--- libs/realtime/src/collaborate/retry.rs | 19 ++++++++----------- libs/realtime/src/collaborate/server.rs | 16 +++++++--------- tests/client/constants.rs | 3 --- tests/collab/storage_test.rs | 2 +- .../file_test.rs} | 14 +++++--------- tests/file_storage/mod.rs | 1 + tests/gotrue/admin.rs | 6 ++---- tests/gotrue/settings.rs | 2 +- tests/main.rs | 10 +++++++--- tests/realtime/connect_test.rs | 2 +- tests/realtime/offline_edit_collab_test.rs | 2 +- tests/realtime/test_client.rs | 2 +- tests/{client => user}/mod.rs | 2 -- tests/{client => user}/refresh.rs | 2 +- tests/{client => user}/sign_in.rs | 2 +- tests/{client => user}/sign_out.rs | 2 +- tests/{client => user}/sign_up.rs | 2 +- tests/{client => user}/update.rs | 2 +- tests/{client => user}/utils.rs | 0 tests/{client => user}/workspace.rs | 2 +- 21 files changed, 44 insertions(+), 55 deletions(-) delete mode 100644 tests/client/constants.rs rename tests/{client/file_storage.rs => file_storage/file_test.rs} (82%) create mode 100644 tests/file_storage/mod.rs rename tests/{client => user}/mod.rs (71%) rename tests/{client => user}/refresh.rs (93%) rename tests/{client => user}/sign_in.rs (97%) rename tests/{client => user}/sign_out.rs (81%) rename tests/{client => user}/sign_up.rs (95%) rename tests/{client => user}/update.rs (94%) rename tests/{client => user}/utils.rs (100%) rename tests/{client => user}/workspace.rs (96%) diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index bd2fc2bc..84157aa1 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -513,10 +513,10 @@ impl Client { Ok(format!("{}/{}/{}", self.ws_addr, access_token, device_id)) } - pub async fn put_file_storage_object( + pub async fn put_file_storage_object>( &self, path: &str, - data: Bytes, + data: T, mime: &Mime, ) -> Result<(), AppError> { let url = format!("{}/api/file_storage/{}", self.base_url, path); @@ -524,7 +524,7 @@ impl Client { .http_client_with_auth(Method::PUT, &url) .await? .header(header::CONTENT_TYPE, mime.to_string()) - .body(data) + .body(data.into()) .send() .await?; AppResponse::<()>::from_response(resp).await?.into_error() diff --git a/libs/realtime/src/collaborate/retry.rs b/libs/realtime/src/collaborate/retry.rs index 93993b5a..b2339b25 100644 --- a/libs/realtime/src/collaborate/retry.rs +++ b/libs/realtime/src/collaborate/retry.rs @@ -23,22 +23,21 @@ use crate::collaborate::group::CollabGroupCache; use crate::error::RealtimeError; use tracing::{error, trace, warn}; -pub(crate) struct SubscribeGroupIfNeedAction<'a, U, S> { +pub(crate) struct SubscribeGroupIfNeed<'a, U, S> { pub(crate) client_msg: &'a ClientMessage, pub(crate) groups: &'a Arc>, pub(crate) edit_collab_by_user: &'a Arc>>>, pub(crate) client_stream_by_user: &'a Arc>>, } -impl<'a, U, S> SubscribeGroupIfNeedAction<'a, U, S> +impl<'a, U, S> SubscribeGroupIfNeed<'a, U, S> where U: RealtimeUser, S: CollabStorage, { pub(crate) fn run( self, - ) -> RetryIf, SubscribeGroupIfNeedAction<'a, U, S>, SubscribeGroupCondition> - { + ) -> RetryIf, SubscribeGroupIfNeed<'a, U, S>, SubscribeGroupCondition> { let weak_client_stream = Arc::downgrade(self.client_stream_by_user); let retry_strategy = FixedInterval::new(Duration::from_secs(2)).take(5); RetryIf::spawn( @@ -49,7 +48,7 @@ where } } -impl<'a, U, S> Action for SubscribeGroupIfNeedAction<'a, U, S> +impl<'a, U, S> Action for SubscribeGroupIfNeed<'a, U, S> where U: RealtimeUser, S: CollabStorage, @@ -87,7 +86,7 @@ where } } - // If the client's stream is already subscribed to the collab group, return. + // If the client's stream is already subscribe to the collab, return. if self .groups .contains_user(object_id, &self.client_msg.user) @@ -136,11 +135,9 @@ where }); let (sink, stream) = client_stream - .client_channel::( - object_id, - move |object_id, msg| msg.object_id() == object_id, - move |object_id, msg| msg.object_id == object_id, - ) + .client_channel::(object_id, move |object_id, msg| { + msg.object_id() == object_id + }) .unwrap(); collab_group diff --git a/libs/realtime/src/collaborate/server.rs b/libs/realtime/src/collaborate/server.rs index 037f686a..5f2f91a1 100644 --- a/libs/realtime/src/collaborate/server.rs +++ b/libs/realtime/src/collaborate/server.rs @@ -17,7 +17,7 @@ use tracing::{info, trace}; use crate::client::ClientWSSink; use crate::collaborate::group::CollabGroupCache; -use crate::collaborate::retry::SubscribeGroupIfNeedAction; +use crate::collaborate::retry::SubscribeGroupIfNeed; use crate::util::channel_ext::UnboundedSenderSink; use database::collab::CollabStorage; @@ -149,7 +149,7 @@ where let edit_collab_by_user = self.editing_collab_by_user.clone(); Box::pin(async move { - SubscribeGroupIfNeedAction { + SubscribeGroupIfNeed { client_msg: &client_msg, groups: &groups, edit_collab_by_user: &edit_collab_by_user, @@ -158,14 +158,14 @@ where .run() .await?; - forward_message_to_collab_group(&client_msg, &client_stream_by_user).await; + broadcast_message(&client_msg, &client_stream_by_user).await; Ok(()) }) } } #[inline] -async fn forward_message_to_collab_group( +async fn broadcast_message( client_msg: &ClientMessage, client_streams: &Arc>>, ) where @@ -247,11 +247,10 @@ impl CollabClientStream { /// Returns a [UnboundedSenderSink] and a [ReceiverStream] for the object_id. #[allow(clippy::type_complexity)] - pub fn client_channel( + pub fn client_channel( &mut self, object_id: &str, sink_filter: F1, - stream_filter: F2, ) -> Option<( UnboundedSenderSink, ReceiverStream>, @@ -260,7 +259,6 @@ impl CollabClientStream { T: TryFrom + Into + Send + Sync + 'static, F1: Fn(&str, &T) -> bool + Send + Sync + 'static, - F2: Fn(&str, &RealtimeMessage) -> bool + Send + Sync + 'static, { let client_ws_sink = self.ws_sink.clone(); let mut stream_rx = BroadcastStream::new(self.stream_tx.subscribe()); @@ -277,13 +275,13 @@ impl CollabClientStream { }); let client_forward_sink = UnboundedSenderSink::::new(tx); - // forward the message to the stream that can be subscribed by the broadcast group, which will + // forward the message to the stream that was subscribed by the broadcast group, which will // send the messages to all connected clients using the client_forward_sink let cloned_object_id = object_id.to_string(); let (tx, rx) = tokio::sync::mpsc::channel(100); tokio::spawn(async move { while let Some(Ok(Ok(msg))) = stream_rx.next().await { - if stream_filter(&cloned_object_id, &msg) { + if cloned_object_id == msg.object_id { let _ = tx.send(T::try_from(msg)).await; } } diff --git a/tests/client/constants.rs b/tests/client/constants.rs deleted file mode 100644 index b09cf08c..00000000 --- a/tests/client/constants.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub const LOCALHOST_URL: &str = "http://localhost:8000"; -pub const LOCALHOST_WS: &str = "ws://localhost:8000/ws"; -pub const LOCALHOST_GOTRUE: &str = "http://localhost:9998"; diff --git a/tests/collab/storage_test.rs b/tests/collab/storage_test.rs index 566ad463..c803ac62 100644 --- a/tests/collab/storage_test.rs +++ b/tests/collab/storage_test.rs @@ -1,5 +1,5 @@ use crate::{ - client::utils::generate_unique_registered_user_client, collab::workspace_id_from_client, + collab::workspace_id_from_client, user::utils::generate_unique_registered_user_client, }; use collab_define::CollabType; diff --git a/tests/client/file_storage.rs b/tests/file_storage/file_test.rs similarity index 82% rename from tests/client/file_storage.rs rename to tests/file_storage/file_test.rs index d00dea34..3822d6ed 100644 --- a/tests/client/file_storage.rs +++ b/tests/file_storage/file_test.rs @@ -1,6 +1,6 @@ use shared_entity::error_code::ErrorCode; -use crate::client::utils::generate_unique_registered_user_client; +use crate::user::utils::generate_unique_registered_user_client; #[tokio::test] async fn get_but_not_exists() { @@ -18,9 +18,7 @@ async fn put_and_get() { let mime = mime::TEXT_PLAIN_UTF_8; let data = "hello world"; let path = "mydata"; - c1.put_file_storage_object(path, data.into(), &mime) - .await - .unwrap(); + c1.put_file_storage_object(path, data, &mime).await.unwrap(); let got_data = c1.get_file_storage_object(path).await.unwrap(); assert_eq!(got_data, data.as_bytes()); @@ -33,10 +31,10 @@ async fn put_and_put_and_get() { let data1 = "my content 1"; let data2 = "my content 2"; let path = "mydata"; - c1.put_file_storage_object(path, data1.into(), &mime) + c1.put_file_storage_object(path, data1, &mime) .await .unwrap(); - c1.put_file_storage_object(path, data2.into(), &mime) + c1.put_file_storage_object(path, data2, &mime) .await .unwrap(); @@ -60,9 +58,7 @@ async fn put_delete_get() { let mime = mime::TEXT_PLAIN_UTF_8; let data = "my contents"; let path = "mydata"; - c1.put_file_storage_object(path, data.into(), &mime) - .await - .unwrap(); + c1.put_file_storage_object(path, data, &mime).await.unwrap(); c1.delete_file_storage_object(path).await.unwrap(); let err = c1.get_file_storage_object(path).await.unwrap_err(); diff --git a/tests/file_storage/mod.rs b/tests/file_storage/mod.rs new file mode 100644 index 00000000..eefccb1b --- /dev/null +++ b/tests/file_storage/mod.rs @@ -0,0 +1 @@ +mod file_test; diff --git a/tests/gotrue/admin.rs b/tests/gotrue/admin.rs index 886b4c59..2b400a23 100644 --- a/tests/gotrue/admin.rs +++ b/tests/gotrue/admin.rs @@ -6,11 +6,9 @@ use gotrue::{ }; use crate::{ - client::{ - constants::LOCALHOST_GOTRUE, - utils::{generate_unique_email, ADMIN_USER}, - }, client_api_client, + user::utils::{generate_unique_email, ADMIN_USER}, + LOCALHOST_GOTRUE, }; #[tokio::test] diff --git a/tests/gotrue/settings.rs b/tests/gotrue/settings.rs index 64eeebf9..f2c2a6b3 100644 --- a/tests/gotrue/settings.rs +++ b/tests/gotrue/settings.rs @@ -4,7 +4,7 @@ use gotrue::{ params::AdminUserParams, }; -use crate::client::utils::{generate_unique_email, ADMIN_USER}; +use crate::user::utils::{generate_unique_email, ADMIN_USER}; #[tokio::test] async fn gotrue_settings() { diff --git a/tests/main.rs b/tests/main.rs index 916bb03b..05f5a08d 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,11 +1,15 @@ -use crate::client::constants::{LOCALHOST_URL, LOCALHOST_WS}; -use client::constants::LOCALHOST_GOTRUE; use client_api::Client; -mod client; mod collab; mod gotrue; mod realtime; +mod user; + +mod file_storage; + +pub const LOCALHOST_URL: &str = "http://localhost:8000"; +pub const LOCALHOST_WS: &str = "ws://localhost:8000/ws"; +pub const LOCALHOST_GOTRUE: &str = "http://localhost:9998"; pub fn client_api_client() -> Client { Client::new(LOCALHOST_URL, LOCALHOST_WS, LOCALHOST_GOTRUE) diff --git a/tests/realtime/connect_test.rs b/tests/realtime/connect_test.rs index fba559fd..5e99cf1e 100644 --- a/tests/realtime/connect_test.rs +++ b/tests/realtime/connect_test.rs @@ -1,6 +1,6 @@ use client_api::ws::{ConnectState, WSClient, WSClientConfig}; -use crate::client::utils::generate_unique_registered_user_client; +use crate::user::utils::generate_unique_registered_user_client; #[tokio::test] async fn realtime_connect_test() { diff --git a/tests/realtime/offline_edit_collab_test.rs b/tests/realtime/offline_edit_collab_test.rs index 8c92912f..bcf6431e 100644 --- a/tests/realtime/offline_edit_collab_test.rs +++ b/tests/realtime/offline_edit_collab_test.rs @@ -1,5 +1,5 @@ -use crate::client::utils::generate_unique_registered_user; use crate::realtime::test_client::{assert_client_collab, assert_remote_collab, TestClient}; +use crate::user::utils::generate_unique_registered_user; use std::time::Duration; use collab_define::CollabType; diff --git a/tests/realtime/test_client.rs b/tests/realtime/test_client.rs index 9a33d8f6..2b95605e 100644 --- a/tests/realtime/test_client.rs +++ b/tests/realtime/test_client.rs @@ -17,8 +17,8 @@ use tracing_subscriber::fmt::Subscriber; use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; -use crate::client::utils::{generate_unique_registered_user, User}; use crate::client_api_client; +use crate::user::utils::{generate_unique_registered_user, User}; pub(crate) struct TestClient { pub ws_client: WSClient, diff --git a/tests/client/mod.rs b/tests/user/mod.rs similarity index 71% rename from tests/client/mod.rs rename to tests/user/mod.rs index 9731bb5d..43588f95 100644 --- a/tests/client/mod.rs +++ b/tests/user/mod.rs @@ -1,5 +1,3 @@ -pub mod constants; -mod file_storage; mod refresh; mod sign_in; mod sign_out; diff --git a/tests/client/refresh.rs b/tests/user/refresh.rs similarity index 93% rename from tests/client/refresh.rs rename to tests/user/refresh.rs index 2c4aac55..4f63152a 100644 --- a/tests/client/refresh.rs +++ b/tests/user/refresh.rs @@ -1,6 +1,6 @@ use std::time::SystemTime; -use crate::client::utils::generate_unique_registered_user_client; +use crate::user::utils::generate_unique_registered_user_client; #[tokio::test] async fn refresh_success() { diff --git a/tests/client/sign_in.rs b/tests/user/sign_in.rs similarity index 97% rename from tests/client/sign_in.rs rename to tests/user/sign_in.rs index ed7f6271..ce46d961 100644 --- a/tests/client/sign_in.rs +++ b/tests/user/sign_in.rs @@ -1,7 +1,7 @@ use shared_entity::error_code::ErrorCode; -use crate::client::utils::{generate_unique_email, generate_unique_registered_user, ADMIN_USER}; use crate::client_api_client; +use crate::user::utils::{generate_unique_email, generate_unique_registered_user, ADMIN_USER}; #[tokio::test] async fn sign_in_unknown_user() { diff --git a/tests/client/sign_out.rs b/tests/user/sign_out.rs similarity index 81% rename from tests/client/sign_out.rs rename to tests/user/sign_out.rs index ffcae042..a159fc51 100644 --- a/tests/client/sign_out.rs +++ b/tests/user/sign_out.rs @@ -1,4 +1,4 @@ -use crate::{client::utils::generate_unique_registered_user_client, client_api_client}; +use crate::{client_api_client, user::utils::generate_unique_registered_user_client}; #[tokio::test] async fn sign_out_but_not_sign_in() { diff --git a/tests/client/sign_up.rs b/tests/user/sign_up.rs similarity index 95% rename from tests/client/sign_up.rs rename to tests/user/sign_up.rs index c95e5a82..78540292 100644 --- a/tests/client/sign_up.rs +++ b/tests/user/sign_up.rs @@ -2,8 +2,8 @@ use gotrue_entity::OAuthProvider; use shared_entity::error_code::ErrorCode; use crate::{ - client::utils::{generate_unique_email, generate_unique_registered_user_client}, client_api_client, + user::utils::{generate_unique_email, generate_unique_registered_user_client}, }; #[tokio::test] diff --git a/tests/client/update.rs b/tests/user/update.rs similarity index 94% rename from tests/client/update.rs rename to tests/user/update.rs index 7d19b225..a707fcb3 100644 --- a/tests/client/update.rs +++ b/tests/user/update.rs @@ -1,8 +1,8 @@ use shared_entity::dto::UserUpdateParams; use shared_entity::error_code::ErrorCode; -use crate::client::utils::{generate_unique_email, generate_unique_registered_user_client}; use crate::client_api_client; +use crate::user::utils::{generate_unique_email, generate_unique_registered_user_client}; #[tokio::test] async fn update_but_not_logged_in() { diff --git a/tests/client/utils.rs b/tests/user/utils.rs similarity index 100% rename from tests/client/utils.rs rename to tests/user/utils.rs diff --git a/tests/client/workspace.rs b/tests/user/workspace.rs similarity index 96% rename from tests/client/workspace.rs rename to tests/user/workspace.rs index ce7d5209..f215c505 100644 --- a/tests/client/workspace.rs +++ b/tests/user/workspace.rs @@ -1,6 +1,6 @@ use shared_entity::error_code::ErrorCode; -use crate::client::utils::generate_unique_registered_user_client; +use crate::user::utils::generate_unique_registered_user_client; #[tokio::test] async fn add_workspace_members_not_enough_permission() {