Skip to content

Commit

Permalink
new: added DacSrc and several related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Dec 30, 2024
1 parent 20da2c5 commit b006361
Show file tree
Hide file tree
Showing 4 changed files with 623 additions and 16 deletions.
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

0 comments on commit b006361

Please sign in to comment.