89
89
pub fn take ( self ) -> Recipient < Response , Streams > {
90
90
let ( response_sender, response_promise) = Promise :: < Response > :: new ( ) ;
91
91
self . commands . add ( AddImpulse :: new (
92
- dbg ! ( self . target) ,
92
+ self . target ,
93
93
TakenResponse :: < Response > :: new ( response_sender) ,
94
94
) ) ;
95
95
let mut map = StreamTargetMap :: default ( ) ;
@@ -107,7 +107,7 @@ where
107
107
pub fn take_response ( self ) -> Promise < Response > {
108
108
let ( response_sender, response_promise) = Promise :: < Response > :: new ( ) ;
109
109
self . commands . add ( AddImpulse :: new (
110
- dbg ! ( self . target) ,
110
+ self . target ,
111
111
TakenResponse :: < Response > :: new ( response_sender) ,
112
112
) ) ;
113
113
response_promise
@@ -327,39 +327,115 @@ impl<T> Default for Collection<T> {
327
327
#[ cfg( test) ]
328
328
mod tests {
329
329
use crate :: { * , testing:: * } ;
330
- use std:: time:: Duration ;
330
+ use std:: time:: { Instant , Duration } ;
331
331
332
332
#[ test]
333
- fn test_provide ( ) {
333
+ fn test_blocking_map ( ) {
334
334
let mut context = TestingContext :: minimal_plugins ( ) ;
335
335
336
336
let mut promise = context. build ( |commands| {
337
- commands. provide ( "hello" . to_owned ( ) ) . take_response ( )
337
+ commands
338
+ . request ( "hello" . to_owned ( ) , to_uppercase. into_blocking_map ( ) )
339
+ . take_response ( )
338
340
} ) ;
339
- assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "hello" ) ) ;
341
+
342
+ context. run_while_pending ( & mut promise) ;
343
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "HELLO" ) ) ;
344
+ assert ! ( context. no_unhandled_errors( ) ) ;
345
+
346
+ let mut promise = context. build ( |commands| {
347
+ commands
348
+ . request ( "hello" . to_owned ( ) , to_uppercase. into_blocking_map_once ( ) )
349
+ . take_response ( )
350
+ } ) ;
351
+
352
+ context. run_while_pending ( & mut promise) ;
353
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "HELLO" ) ) ;
354
+ assert ! ( context. no_unhandled_errors( ) ) ;
355
+
356
+ let mut promise = context. build ( |commands| {
357
+ commands
358
+ . provide ( "hello" . to_owned ( ) )
359
+ . map_block ( to_uppercase)
360
+ . take_response ( )
361
+ } ) ;
362
+
363
+ context. run_while_pending ( & mut promise) ;
364
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "HELLO" ) ) ;
365
+ assert ! ( context. no_unhandled_errors( ) ) ;
366
+
367
+ let mut promise = context. build ( |commands| {
368
+ commands
369
+ . provide ( "hello" . to_owned ( ) )
370
+ . map_block ( |request| request. to_uppercase ( ) )
371
+ . take_response ( )
372
+ } ) ;
373
+
374
+ context. run_while_pending ( & mut promise) ;
375
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "HELLO" ) ) ;
376
+ assert ! ( context. no_unhandled_errors( ) ) ;
340
377
}
341
378
342
379
#[ test]
343
380
fn test_async_map ( ) {
344
381
let mut context = TestingContext :: minimal_plugins ( ) ;
345
382
383
+ let request = WaitRequest {
384
+ duration : Duration :: from_secs_f64 ( 0.001 ) ,
385
+ value : "hello" . to_owned ( ) ,
386
+ } ;
387
+
388
+ let conditions = FlushConditions :: new ( )
389
+ . with_timeout ( Duration :: from_secs_f64 ( 5.0 ) ) ;
390
+
346
391
let mut promise = context. build ( |commands| {
347
392
commands
348
- . request (
349
- WaitRequest {
350
- duration : Duration :: from_secs_f64 ( 0.001 ) ,
351
- value : "hello" . to_owned ( ) ,
352
- } ,
353
- wait. into_async_map ( ) ,
354
- )
393
+ . request ( request. clone ( ) , wait. into_async_map ( ) )
355
394
. take_response ( )
356
395
} ) ;
357
396
358
- context. run_with_conditions (
359
- & mut promise,
360
- FlushConditions :: new ( )
361
- . with_timeout ( Duration :: from_secs_f64 ( 5.0 ) ) ,
362
- ) ;
397
+ assert ! ( context. run_with_conditions( & mut promise, conditions. clone( ) ) ) ;
398
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "hello" ) ) ;
399
+ assert ! ( context. no_unhandled_errors( ) ) ;
400
+
401
+ let mut promise = context. build ( |commands| {
402
+ commands
403
+ . request ( request. clone ( ) , wait. into_async_map_once ( ) )
404
+ . take_response ( )
405
+ } ) ;
406
+
407
+ assert ! ( context. run_with_conditions( & mut promise, conditions. clone( ) ) ) ;
408
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "hello" ) ) ;
409
+ assert ! ( context. no_unhandled_errors( ) ) ;
410
+
411
+ let mut promise = context. build ( |commands| {
412
+ commands
413
+ . provide ( request. clone ( ) )
414
+ . map_async ( wait)
415
+ . take_response ( )
416
+ } ) ;
417
+
418
+ assert ! ( context. run_with_conditions( & mut promise, conditions. clone( ) ) ) ;
419
+ assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "hello" ) ) ;
420
+ assert ! ( context. no_unhandled_errors( ) ) ;
421
+
422
+ let mut promise = context. build ( |commands| {
423
+ commands
424
+ . provide ( request. clone ( ) )
425
+ . map_async ( |request| {
426
+ async move {
427
+ let t = Instant :: now ( ) ;
428
+ while t. elapsed ( ) < request. duration {
429
+ // Busy wait
430
+ }
431
+ request. value
432
+ }
433
+ } )
434
+ . take_response ( )
435
+ } ) ;
436
+
437
+ assert ! ( context. run_with_conditions( & mut promise, conditions. clone( ) ) ) ;
363
438
assert ! ( promise. peek( ) . available( ) . is_some_and( |v| v == "hello" ) ) ;
439
+ assert ! ( context. no_unhandled_errors( ) ) ;
364
440
}
365
441
}
0 commit comments