mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
sync with server when ws becomes avaliable
This commit is contained in:
@ -90,10 +90,14 @@ path = "src/lib.rs"
|
||||
name = "backend"
|
||||
path = "src/main.rs"
|
||||
|
||||
[features]
|
||||
flowy_test = []
|
||||
|
||||
[dev-dependencies]
|
||||
parking_lot = "0.11"
|
||||
once_cell = "1.7.2"
|
||||
linkify = "0.5.0"
|
||||
backend = { path = ".", features = ["flowy_test"]}
|
||||
flowy-user = { path = "../rust-lib/flowy-user", features = ["http_server"] }
|
||||
flowy-workspace = { path = "../rust-lib/flowy-workspace", features = ["http_server"] }
|
||||
flowy-ws = { path = "../rust-lib/flowy-ws" }
|
||||
|
@ -50,7 +50,7 @@ pub(crate) async fn read_doc(pool: &PgPool, params: QueryDocParams) -> Result<Do
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(pool, params), err)]
|
||||
pub(crate) async fn update_doc(pool: &PgPool, mut params: UpdateDocParams) -> Result<(), ServerError> {
|
||||
pub async fn update_doc(pool: &PgPool, mut params: UpdateDocParams) -> Result<(), ServerError> {
|
||||
let doc_id = Uuid::parse_str(¶ms.doc_id)?;
|
||||
let mut transaction = pool
|
||||
.begin()
|
||||
|
@ -107,7 +107,7 @@ impl EditDocActor {
|
||||
user: user.clone(),
|
||||
socket: socket.clone(),
|
||||
};
|
||||
let _ = ret.send(self.edit_doc.new_connection(user, rev_id, self.pg_pool.clone()).await);
|
||||
let _ = ret.send(self.edit_doc.new_doc_user(user, rev_id).await);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ use crate::service::{
|
||||
};
|
||||
use actix_web::web::Data;
|
||||
|
||||
use crate::service::doc::edit::interval::Interval;
|
||||
use bytes::Bytes;
|
||||
use dashmap::DashMap;
|
||||
use flowy_document::{
|
||||
@ -54,9 +53,18 @@ impl ServerEditDoc {
|
||||
|
||||
pub fn document_json(&self) -> String { self.document.read().to_json() }
|
||||
|
||||
pub async fn new_connection(&self, user: EditUser, rev_id: i64, _pg_pool: Data<PgPool>) -> Result<(), ServerError> {
|
||||
#[tracing::instrument(
|
||||
level = "debug",
|
||||
skip(self, user),
|
||||
fields(
|
||||
user_id = %user.id(),
|
||||
rev_id = %rev_id,
|
||||
)
|
||||
)]
|
||||
pub async fn new_doc_user(&self, user: EditUser, rev_id: i64) -> Result<(), ServerError> {
|
||||
self.users.insert(user.id(), user.clone());
|
||||
let cur_rev_id = self.rev_id.load(SeqCst);
|
||||
|
||||
if cur_rev_id > rev_id {
|
||||
let doc_delta = self.document.read().delta().clone();
|
||||
let cli_revision = self.mk_revision(rev_id, doc_delta);
|
||||
|
@ -1,10 +1,13 @@
|
||||
use crate::document::helper::{DocScript, DocumentTest};
|
||||
use flowy_document::services::doc::{Document, FlowyDoc};
|
||||
use flowy_ot::core::Delta;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn edit_doc_insert_text() {
|
||||
async fn sync_doc_insert_text() {
|
||||
let test = DocumentTest::new().await;
|
||||
test.run_scripts(vec![
|
||||
DocScript::ConnectWs,
|
||||
DocScript::OpenDoc,
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(3, "123"),
|
||||
DocScript::SendText(6, "efg"),
|
||||
@ -15,21 +18,52 @@ async fn edit_doc_insert_text() {
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn edit_doc_insert_large_text() {
|
||||
async fn sync_open_empty_doc_and_sync_from_server() {
|
||||
let test = DocumentTest::new().await;
|
||||
let mut document = Document::new::<FlowyDoc>();
|
||||
document.insert(0, "123").unwrap();
|
||||
document.insert(3, "456").unwrap();
|
||||
let json = document.to_json();
|
||||
|
||||
test.run_scripts(vec![
|
||||
DocScript::ConnectWs,
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::SendText(0, "abc"),
|
||||
/* DocScript::AssertClient(r#"[{"insert":"abc123efg\n"}]"#),
|
||||
* DocScript::AssertServer(r#"[{"insert":"abc123efg\n"}]"#), */
|
||||
DocScript::SetServerDocument(json, 3),
|
||||
DocScript::OpenDoc,
|
||||
DocScript::AssertClient(r#"[{"insert":"123456\n"}]"#),
|
||||
DocScript::AssertServer(r#"[{"insert":"123456\n"}]"#),
|
||||
])
|
||||
.await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn sync_open_empty_doc_and_sync_from_server_using_ws() {
|
||||
let test = DocumentTest::new().await;
|
||||
let mut document = Document::new::<FlowyDoc>();
|
||||
document.insert(0, "123").unwrap();
|
||||
let json = document.to_json();
|
||||
|
||||
test.run_scripts(vec![
|
||||
DocScript::OpenDoc,
|
||||
DocScript::SetServerDocument(json, 3),
|
||||
DocScript::ConnectWs,
|
||||
DocScript::AssertClient(r#"[{"insert":"123\n\n"}]"#),
|
||||
])
|
||||
.await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn sync_open_non_empty_doc_and_sync_with_sever() {
|
||||
let test = DocumentTest::new().await;
|
||||
let mut document = Document::new::<FlowyDoc>();
|
||||
document.insert(0, "123").unwrap();
|
||||
let json = document.to_json();
|
||||
|
||||
test.run_scripts(vec![
|
||||
DocScript::OpenDoc,
|
||||
DocScript::SetServerDocument(json, 3),
|
||||
DocScript::SendText(0, "abc"),
|
||||
DocScript::ConnectWs,
|
||||
DocScript::AssertClient(r#"[{"insert":"123\nabc\n"}]"#),
|
||||
// DocScript::AssertServer(r#"[{"insert":"123\nabc\n"}]"#),
|
||||
])
|
||||
.await;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use futures_util::{stream, stream::StreamExt};
|
||||
use sqlx::PgPool;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
use backend::service::doc::doc::DocManager;
|
||||
use backend::service::doc::{crud::update_doc, doc::DocManager};
|
||||
use flowy_document::{entities::doc::QueryDocParams, services::doc::edit::ClientEditDoc as ClientEditDocContext};
|
||||
use flowy_net::config::ServerConfig;
|
||||
use flowy_test::{workspace::ViewTest, FlowyTest};
|
||||
@ -13,6 +13,10 @@ use flowy_user::services::user::UserSession;
|
||||
|
||||
// use crate::helper::*;
|
||||
use crate::helper::{spawn_server, TestServer};
|
||||
use flowy_document::protobuf::UpdateDocParams;
|
||||
use flowy_ot::core::Delta;
|
||||
use parking_lot::RwLock;
|
||||
use serde::__private::Formatter;
|
||||
|
||||
pub struct DocumentTest {
|
||||
server: TestServer,
|
||||
@ -24,6 +28,22 @@ pub enum DocScript {
|
||||
SendText(usize, &'static str),
|
||||
AssertClient(&'static str),
|
||||
AssertServer(&'static str),
|
||||
SetServerDocument(String, i64), // delta_json, rev_id
|
||||
OpenDoc,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for DocScript {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let name = match self {
|
||||
DocScript::ConnectWs => "ConnectWs",
|
||||
DocScript::SendText(_, _) => "SendText",
|
||||
DocScript::AssertClient(_) => "AssertClient",
|
||||
DocScript::AssertServer(_) => "AssertServer",
|
||||
DocScript::SetServerDocument(_, _) => "SetServerDocument",
|
||||
DocScript::OpenDoc => "OpenDoc",
|
||||
};
|
||||
f.write_str(&format!("******** {} *********", name))
|
||||
}
|
||||
}
|
||||
|
||||
impl DocumentTest {
|
||||
@ -37,54 +57,92 @@ impl DocumentTest {
|
||||
pub async fn run_scripts(self, scripts: Vec<DocScript>) {
|
||||
let _ = self.flowy_test.sign_up().await;
|
||||
let DocumentTest { server, flowy_test } = self;
|
||||
let script_context = ScriptContext {
|
||||
client_edit_context: create_doc(&flowy_test).await,
|
||||
user_session: flowy_test.sdk.user_session.clone(),
|
||||
doc_manager: server.app_ctx.doc_biz.manager.clone(),
|
||||
pool: Data::new(server.pg_pool.clone()),
|
||||
};
|
||||
|
||||
let script_context = Arc::new(RwLock::new(ScriptContext::new(flowy_test, server).await));
|
||||
run_scripts(script_context, scripts).await;
|
||||
std::mem::forget(flowy_test);
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ScriptContext {
|
||||
client_edit_context: Arc<ClientEditDocContext>,
|
||||
client_edit_context: Option<Arc<ClientEditDocContext>>,
|
||||
flowy_test: FlowyTest,
|
||||
user_session: Arc<UserSession>,
|
||||
doc_manager: Arc<DocManager>,
|
||||
pool: Data<PgPool>,
|
||||
doc_id: String,
|
||||
}
|
||||
|
||||
async fn run_scripts(context: ScriptContext, scripts: Vec<DocScript>) {
|
||||
impl ScriptContext {
|
||||
async fn new(flowy_test: FlowyTest, server: TestServer) -> Self {
|
||||
let user_session = flowy_test.sdk.user_session.clone();
|
||||
let doc_id = create_doc(&flowy_test).await;
|
||||
|
||||
Self {
|
||||
client_edit_context: None,
|
||||
flowy_test,
|
||||
user_session,
|
||||
doc_manager: server.app_ctx.doc_biz.manager.clone(),
|
||||
pool: Data::new(server.pg_pool.clone()),
|
||||
doc_id,
|
||||
}
|
||||
}
|
||||
|
||||
async fn open_doc(&mut self) {
|
||||
let flowy_document = self.flowy_test.sdk.flowy_document.clone();
|
||||
let pool = self.user_session.db_pool().unwrap();
|
||||
let doc_id = self.doc_id.clone();
|
||||
|
||||
let edit_context = flowy_document.open(QueryDocParams { doc_id }, pool).await.unwrap();
|
||||
self.client_edit_context = Some(edit_context);
|
||||
}
|
||||
|
||||
fn client_edit_context(&self) -> Arc<ClientEditDocContext> { self.client_edit_context.as_ref().unwrap().clone() }
|
||||
}
|
||||
|
||||
impl Drop for ScriptContext {
|
||||
fn drop(&mut self) {
|
||||
// std::mem::forget(self.flowy_test);
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_scripts(context: Arc<RwLock<ScriptContext>>, scripts: Vec<DocScript>) {
|
||||
let mut fut_scripts = vec![];
|
||||
for script in scripts {
|
||||
let context = context.clone();
|
||||
let fut = async move {
|
||||
let doc_id = context.read().doc_id.clone();
|
||||
match script {
|
||||
DocScript::ConnectWs => {
|
||||
let token = context.user_session.token().unwrap();
|
||||
let _ = context.user_session.start_ws_connection(&token).await.unwrap();
|
||||
// sleep(Duration::from_millis(300)).await;
|
||||
let user_session = context.read().user_session.clone();
|
||||
let token = user_session.token().unwrap();
|
||||
let _ = user_session.start_ws_connection(&token).await.unwrap();
|
||||
},
|
||||
DocScript::OpenDoc => {
|
||||
context.write().open_doc().await;
|
||||
},
|
||||
DocScript::SendText(index, s) => {
|
||||
context.client_edit_context.insert(index, s).await.unwrap();
|
||||
context.read().client_edit_context().insert(index, s).await.unwrap();
|
||||
},
|
||||
DocScript::AssertClient(s) => {
|
||||
let json = context.client_edit_context.doc_json().await.unwrap();
|
||||
sleep(Duration::from_millis(300)).await;
|
||||
let json = context.read().client_edit_context().doc_json().await.unwrap();
|
||||
assert_eq(s, &json);
|
||||
},
|
||||
DocScript::AssertServer(s) => {
|
||||
let edit_doc = context
|
||||
.doc_manager
|
||||
.get(&context.client_edit_context.doc_id, context.pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
sleep(Duration::from_millis(300)).await;
|
||||
|
||||
let pg_pool = context.read().pool.clone();
|
||||
let doc_manager = context.read().doc_manager.clone();
|
||||
let edit_doc = doc_manager.get(&doc_id, pg_pool).await.unwrap().unwrap();
|
||||
let json = edit_doc.document_json().await.unwrap();
|
||||
assert_eq(s, &json);
|
||||
},
|
||||
DocScript::SetServerDocument(json, rev_id) => {
|
||||
let pg_pool = context.read().pool.clone();
|
||||
save_doc(&doc_id, json, rev_id, pg_pool).await;
|
||||
},
|
||||
}
|
||||
};
|
||||
fut_scripts.push(fut);
|
||||
@ -94,6 +152,8 @@ async fn run_scripts(context: ScriptContext, scripts: Vec<DocScript>) {
|
||||
while let Some(script) = stream.next().await {
|
||||
let _ = script.await;
|
||||
}
|
||||
|
||||
std::mem::forget(context);
|
||||
}
|
||||
|
||||
fn assert_eq(expect: &str, receive: &str) {
|
||||
@ -104,16 +164,16 @@ fn assert_eq(expect: &str, receive: &str) {
|
||||
assert_eq!(expect, receive);
|
||||
}
|
||||
|
||||
async fn create_doc(flowy_test: &FlowyTest) -> Arc<ClientEditDocContext> {
|
||||
async fn create_doc(flowy_test: &FlowyTest) -> String {
|
||||
let view_test = ViewTest::new(flowy_test).await;
|
||||
let doc_id = view_test.view.id.clone();
|
||||
let user_session = flowy_test.sdk.user_session.clone();
|
||||
let flowy_document = flowy_test.sdk.flowy_document.clone();
|
||||
|
||||
let edit_context = flowy_document
|
||||
.open(QueryDocParams { doc_id }, user_session.db_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
edit_context
|
||||
doc_id
|
||||
}
|
||||
|
||||
async fn save_doc(doc_id: &str, json: String, rev_id: i64, pool: Data<PgPool>) {
|
||||
let mut params = UpdateDocParams::new();
|
||||
params.set_doc_id(doc_id.to_owned());
|
||||
params.set_data(json);
|
||||
params.set_rev_id(rev_id);
|
||||
let _ = update_doc(pool.get_ref(), params).await.unwrap();
|
||||
}
|
||||
|
Reference in New Issue
Block a user