Skip to content

new: added DataSrc and several related functions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/async_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use std::collections::HashMap;
use std::thread;

use crate::errors;

use crate::Err;

/// The trait to execute added functions asynchronously.
/// Executes added functions asynchronously.
///
/// This trait is used as an argument of DaxSrc#setup, DaxConn#commit, DacConn#rollback, and
/// DaxConn#forceback.
Expand Down Expand Up @@ -42,8 +44,7 @@ impl AsyncGroupAsync<'_> {
}
}

pub(crate) fn wait(&mut self) -> HashMap<String, Err> {
let mut err_map = HashMap::new();
pub(crate) fn wait(&mut self, err_map: &mut HashMap<String, Err>) {
while self.join_handles.len() > 0 {
let name = self.names.remove(0);
match self.join_handles.remove(0).join() {
Expand All @@ -60,11 +61,15 @@ impl AsyncGroupAsync<'_> {
None => "Thread panicked!",
},
};
err_map.insert(name, Err::new(msg.to_string()));
err_map.insert(
name,
Err::new(errors::AsyncGroup::ThreadPanicked {
message: msg.to_string(),
}),
);
}
}
}
err_map
}
}

Expand Down Expand Up @@ -101,7 +106,8 @@ mod tests_async_group {
#[test]
fn when_zero_function() {
let mut ag = AsyncGroupAsync::new();
let hm = ag.wait();
let mut hm = HashMap::new();
ag.wait(&mut hm);
assert_eq!(hm.len(), 0);
}

Expand All @@ -110,7 +116,8 @@ mod tests_async_group {
let mut ag = AsyncGroupAsync::new();
ag.name = "foo";
ag.add(|| Ok(()));
let hm = ag.wait();
let mut hm = HashMap::new();
ag.wait(&mut hm);
assert_eq!(hm.len(), 0);
}

Expand All @@ -127,7 +134,8 @@ mod tests_async_group {
thread::sleep(time::Duration::from_millis(10));
Ok(())
});
let hm = ag.wait();
let mut hm = HashMap::new();
ag.wait(&mut hm);
assert_eq!(hm.len(), 0);
}

Expand All @@ -142,7 +150,8 @@ mod tests_async_group {
let mut ag = AsyncGroupAsync::new();
ag.name = "foo";
ag.add(|| Err(Err::new(Reasons::BadNumber(123u32))));
let hm = ag.wait();
let mut hm = HashMap::new();
ag.wait(&mut hm);
assert_eq!(hm.len(), 1);
assert_eq!(
*(hm.get("foo").unwrap().reason::<Reasons>().unwrap()),
Expand All @@ -163,7 +172,8 @@ mod tests_async_group {
thread::sleep(time::Duration::from_millis(10));
Err(Err::new(Reasons::BadString("hello".to_string())))
});
let hm = ag.wait();
let mut hm = HashMap::new();
ag.wait(&mut hm);
assert_eq!(hm.len(), 2);
assert_eq!(
*(hm.get("foo").unwrap().reason::<Reasons>().unwrap()),
Expand All @@ -183,12 +193,19 @@ mod tests_async_group {
thread::sleep(time::Duration::from_millis(20));
panic!("panic 1");
});
let hm = ag.wait();
let mut hm = HashMap::new();
ag.wait(&mut hm);
assert_eq!(hm.len(), 1);
assert_eq!(
*(hm.get("foo").unwrap().reason::<String>().unwrap()),
"panic 1"
);

match hm
.get("foo")
.unwrap()
.reason::<errors::AsyncGroup>()
.unwrap()
{
errors::AsyncGroup::ThreadPanicked { message } => assert_eq!(message, "panic 1"),
_ => panic!(),
}
}
}

Expand Down
Loading
Loading