fix: copy_to_diff_dir_doesnt_dupe_files hangs

Closes: #285
This commit is contained in:
Josh Megnauth 2024-07-22 23:23:47 -04:00
parent ec4d7341d7
commit 68288fc9c1
No known key found for this signature in database
GPG key ID: 70813183462EFAD3

View file

@ -575,19 +575,19 @@ mod tests {
path::PathBuf, path::PathBuf,
}; };
use cosmic::iced::futures::channel::mpsc; use cosmic::iced::futures::{channel::mpsc, StreamExt};
use log::{debug, trace}; use log::{debug, trace};
use test_log::test; use test_log::test;
use tokio::sync; use tokio::sync;
use super::Operation; use super::{Operation, ReplaceResult};
use crate::{ use crate::{
app::{ app::{
test_utils::{ test_utils::{
empty_fs, filter_dirs, filter_files, read_dir_sorted, simple_fs, NAME_LEN, empty_fs, filter_dirs, filter_files, read_dir_sorted, simple_fs, NAME_LEN,
NUM_DIRS, NUM_FILES, NUM_HIDDEN, NUM_NESTED, NUM_DIRS, NUM_FILES, NUM_HIDDEN, NUM_NESTED,
}, },
Message, DialogPage, Message,
}, },
fl, fl,
}; };
@ -599,25 +599,32 @@ mod tests {
pub async fn operation_copy(paths: Vec<PathBuf>, to: PathBuf) -> Result<(), String> { pub async fn operation_copy(paths: Vec<PathBuf>, to: PathBuf) -> Result<(), String> {
let id = fastrand::u64(0..u64::MAX); let id = fastrand::u64(0..u64::MAX);
let (tx, mut rx) = mpsc::channel(BUF_SIZE); let (tx, mut rx) = mpsc::channel(BUF_SIZE);
Operation::Copy { let paths_clone = paths.clone();
paths: paths.clone(), let to_clone = to.clone();
to: to.clone(), let handle_copy = tokio::spawn(async move {
} Operation::Copy {
.perform(id, &sync::Mutex::new(tx).into()) paths: paths_clone,
.await?; to: to_clone,
}
.perform(id, &sync::Mutex::new(tx).into())
.await
});
loop { while let Some(msg) = rx.next().await {
match rx.try_next() { match msg {
Ok(Some(Message::PendingProgress(id, progress))) => { Message::PendingProgress(id, progress) => {
trace!("({id}) [ {paths:?} => {to:?} ] {progress}% complete)") trace!("({id}) [ {paths:?} => {to:?} ] {progress}% complete)")
} }
Ok(None) => break, Message::DialogPush(DialogPage::Replace { tx, .. }) => {
Err(e) => panic!("Receiving message from operation should succeed: {e:?}"), debug!("[{id}] Replace request");
_ => unreachable!("Only `Message::PendingProgress` is sent from operation"), tx.send(ReplaceResult::Cancel).await.expect("Sending a response to a replace request should succeed")
}
_ => unreachable!("Only [ `Message::PendingProgress`, `Message::DialogPush(DialogPage::Replace)` ] are sent from operation"),
} }
} }
Ok(()) handle_copy.await.unwrap()
} }
#[test(tokio::test)] #[test(tokio::test)]
@ -752,8 +759,8 @@ mod tests {
); );
operation_copy(vec![first_file.clone()], second_dir.clone()) operation_copy(vec![first_file.clone()], second_dir.clone())
.await .await
.expect_err( .expect(
"Copy operation should have failed because we're copying to different directories", "Copy operation should have been cancelled because we're copying to different directories without replacement",
); );
assert!( assert!(
first_dir.join(base_name).exists(), first_dir.join(base_name).exists(),