File tree 1 file changed +34
-6
lines changed 1 file changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -419,11 +419,39 @@ mod tests {
419
419
#[ test]
420
420
fn test_nested_spawn ( ) {
421
421
let pool = TaskPool :: new ( ) ;
422
- pool. scope ( |scope| {
423
- scope. clone ( ) . spawn ( async move {
424
- println ! ( "hello from the first scoped thread" ) ;
425
- scope. spawn ( async move { println ! ( "nested" ) } ) ;
426
- } ) ;
427
- } ) ;
422
+
423
+ let foo = Box :: new ( 42 ) ;
424
+ let foo = & * foo;
425
+
426
+ let count = Arc :: new ( AtomicI32 :: new ( 0 ) ) ;
427
+
428
+ let outputs: Vec < i32 > = pool. scope ( |scope| {
429
+ for _ in 0 ..10 {
430
+ let count_clone = count. clone ( ) ;
431
+ let scope = scope. clone ( ) ;
432
+ scope. clone ( ) . spawn ( async move {
433
+ for _ in 0 ..10 {
434
+ let count_clone_clone = count_clone. clone ( ) ;
435
+ scope. spawn ( async move {
436
+ if * foo != 42 {
437
+ panic ! ( "not 42!?!?" )
438
+ } else {
439
+ count_clone_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
440
+ * foo
441
+ }
442
+ } ) ;
443
+ }
444
+ * foo
445
+ } ) ;
446
+ }
447
+ } ) . collect ( ) ;
448
+
449
+ for output in & outputs {
450
+ assert_eq ! ( * output, 42 ) ;
451
+ }
452
+
453
+ // the inner loop runs 100 times and the outer one runs 10. 100 + 10
454
+ assert_eq ! ( outputs. len( ) , 110 ) ;
455
+ assert_eq ! ( count. load( Ordering :: Relaxed ) , 100 ) ;
428
456
}
429
457
}
You can’t perform that action at this time.
0 commit comments