Skip to content

Commit b006361

Browse files
committed
new: added DacSrc and several related functions
1 parent 20da2c5 commit b006361

File tree

4 files changed

+623
-16
lines changed

4 files changed

+623
-16
lines changed

src/async_group.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use std::collections::HashMap;
66
use std::thread;
77

8+
use crate::errors;
9+
810
use crate::Err;
911

10-
/// The trait to execute added functions asynchronously.
12+
/// Executes added functions asynchronously.
1113
///
1214
/// This trait is used as an argument of DaxSrc#setup, DaxConn#commit, DacConn#rollback, and
1315
/// DaxConn#forceback.
@@ -42,8 +44,7 @@ impl AsyncGroupAsync<'_> {
4244
}
4345
}
4446

45-
pub(crate) fn wait(&mut self) -> HashMap<String, Err> {
46-
let mut err_map = HashMap::new();
47+
pub(crate) fn wait(&mut self, err_map: &mut HashMap<String, Err>) {
4748
while self.join_handles.len() > 0 {
4849
let name = self.names.remove(0);
4950
match self.join_handles.remove(0).join() {
@@ -60,11 +61,15 @@ impl AsyncGroupAsync<'_> {
6061
None => "Thread panicked!",
6162
},
6263
};
63-
err_map.insert(name, Err::new(msg.to_string()));
64+
err_map.insert(
65+
name,
66+
Err::new(errors::AsyncGroup::ThreadPanicked {
67+
message: msg.to_string(),
68+
}),
69+
);
6470
}
6571
}
6672
}
67-
err_map
6873
}
6974
}
7075

@@ -101,7 +106,8 @@ mod tests_async_group {
101106
#[test]
102107
fn when_zero_function() {
103108
let mut ag = AsyncGroupAsync::new();
104-
let hm = ag.wait();
109+
let mut hm = HashMap::new();
110+
ag.wait(&mut hm);
105111
assert_eq!(hm.len(), 0);
106112
}
107113

@@ -110,7 +116,8 @@ mod tests_async_group {
110116
let mut ag = AsyncGroupAsync::new();
111117
ag.name = "foo";
112118
ag.add(|| Ok(()));
113-
let hm = ag.wait();
119+
let mut hm = HashMap::new();
120+
ag.wait(&mut hm);
114121
assert_eq!(hm.len(), 0);
115122
}
116123

@@ -127,7 +134,8 @@ mod tests_async_group {
127134
thread::sleep(time::Duration::from_millis(10));
128135
Ok(())
129136
});
130-
let hm = ag.wait();
137+
let mut hm = HashMap::new();
138+
ag.wait(&mut hm);
131139
assert_eq!(hm.len(), 0);
132140
}
133141

@@ -142,7 +150,8 @@ mod tests_async_group {
142150
let mut ag = AsyncGroupAsync::new();
143151
ag.name = "foo";
144152
ag.add(|| Err(Err::new(Reasons::BadNumber(123u32))));
145-
let hm = ag.wait();
153+
let mut hm = HashMap::new();
154+
ag.wait(&mut hm);
146155
assert_eq!(hm.len(), 1);
147156
assert_eq!(
148157
*(hm.get("foo").unwrap().reason::<Reasons>().unwrap()),
@@ -163,7 +172,8 @@ mod tests_async_group {
163172
thread::sleep(time::Duration::from_millis(10));
164173
Err(Err::new(Reasons::BadString("hello".to_string())))
165174
});
166-
let hm = ag.wait();
175+
let mut hm = HashMap::new();
176+
ag.wait(&mut hm);
167177
assert_eq!(hm.len(), 2);
168178
assert_eq!(
169179
*(hm.get("foo").unwrap().reason::<Reasons>().unwrap()),
@@ -183,12 +193,19 @@ mod tests_async_group {
183193
thread::sleep(time::Duration::from_millis(20));
184194
panic!("panic 1");
185195
});
186-
let hm = ag.wait();
196+
let mut hm = HashMap::new();
197+
ag.wait(&mut hm);
187198
assert_eq!(hm.len(), 1);
188-
assert_eq!(
189-
*(hm.get("foo").unwrap().reason::<String>().unwrap()),
190-
"panic 1"
191-
);
199+
200+
match hm
201+
.get("foo")
202+
.unwrap()
203+
.reason::<errors::AsyncGroup>()
204+
.unwrap()
205+
{
206+
errors::AsyncGroup::ThreadPanicked { message } => assert_eq!(message, "panic 1"),
207+
_ => panic!(),
208+
}
192209
}
193210
}
194211

0 commit comments

Comments
 (0)