Skip to content

Commit cfefbcf

Browse files
author
jizhuozhi.george
committed
Add Promise support for http callout
fix ci Signed-off-by: jizhuozhi.george <[email protected]>
1 parent 0ad1305 commit cfefbcf

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/promise.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ where
9494

9595
pub fn all_of(promises: Vec<Rc<Self>>) -> Rc<Promise<Vec<T>>> {
9696
let next_promise = Promise::new();
97+
98+
if promises.is_empty() {
99+
next_promise.fulfill(vec![]);
100+
return next_promise;
101+
}
102+
97103
let total = promises.len();
98104
let results = Rc::new(RefCell::new(vec![None; total]));
99105
let remaining = Rc::new(RefCell::new(total));
@@ -186,41 +192,59 @@ mod tests {
186192

187193
#[test]
188194
fn test_promise_fulfill() {
195+
let touched = Rc::new(RefCell::new(false));
196+
let touched_clone = touched.clone();
197+
189198
let promise = Promise::<i32>::new();
190-
let next_promise = promise.then(|result| {
199+
let _next_promise = promise.then(move |result| {
191200
assert_eq!(result, 42);
201+
*touched_clone.borrow_mut() = true;
192202
});
193203

194204
promise.fulfill(42);
205+
assert_eq!(true, touched.take())
195206
}
196207

197208
#[test]
198209
fn test_promise_reject() {
210+
let touched = Rc::new(RefCell::new(false));
211+
let touched_clone = touched.clone();
212+
199213
let promise = Promise::<String>::new();
200-
let next_promise = promise.catch(|err| {
214+
let _next_promise = promise.catch(move |err| {
201215
assert_eq!(err, "Error");
216+
*touched_clone.borrow_mut() = true;
202217
});
203218

204219
promise.reject("Error".to_string());
220+
assert_eq!(true, touched.take())
205221
}
206222

207223
#[test]
208224
fn test_promise_chain() {
225+
let touched = Rc::new(RefCell::new(false));
226+
let touched_clone = touched.clone();
227+
209228
let promise = Promise::<i32>::new();
210229
let next_promise = promise.then(|result| {
211230
assert_eq!(result, 10);
212231
20
213232
});
214233

215-
next_promise.then(|result| {
234+
next_promise.then(move |result| {
216235
assert_eq!(result, 20);
236+
*touched_clone.borrow_mut() = true;
217237
});
218238

219239
promise.fulfill(10);
240+
assert_eq!(true, touched.take())
220241
}
221242

222243
#[test]
223244
fn test_all_of_success() {
245+
let touched = Rc::new(RefCell::new(false));
246+
let touched_clone = touched.clone();
247+
224248
let promise1 = Promise::<i32>::new();
225249
let promise2 = Promise::<i32>::new();
226250

@@ -230,18 +254,24 @@ mod tests {
230254
promise2.fulfill(100);
231255

232256
all_promise
233-
.then(|results| {
257+
.then(move |results| {
234258
assert_eq!(results.len(), 2);
235259
assert_eq!(results[0], 42);
236260
assert_eq!(results[1], 100);
261+
*touched_clone.borrow_mut() = true;
237262
})
238263
.catch(|_err| {
239264
panic!("Should not reach here");
240265
});
266+
267+
assert_eq!(true, touched.take())
241268
}
242269

243270
#[test]
244271
fn test_all_of_failure() {
272+
let touched = Rc::new(RefCell::new(false));
273+
let touched_clone = touched.clone();
274+
245275
let promise1 = Promise::<i32>::new();
246276
let promise2 = Promise::<i32>::new();
247277

@@ -254,13 +284,19 @@ mod tests {
254284
.then(|_results| {
255285
panic!("Should not reach here");
256286
})
257-
.catch(|err| {
287+
.catch(move |err| {
258288
assert_eq!(err, "Error 1");
289+
*touched_clone.borrow_mut() = true;
259290
});
291+
292+
assert_eq!(true, touched.take())
260293
}
261294

262295
#[test]
263296
fn test_all_of_mixed_results() {
297+
let touched = Rc::new(RefCell::new(false));
298+
let touched_clone = touched.clone();
299+
264300
let promise1 = Promise::<i32>::new();
265301
let promise2 = Promise::<i32>::new();
266302

@@ -273,19 +309,30 @@ mod tests {
273309
.then(|_| {
274310
panic!("Should not reach here");
275311
})
276-
.catch(|reason| assert_eq!(reason, "Error".to_string()));
312+
.catch(move |reason| {
313+
assert_eq!(reason, "Error".to_string());
314+
*touched_clone.borrow_mut() = true;
315+
});
316+
317+
assert_eq!(true, touched.take())
277318
}
278319

279320
#[test]
280321
fn test_all_of_empty() {
322+
let touched = Rc::new(RefCell::new(false));
323+
let touched_clone = touched.clone();
324+
281325
let all_promise = Promise::<i32>::all_of(vec![]);
282326

283327
all_promise
284-
.then(|results| {
328+
.then(move |results| {
285329
assert!(results.is_empty());
330+
*touched_clone.borrow_mut() = true;
286331
})
287332
.catch(|_err| {
288333
panic!("Should not reach here");
289334
});
335+
336+
assert_eq!(true, touched.take())
290337
}
291338
}

0 commit comments

Comments
 (0)