Skip to content

Commit

Permalink
Merge pull request #98 from sustech-cs304/test-server
Browse files Browse the repository at this point in the history
Fix server
  • Loading branch information
vollate authored Jun 2, 2024
2 parents f125533 + 9d31bc0 commit fa358f1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
18 changes: 9 additions & 9 deletions src-tauri/src/remote/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub mod editor_rpc {
struct ServerHandle {
window: Option<Window>,
map_state: Mutex<(String, Option<Ptr<TabMap>>)>,
version: atomic::AtomicUsize,
password: Mutex<String>,
clients: Mutex<Vec<SocketAddr>>,
cursor_list: Arc<Mutex<CursorList>>,
Expand Down Expand Up @@ -115,12 +114,12 @@ impl ServerHandle {
}
}

fn get_history_since<T>(&self, version: usize) -> Vec<T>
fn get_history_since<T>(&self, start_version: usize) -> Vec<T>
where
Modification: Into<T> + Clone,
{
let lock = self.history.lock().unwrap();
lock[version..]
lock[start_version..]
.to_vec()
.into_iter()
.map(Into::into)
Expand All @@ -143,7 +142,7 @@ impl Editor for Arc<Mutex<ServerHandle>> {
Ok(AuthorizeReply {
success: true,
file_name: tab.text.get_path_str(),
version: handler.version.load(atomic::Ordering::Relaxed) as u64,
version: tab.text.get_version() as u64,
content: tab.text.to_string(),
})
} else {
Expand All @@ -152,7 +151,7 @@ impl Editor for Arc<Mutex<ServerHandle>> {
Ok(AuthorizeReply {
success: true,
file_name: tab.text.get_path_str(),
version: handler.version.load(atomic::Ordering::Relaxed) as u64,
version: tab.text.get_version() as u64,
content: tab.text.to_string(),
})
}
Expand Down Expand Up @@ -226,9 +225,7 @@ impl Editor for Arc<Mutex<ServerHandle>> {
history: vec![],
full_content: tab.text.to_string(),
});
} else if request.get_ref().version
== handler.version.load(atomic::Ordering::Relaxed) as u64
{
} else if request.get_ref().version == tab.text.get_version() as u64 {
Ok(GetContentReply {
history: vec![],
full_content: String::new(),
Expand Down Expand Up @@ -263,7 +260,7 @@ impl Editor for Arc<Mutex<ServerHandle>> {
let end = content_position.end.unwrap();

// check version correct(up to date)
if request_ref.version != handler.version.load(atomic::Ordering::Relaxed) as u64 {
if request_ref.version != tab.text.get_version() as u64 {
return Ok(Response::new(UpdateContentReply {
success: false,
message: "Version mismatch".to_string(),
Expand All @@ -283,6 +280,9 @@ impl Editor for Arc<Mutex<ServerHandle>> {
// }
// }

let mut history = handler.history.lock().unwrap();
history.append(&mut vec![Modification::from(request_ref.clone())]);

// handle operation
match tab.text.merge_history(
&vec![Modification::from(request_ref.clone())],
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/tests/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static TEST_FILE_NAME: &str = "/foo/bar/test_file.txt";

static TEST_PASSWD: &str = "wasd";

static MAX_PORT_RETRY: usize = 1145;
static MAX_PORT_RETRY: usize = 114514;

static TAB_MAP: Lazy<Mutex<Option<TabMap>>> = Lazy::new(|| Mutex::new(None));

Expand Down
28 changes: 23 additions & 5 deletions src-tauri/src/tests/remote/server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod local_test {
#[test]
fn test_authorize_disconnect() {
let mut server = init_test_server(TEST_FILE_CONTENT).unwrap();
thread::sleep(Duration::from_secs(5));
thread::sleep(Duration::from_secs(2));
let mut client = init_test_client(server.get_port()).unwrap();

let (filename, version, content) = block_on(client.send_authorize(TEST_PASSWD)).unwrap();
Expand All @@ -29,14 +29,14 @@ mod local_test {

let _ = block_on(client.send_disconnect()).unwrap();
client.stop().unwrap();
thread::sleep(Duration::from_secs(5));
thread::sleep(Duration::from_secs(2));
server.stop_server();
}

#[test]
fn test_update_content() {
fn test_update_and_get_content() {
let mut server = init_test_server(TEST_FILE_CONTENT).unwrap();
thread::sleep(Duration::from_secs(5));
thread::sleep(Duration::from_secs(2));
let mut client1 = init_test_client(server.get_port()).unwrap();

let (filename, version, content) = block_on(client1.send_authorize(TEST_PASSWD)).unwrap();
Expand Down Expand Up @@ -67,10 +67,28 @@ mod local_test {
let res = tab.text.get_raw().to_string();
assert_eq!(res, expected);
}
let res = block_on(client1.send_update_content(
0,
&Modification {
op: OperationType::Insert,
version: 0,
op_range: OpRange {
start: CursorPosition { row: 0, col: 5 },
end: CursorPosition { row: 0, col: 0 },
},
modified_content: "Bar".to_string(),
},
));
assert_eq!(res.as_ref().unwrap().success, false);

let res = block_on(client1.send_get_content(0));
assert_eq!(res.as_ref().unwrap().history.len(), 1);
assert_eq!(res.as_ref().unwrap().history[0].version, 0);
assert_eq!(res.as_ref().unwrap().history[0].modified_content, "Test");

let _ = block_on(client1.send_disconnect());
client1.stop().unwrap();
thread::sleep(Duration::from_secs(5));
thread::sleep(Duration::from_secs(2));
server.stop_server();
}
}

0 comments on commit fa358f1

Please sign in to comment.