5
5
use std:: collections:: HashMap ;
6
6
use std:: thread;
7
7
8
+ use crate :: errors;
9
+
8
10
use crate :: Err ;
9
11
10
- /// The trait to execute added functions asynchronously.
12
+ /// Executes added functions asynchronously.
11
13
///
12
14
/// This trait is used as an argument of DaxSrc#setup, DaxConn#commit, DacConn#rollback, and
13
15
/// DaxConn#forceback.
@@ -42,8 +44,7 @@ impl AsyncGroupAsync<'_> {
42
44
}
43
45
}
44
46
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 > ) {
47
48
while self . join_handles . len ( ) > 0 {
48
49
let name = self . names . remove ( 0 ) ;
49
50
match self . join_handles . remove ( 0 ) . join ( ) {
@@ -60,11 +61,15 @@ impl AsyncGroupAsync<'_> {
60
61
None => "Thread panicked!" ,
61
62
} ,
62
63
} ;
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
+ ) ;
64
70
}
65
71
}
66
72
}
67
- err_map
68
73
}
69
74
}
70
75
@@ -101,7 +106,8 @@ mod tests_async_group {
101
106
#[ test]
102
107
fn when_zero_function ( ) {
103
108
let mut ag = AsyncGroupAsync :: new ( ) ;
104
- let hm = ag. wait ( ) ;
109
+ let mut hm = HashMap :: new ( ) ;
110
+ ag. wait ( & mut hm) ;
105
111
assert_eq ! ( hm. len( ) , 0 ) ;
106
112
}
107
113
@@ -110,7 +116,8 @@ mod tests_async_group {
110
116
let mut ag = AsyncGroupAsync :: new ( ) ;
111
117
ag. name = "foo" ;
112
118
ag. add ( || Ok ( ( ) ) ) ;
113
- let hm = ag. wait ( ) ;
119
+ let mut hm = HashMap :: new ( ) ;
120
+ ag. wait ( & mut hm) ;
114
121
assert_eq ! ( hm. len( ) , 0 ) ;
115
122
}
116
123
@@ -127,7 +134,8 @@ mod tests_async_group {
127
134
thread:: sleep ( time:: Duration :: from_millis ( 10 ) ) ;
128
135
Ok ( ( ) )
129
136
} ) ;
130
- let hm = ag. wait ( ) ;
137
+ let mut hm = HashMap :: new ( ) ;
138
+ ag. wait ( & mut hm) ;
131
139
assert_eq ! ( hm. len( ) , 0 ) ;
132
140
}
133
141
@@ -142,7 +150,8 @@ mod tests_async_group {
142
150
let mut ag = AsyncGroupAsync :: new ( ) ;
143
151
ag. name = "foo" ;
144
152
ag. add ( || Err ( Err :: new ( Reasons :: BadNumber ( 123u32 ) ) ) ) ;
145
- let hm = ag. wait ( ) ;
153
+ let mut hm = HashMap :: new ( ) ;
154
+ ag. wait ( & mut hm) ;
146
155
assert_eq ! ( hm. len( ) , 1 ) ;
147
156
assert_eq ! (
148
157
* ( hm. get( "foo" ) . unwrap( ) . reason:: <Reasons >( ) . unwrap( ) ) ,
@@ -163,7 +172,8 @@ mod tests_async_group {
163
172
thread:: sleep ( time:: Duration :: from_millis ( 10 ) ) ;
164
173
Err ( Err :: new ( Reasons :: BadString ( "hello" . to_string ( ) ) ) )
165
174
} ) ;
166
- let hm = ag. wait ( ) ;
175
+ let mut hm = HashMap :: new ( ) ;
176
+ ag. wait ( & mut hm) ;
167
177
assert_eq ! ( hm. len( ) , 2 ) ;
168
178
assert_eq ! (
169
179
* ( hm. get( "foo" ) . unwrap( ) . reason:: <Reasons >( ) . unwrap( ) ) ,
@@ -183,12 +193,19 @@ mod tests_async_group {
183
193
thread:: sleep ( time:: Duration :: from_millis ( 20 ) ) ;
184
194
panic ! ( "panic 1" ) ;
185
195
} ) ;
186
- let hm = ag. wait ( ) ;
196
+ let mut hm = HashMap :: new ( ) ;
197
+ ag. wait ( & mut hm) ;
187
198
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
+ }
192
209
}
193
210
}
194
211
0 commit comments