2023-05-21 11:13:22 +08:00
|
|
|
use std::env::temp_dir;
|
2023-07-05 20:57:09 +08:00
|
|
|
use std::path::PathBuf;
|
2023-05-23 23:55:21 +08:00
|
|
|
use std::sync::Arc;
|
2022-10-20 11:35:11 +08:00
|
|
|
|
2023-06-07 00:05:27 +08:00
|
|
|
use nanoid::nanoid;
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
|
2023-11-17 15:38:56 +08:00
|
|
|
use flowy_core::config::AppFlowyCoreConfig;
|
|
|
|
use flowy_core::AppFlowyCore;
|
2023-10-24 20:11:06 +08:00
|
|
|
use flowy_notification::register_notification_sender;
|
|
|
|
use flowy_user::entities::AuthTypePB;
|
2023-05-17 09:49:39 +08:00
|
|
|
|
2023-10-25 15:25:31 +08:00
|
|
|
use crate::user_event::TestNotificationSender;
|
2023-06-07 00:05:27 +08:00
|
|
|
|
2023-10-25 15:25:31 +08:00
|
|
|
pub mod database_event;
|
2023-07-04 17:17:25 +08:00
|
|
|
pub mod document;
|
2023-10-25 15:25:31 +08:00
|
|
|
pub mod document_event;
|
2023-05-17 09:49:39 +08:00
|
|
|
pub mod event_builder;
|
2023-10-25 15:25:31 +08:00
|
|
|
pub mod folder_event;
|
|
|
|
pub mod user_event;
|
2021-07-06 14:14:47 +08:00
|
|
|
|
2021-09-04 15:12:53 +08:00
|
|
|
#[derive(Clone)]
|
2023-10-24 20:11:06 +08:00
|
|
|
pub struct EventIntegrationTest {
|
|
|
|
pub auth_type: Arc<RwLock<AuthTypePB>>,
|
|
|
|
pub inner: AppFlowyCore,
|
2023-08-06 11:51:03 +08:00
|
|
|
#[allow(dead_code)]
|
|
|
|
cleaner: Arc<Cleaner>,
|
2023-07-05 20:57:09 +08:00
|
|
|
pub notification_sender: TestNotificationSender,
|
2022-01-07 17:37:11 +08:00
|
|
|
}
|
2021-12-08 17:33:22 +08:00
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
impl EventIntegrationTest {
|
|
|
|
pub async fn new() -> Self {
|
2023-08-09 12:43:03 +08:00
|
|
|
let temp_dir = temp_dir().join(nanoid!(6));
|
2023-08-06 11:51:03 +08:00
|
|
|
std::fs::create_dir_all(&temp_dir).unwrap();
|
2023-10-30 12:35:06 +08:00
|
|
|
Self::new_with_user_data_path(temp_dir, nanoid!(6)).await
|
2023-08-03 09:14:52 +08:00
|
|
|
}
|
2023-10-30 12:35:06 +08:00
|
|
|
pub async fn new_with_user_data_path(path: PathBuf, name: String) -> Self {
|
2023-08-09 12:43:03 +08:00
|
|
|
let config = AppFlowyCoreConfig::new(path.to_str().unwrap(), name).log_filter(
|
2023-10-02 17:22:22 +08:00
|
|
|
"trace",
|
|
|
|
vec![
|
|
|
|
"flowy_test".to_string(),
|
2023-10-30 12:35:06 +08:00
|
|
|
"tokio".to_string(),
|
|
|
|
"lib_dispatch".to_string(),
|
2023-10-02 17:22:22 +08:00
|
|
|
],
|
2023-07-29 09:46:24 +08:00
|
|
|
);
|
2023-07-05 20:57:09 +08:00
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
let inner = init_core(config).await;
|
2023-07-05 20:57:09 +08:00
|
|
|
let notification_sender = TestNotificationSender::new();
|
2023-08-03 09:14:52 +08:00
|
|
|
let auth_type = Arc::new(RwLock::new(AuthTypePB::Local));
|
2023-07-05 20:57:09 +08:00
|
|
|
register_notification_sender(notification_sender.clone());
|
2023-05-23 23:55:21 +08:00
|
|
|
std::mem::forget(inner.dispatcher());
|
2023-07-05 20:57:09 +08:00
|
|
|
Self {
|
|
|
|
inner,
|
|
|
|
auth_type,
|
|
|
|
notification_sender,
|
2023-08-06 11:51:03 +08:00
|
|
|
cleaner: Arc::new(Cleaner(path)),
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-07-06 14:14:47 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
#[cfg(feature = "single_thread")]
|
|
|
|
async fn init_core(config: AppFlowyCoreConfig) -> AppFlowyCore {
|
|
|
|
// let runtime = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
// let local_set = tokio::task::LocalSet::new();
|
|
|
|
// runtime.block_on(AppFlowyCore::new(config))
|
|
|
|
AppFlowyCore::new(config).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "single_thread"))]
|
|
|
|
async fn init_core(config: AppFlowyCoreConfig) -> AppFlowyCore {
|
|
|
|
std::thread::spawn(|| AppFlowyCore::new(config))
|
|
|
|
.join()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2023-10-24 20:11:06 +08:00
|
|
|
impl std::ops::Deref for EventIntegrationTest {
|
2023-05-23 23:55:21 +08:00
|
|
|
type Target = AppFlowyCore;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 16:09:18 +08:00
|
|
|
|
2023-08-03 10:33:25 +08:00
|
|
|
pub struct Cleaner(PathBuf);
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
impl Cleaner {
|
2023-08-03 10:33:25 +08:00
|
|
|
pub fn new(dir: PathBuf) -> Self {
|
2023-07-05 20:57:09 +08:00
|
|
|
Cleaner(dir)
|
|
|
|
}
|
|
|
|
|
2023-08-06 11:51:03 +08:00
|
|
|
fn cleanup(dir: &PathBuf) {
|
|
|
|
let _ = std::fs::remove_dir_all(dir);
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Cleaner {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
Self::cleanup(&self.0)
|
|
|
|
}
|
|
|
|
}
|