Browse Source

[rust]: fix multi thread test bugs

appflowy 3 years ago
parent
commit
635f6b8607

+ 1 - 1
rust-lib/dart-ffi/src/lib.rs

@@ -25,7 +25,7 @@ pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
     let path: &str = c_str.to_str().unwrap();
 
     let server_config = ServerConfig::default();
-    let config = FlowySDKConfig::new(path, server_config).log_filter("debug");
+    let config = FlowySDKConfig::new(path, server_config, "AppFlowy").log_filter("debug");
     *FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config)));
 
     return 1;

+ 5 - 2
rust-lib/flowy-sdk/src/lib.rs

@@ -20,14 +20,16 @@ static INIT_LOG: AtomicBool = AtomicBool::new(false);
 
 #[derive(Debug, Clone)]
 pub struct FlowySDKConfig {
+    name: String,
     root: String,
     log_filter: String,
     server_config: ServerConfig,
 }
 
 impl FlowySDKConfig {
-    pub fn new(root: &str, server_config: ServerConfig) -> Self {
+    pub fn new(root: &str, server_config: ServerConfig, name: &str) -> Self {
         FlowySDKConfig {
+            name: name.to_owned(),
             root: root.to_owned(),
             log_filter: crate_log_filter(None),
             server_config,
@@ -70,9 +72,10 @@ impl FlowySDK {
         init_kv(&config.root);
         tracing::debug!("🔥 {:?}", config);
 
+        let session_cache_key = format!("{}_session_cache", &config.name);
         let user_session = Arc::new(
             UserSessionBuilder::new()
-                .root_dir(&config.root, &config.server_config)
+                .root_dir(&config.root, &config.server_config, &session_cache_key)
                 .build(),
         );
         let flowy_document = mk_document_module(user_session.clone(), &config.server_config);

+ 2 - 1
rust-lib/flowy-test/src/lib.rs

@@ -3,6 +3,7 @@ mod helper;
 pub mod workspace;
 
 use crate::helper::*;
+use flowy_infra::uuid;
 use flowy_net::config::ServerConfig;
 use flowy_sdk::{FlowySDK, FlowySDKConfig};
 use flowy_user::entities::UserProfile;
@@ -38,7 +39,7 @@ impl FlowyTest {
     }
 
     pub fn setup_with(server_config: ServerConfig) -> Self {
-        let config = FlowySDKConfig::new(&root_dir(), server_config).log_filter("debug");
+        let config = FlowySDKConfig::new(&root_dir(), server_config, &uuid().to_string()).log_filter("debug");
         let sdk = FlowySDK::new(config);
         Self { sdk }
     }

+ 2 - 2
rust-lib/flowy-user/src/services/user/builder.rs

@@ -8,8 +8,8 @@ pub struct UserSessionBuilder {
 impl UserSessionBuilder {
     pub fn new() -> Self { Self { config: None } }
 
-    pub fn root_dir(mut self, dir: &str, server_config: &ServerConfig) -> Self {
-        self.config = Some(UserSessionConfig::new(dir, server_config));
+    pub fn root_dir(mut self, dir: &str, server_config: &ServerConfig, session_cache_key: &str) -> Self {
+        self.config = Some(UserSessionConfig::new(dir, server_config, session_cache_key));
         self
     }
 

+ 8 - 6
rust-lib/flowy-user/src/services/user/user_session.rs

@@ -36,13 +36,15 @@ pub enum UserStatus {
 pub struct UserSessionConfig {
     root_dir: String,
     server_config: ServerConfig,
+    session_cache_key: String,
 }
 
 impl UserSessionConfig {
-    pub fn new(root_dir: &str, server_config: &ServerConfig) -> Self {
+    pub fn new(root_dir: &str, server_config: &ServerConfig, session_cache_key: &str) -> Self {
         Self {
             root_dir: root_dir.to_owned(),
             server_config: server_config.clone(),
+            session_cache_key: session_cache_key.to_owned(),
         }
     }
 }
@@ -263,8 +265,10 @@ impl UserSession {
     fn set_session(&self, session: Option<Session>) -> Result<(), UserError> {
         tracing::debug!("Set user session: {:?}", session);
         match &session {
-            None => KV::remove(SESSION_CACHE_KEY).map_err(|e| UserError::new(ErrorCode::InternalError, &e))?,
-            Some(session) => KV::set_str(SESSION_CACHE_KEY, session.clone().into()),
+            None => {
+                KV::remove(&self.config.session_cache_key).map_err(|e| UserError::new(ErrorCode::InternalError, &e))?
+            },
+            Some(session) => KV::set_str(&self.config.session_cache_key, session.clone().into()),
         }
         *self.session.write() = session;
         Ok(())
@@ -273,7 +277,7 @@ impl UserSession {
     fn get_session(&self) -> Result<Session, UserError> {
         let mut session = { (*self.session.read()).clone() };
         if session.is_none() {
-            match KV::get_str(SESSION_CACHE_KEY) {
+            match KV::get_str(&self.config.session_cache_key) {
                 None => {},
                 Some(s) => {
                     session = Some(Session::from(s));
@@ -350,8 +354,6 @@ impl UserDatabaseConnection for UserSession {
     fn get_connection(&self) -> Result<DBConnection, String> { self.db_connection().map_err(|e| format!("{:?}", e)) }
 }
 
-const SESSION_CACHE_KEY: &str = "session_cache_key";
-
 #[derive(Debug, Clone, Default, Serialize, Deserialize)]
 struct Session {
     user_id: String,

+ 1 - 1
rust-lib/flowy-workspace-infra/src/entities/share/export.rs

@@ -34,7 +34,7 @@ pub struct ExportRequest {
     pub export_type: ExportType,
 }
 
-#[derive(Default)]
+#[derive(Default, Debug)]
 pub struct ExportParams {
     pub doc_id: String,
     pub export_type: ExportType,

+ 1 - 1
rust-lib/flowy-workspace/src/services/view_controller.rs

@@ -139,7 +139,7 @@ impl ViewController {
         Ok(())
     }
 
-    #[tracing::instrument(level = "debug", skip(self, params), err)]
+    #[tracing::instrument(level = "debug", skip(self), err)]
     pub(crate) async fn export_doc(&self, _params: ExportParams) -> Result<ExportData, WorkspaceError> {
         unimplemented!()
     }

+ 1 - 1
scripts/makefile/env.toml

@@ -1,5 +1,5 @@
 [tasks.flowy_dev]
-run_task = { name = ["install_targets","install_diesel", "install_protobuf", "install_tools", "install_flutter"] }
+run_task = { name = ["install_targets","install_diesel", "install_protobuf"] }
 
 [tasks.install_diesel]
 script = """