@@ -485,11 +485,30 @@ mod tests {
485
485
}
486
486
487
487
#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
488
- struct TestLabel ;
488
+ struct UnitLabel ;
489
489
490
- // TODO(luca) create a proc-macro for this, because library crates can't
491
- // export proc macros we will need to create a new macros only crate
492
- impl DeliveryLabel for TestLabel {
490
+ // TODO(@mxgrey) Figure out how to make the DeliveryLabel macro usable
491
+ // within the core bevy_impulse library
492
+ impl DeliveryLabel for UnitLabel {
493
+ fn dyn_clone ( & self ) -> Box < dyn DeliveryLabel > {
494
+ Box :: new ( self . clone ( ) )
495
+ }
496
+
497
+ fn as_dyn_eq ( & self ) -> & dyn DynEq {
498
+ self
499
+ }
500
+
501
+ fn dyn_hash ( & self , mut state : & mut dyn std:: hash:: Hasher ) {
502
+ let ty_id = std:: any:: TypeId :: of :: < Self > ( ) ;
503
+ std:: hash:: Hash :: hash ( & ty_id, & mut state) ;
504
+ std:: hash:: Hash :: hash ( self , & mut state) ;
505
+ }
506
+ }
507
+
508
+ #[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
509
+ struct StatefulLabel ( u64 ) ;
510
+
511
+ impl DeliveryLabel for StatefulLabel {
493
512
fn dyn_clone ( & self ) -> Box < dyn DeliveryLabel > {
494
513
Box :: new ( self . clone ( ) )
495
514
}
@@ -534,9 +553,38 @@ mod tests {
534
553
service : Service < Arc < Mutex < u64 > > , ( ) > ,
535
554
context : & mut TestingContext ,
536
555
) {
537
- let queuing_service = service. instruct ( TestLabel ) ;
538
- let preempting_service = service. instruct ( TestLabel . preempt ( ) ) ;
556
+ // Test for a unit struct
557
+ verify_preemption_matrix (
558
+ service. instruct ( UnitLabel ) ,
559
+ service. instruct ( UnitLabel . preempt ( ) ) ,
560
+ context,
561
+ ) ;
562
+
563
+ // Test for a stateful struct
564
+ verify_preemption_matrix (
565
+ service. instruct ( StatefulLabel ( 5 ) ) ,
566
+ service. instruct ( StatefulLabel ( 5 ) . preempt ( ) ) ,
567
+ context,
568
+ ) ;
539
569
570
+ // Test for a unit struct
571
+ verify_queuing_matrix ( service. instruct ( UnitLabel ) , context) ;
572
+
573
+ // Test for a stateful struct
574
+ verify_queuing_matrix ( service. instruct ( StatefulLabel ( 7 ) ) , context) ;
575
+
576
+ // Test for a unit struct
577
+ verify_ensured_matrix ( service, UnitLabel , context) ;
578
+
579
+ // Test for a stateful struct
580
+ verify_ensured_matrix ( service, StatefulLabel ( 2 ) , context) ;
581
+ }
582
+
583
+ fn verify_preemption_matrix (
584
+ queuing_service : Service < Arc < Mutex < u64 > > , ( ) > ,
585
+ preempting_service : Service < Arc < Mutex < u64 > > , ( ) > ,
586
+ context : & mut TestingContext ,
587
+ ) {
540
588
// Test by queuing up a bunch of requests before preempting them all at once.
541
589
verify_preemption ( 1 , queuing_service, preempting_service, context) ;
542
590
verify_preemption ( 2 , queuing_service, preempting_service, context) ;
@@ -548,25 +596,6 @@ mod tests {
548
596
verify_preemption ( 2 , preempting_service, preempting_service, context) ;
549
597
verify_preemption ( 3 , preempting_service, preempting_service, context) ;
550
598
verify_preemption ( 4 , preempting_service, preempting_service, context) ;
551
-
552
- // Test by queuing up a bunch of requests and making sure they all get
553
- // delivered.
554
- verify_queuing ( 2 , queuing_service, context) ;
555
- verify_queuing ( 3 , queuing_service, context) ;
556
- verify_queuing ( 4 , queuing_service, context) ;
557
- verify_queuing ( 5 , queuing_service, context) ;
558
-
559
- // Test by queuing up a mix of ensured and unensured requests, and then
560
- // sending in one that preempts them all. The ensured requests should
561
- // remain in the queue and execute despite the preempter. The unensured
562
- // requests should all be cancelled.
563
- verify_ensured ( [ false , true , false , true ] , service, context) ;
564
- verify_ensured ( [ true , false , false , false ] , service, context) ;
565
- verify_ensured ( [ true , true , false , false ] , service, context) ;
566
- verify_ensured ( [ false , false , true , true ] , service, context) ;
567
- verify_ensured ( [ true , false , false , true ] , service, context) ;
568
- verify_ensured ( [ false , false , false , false ] , service, context) ;
569
- verify_ensured ( [ true , true , true , true ] , service, context) ;
570
599
}
571
600
572
601
fn verify_preemption (
@@ -603,6 +632,18 @@ mod tests {
603
632
assert ! ( context. no_unhandled_errors( ) ) ;
604
633
}
605
634
635
+ fn verify_queuing_matrix (
636
+ queuing_service : Service < Arc < Mutex < u64 > > , ( ) > ,
637
+ context : & mut TestingContext ,
638
+ ) {
639
+ // Test by queuing up a bunch of requests and making sure they all get
640
+ // delivered.
641
+ verify_queuing ( 2 , queuing_service, context) ;
642
+ verify_queuing ( 3 , queuing_service, context) ;
643
+ verify_queuing ( 4 , queuing_service, context) ;
644
+ verify_queuing ( 5 , queuing_service, context) ;
645
+ }
646
+
606
647
fn verify_queuing (
607
648
queue_size : usize ,
608
649
queuing_service : Service < Arc < Mutex < u64 > > , ( ) > ,
@@ -628,9 +669,33 @@ mod tests {
628
669
assert ! ( context. no_unhandled_errors( ) ) ;
629
670
}
630
671
631
- fn verify_ensured (
672
+ fn verify_ensured_matrix < L : DeliveryLabel + Clone > (
673
+ service : Service < Arc < Mutex < u64 > > , ( ) > ,
674
+ label : L ,
675
+ context : & mut TestingContext ,
676
+ ) {
677
+ // Test by queuing up a mix of ensured and unensured requests, and then
678
+ // sending in one that preempts them all. The ensured requests should
679
+ // remain in the queue and execute despite the preempter. The unensured
680
+ // requests should all be cancelled.
681
+ verify_ensured ( [ false , true , false , true ] , service, label. clone ( ) , context) ;
682
+ verify_ensured ( [ true , false , false , false ] , service, label. clone ( ) , context) ;
683
+ verify_ensured ( [ true , true , false , false ] , service, label. clone ( ) , context) ;
684
+ verify_ensured ( [ false , false , true , true ] , service, label. clone ( ) , context) ;
685
+ verify_ensured ( [ true , false , false , true ] , service, label. clone ( ) , context) ;
686
+ verify_ensured (
687
+ [ false , false , false , false ] ,
688
+ service,
689
+ label. clone ( ) ,
690
+ context,
691
+ ) ;
692
+ verify_ensured ( [ true , true , true , true ] , service, label. clone ( ) , context) ;
693
+ }
694
+
695
+ fn verify_ensured < L : DeliveryLabel + Clone > (
632
696
queued : impl IntoIterator < Item = bool > ,
633
697
service : Service < Arc < Mutex < u64 > > , ( ) > ,
698
+ label : L ,
634
699
context : & mut TestingContext ,
635
700
) {
636
701
let counter = Arc :: new ( Mutex :: new ( 0_u64 ) ) ;
@@ -640,9 +705,9 @@ mod tests {
640
705
for ensured in queued {
641
706
let srv = if ensured {
642
707
expected_count += 1 ;
643
- service. instruct ( TestLabel . ensure ( ) )
708
+ service. instruct ( label . clone ( ) . ensure ( ) )
644
709
} else {
645
- service. instruct ( TestLabel )
710
+ service. instruct ( label . clone ( ) )
646
711
} ;
647
712
648
713
let promise = context
@@ -653,7 +718,10 @@ mod tests {
653
718
654
719
let mut preempter = context. command ( |commands| {
655
720
commands
656
- . request ( Arc :: clone ( & counter) , service. instruct ( TestLabel . preempt ( ) ) )
721
+ . request (
722
+ Arc :: clone ( & counter) ,
723
+ service. instruct ( label. clone ( ) . preempt ( ) ) ,
724
+ )
657
725
. take_response ( )
658
726
} ) ;
659
727
0 commit comments