@@ -299,12 +299,19 @@ impl SignedEntityService for MithrilSignedEntityService {
299
299
300
300
#[ cfg( test) ]
301
301
mod tests {
302
+ use std:: {
303
+ sync:: atomic:: { AtomicBool , Ordering } ,
304
+ thread:: sleep,
305
+ time:: Duration ,
306
+ } ;
307
+
302
308
use mithril_common:: {
303
309
entities:: { CardanoTransactionsSnapshot , Epoch } ,
304
310
signable_builder,
305
311
test_utils:: fake_data,
306
312
} ;
307
313
use serde:: { de:: DeserializeOwned , Serialize } ;
314
+ use tokio:: task:: JoinSet ;
308
315
309
316
use crate :: artifact_builder:: MockArtifactBuilder ;
310
317
use crate :: database:: repository:: MockSignedEntityStorer ;
@@ -522,4 +529,142 @@ mod tests {
522
529
. await
523
530
. expect ( error_message_str) ;
524
531
}
532
+
533
+ #[ tokio:: test]
534
+ async fn create_artifact_for_two_signed_entity_types_in_sequence ( ) {
535
+ let signed_entity_type_immutable =
536
+ SignedEntityType :: CardanoImmutableFilesFull ( CardanoDbBeacon :: default ( ) ) ;
537
+ let signed_entity_type_msd = SignedEntityType :: MithrilStakeDistribution ( Epoch ( 1 ) ) ;
538
+ let artifact_snapshot = fake_data:: snapshots ( 1 ) . first ( ) . unwrap ( ) . to_owned ( ) ;
539
+ let artifact_msd = create_stake_distribution ( Epoch ( 1 ) , 5 ) ;
540
+ let mut mock_container = MockDependencyInjector :: new ( ) ;
541
+ {
542
+ let artifact_snapshot_clone: Arc < dyn Artifact > = Arc :: new ( artifact_snapshot. clone ( ) ) ;
543
+ let signed_entity_artifact = serde_json:: to_string ( & artifact_snapshot_clone) . unwrap ( ) ;
544
+ mock_container
545
+ . mock_signed_entity_storer
546
+ . expect_store_signed_entity ( )
547
+ . withf ( move |signed_entity| signed_entity. artifact == signed_entity_artifact)
548
+ . return_once ( |_| Ok ( ( ) ) ) ;
549
+
550
+ let artifact_msd_clone: Arc < dyn Artifact > = Arc :: new ( artifact_msd. clone ( ) ) ;
551
+ let signed_entity_artifact_msd = serde_json:: to_string ( & artifact_msd_clone) . unwrap ( ) ;
552
+ mock_container
553
+ . mock_signed_entity_storer
554
+ . expect_store_signed_entity ( )
555
+ . withf ( move |signed_entity| signed_entity. artifact == signed_entity_artifact_msd)
556
+ . return_once ( |_| Ok ( ( ) ) ) ;
557
+ }
558
+ {
559
+ let artifact_cloned = artifact_snapshot. clone ( ) ;
560
+ mock_container
561
+ . mock_cardano_immutable_files_full_artifact_builder
562
+ . expect_compute_artifact ( )
563
+ . times ( 1 )
564
+ . return_once ( |_, _| Ok ( artifact_cloned) ) ;
565
+
566
+ let artifact_msd_cloned = artifact_msd. clone ( ) ;
567
+ mock_container
568
+ . mock_mithril_stake_distribution_artifact_builder
569
+ . expect_compute_artifact ( )
570
+ . times ( 1 )
571
+ . return_once ( |_, _| Ok ( artifact_msd_cloned) ) ;
572
+ }
573
+ let artifact_builder_service = mock_container. build_artifact_builder_service ( ) ;
574
+
575
+ let certificate = fake_data:: certificate ( "hash" . to_string ( ) ) ;
576
+
577
+ artifact_builder_service
578
+ . create_artifact ( signed_entity_type_immutable, & certificate)
579
+ . await
580
+ . unwrap ( ) ;
581
+
582
+ artifact_builder_service
583
+ . create_artifact ( signed_entity_type_msd, & certificate)
584
+ . await
585
+ . unwrap ( ) ;
586
+ }
587
+
588
+ #[ tokio:: test]
589
+ async fn create_artifact_for_two_signed_entity_types_in_parallel ( ) {
590
+ let signed_entity_type_immutable =
591
+ SignedEntityType :: CardanoImmutableFilesFull ( CardanoDbBeacon :: default ( ) ) ;
592
+ let signed_entity_type_msd = SignedEntityType :: MithrilStakeDistribution ( Epoch ( 1 ) ) ;
593
+ let artifact_snapshot = fake_data:: snapshots ( 1 ) . first ( ) . unwrap ( ) . to_owned ( ) ;
594
+ let artifact_msd = create_stake_distribution ( Epoch ( 1 ) , 5 ) ;
595
+ let mut mock_container = MockDependencyInjector :: new ( ) ;
596
+ {
597
+ let artifact_snapshot_clone: Arc < dyn Artifact > = Arc :: new ( artifact_snapshot. clone ( ) ) ;
598
+ let signed_entity_artifact = serde_json:: to_string ( & artifact_snapshot_clone) . unwrap ( ) ;
599
+ mock_container
600
+ . mock_signed_entity_storer
601
+ . expect_store_signed_entity ( )
602
+ . withf ( move |signed_entity| signed_entity. artifact == signed_entity_artifact)
603
+ . return_once ( |_| Ok ( ( ) ) ) ;
604
+
605
+ let artifact_msd_clone: Arc < dyn Artifact > = Arc :: new ( artifact_msd. clone ( ) ) ;
606
+ let signed_entity_artifact_msd = serde_json:: to_string ( & artifact_msd_clone) . unwrap ( ) ;
607
+ mock_container
608
+ . mock_signed_entity_storer
609
+ . expect_store_signed_entity ( )
610
+ . withf ( move |signed_entity| signed_entity. artifact == signed_entity_artifact_msd)
611
+ . return_once ( |_| Ok ( ( ) ) ) ;
612
+ }
613
+ {
614
+ let artifact_cloned = artifact_snapshot. clone ( ) ;
615
+ mock_container
616
+ . mock_cardano_immutable_files_full_artifact_builder
617
+ . expect_compute_artifact ( )
618
+ . times ( 1 )
619
+ . return_once ( |_, _| {
620
+ sleep ( Duration :: from_millis ( 1000 ) ) ;
621
+ Ok ( artifact_cloned)
622
+ } ) ;
623
+
624
+ let artifact_msd_cloned = artifact_msd. clone ( ) ;
625
+ mock_container
626
+ . mock_mithril_stake_distribution_artifact_builder
627
+ . expect_compute_artifact ( )
628
+ . times ( 1 )
629
+ . return_once ( |_, _| Ok ( artifact_msd_cloned) ) ;
630
+ }
631
+ let artifact_builder_service = Arc :: new ( mock_container. build_artifact_builder_service ( ) ) ;
632
+
633
+ let first_task_started = Arc :: new ( AtomicBool :: new ( false ) ) ;
634
+ let mut set = JoinSet :: new ( ) ;
635
+ for _ in 0 ..2 {
636
+ let first_task_started_cloned = first_task_started. clone ( ) ;
637
+ let artifact_builder_service_cloned = artifact_builder_service. clone ( ) ;
638
+ let signed_entity_type_msd_cloned = signed_entity_type_msd. clone ( ) ;
639
+ let signed_entity_type_immutable_cloned = signed_entity_type_immutable. clone ( ) ;
640
+ set. spawn ( async move {
641
+ if first_task_started_cloned. swap ( true , Ordering :: Relaxed ) {
642
+ println ! ( "Task signed_entity_type_msd_cloned" ) ;
643
+ artifact_builder_service_cloned
644
+ . create_artifact (
645
+ signed_entity_type_msd_cloned,
646
+ & fake_data:: certificate ( "hash" . to_string ( ) ) ,
647
+ )
648
+ . await
649
+ . unwrap ( ) ;
650
+ "signed_entity_type_msd"
651
+ } else {
652
+ println ! ( "Task signed_entity_type_immutable_cloned" ) ;
653
+ artifact_builder_service_cloned
654
+ . create_artifact (
655
+ signed_entity_type_immutable_cloned,
656
+ & fake_data:: certificate ( "hash" . to_string ( ) ) ,
657
+ )
658
+ . await
659
+ . unwrap ( ) ;
660
+ "signed_entity_type_immutable"
661
+ }
662
+ } ) ;
663
+ }
664
+
665
+ while let Some ( res) = set. join_next ( ) . await {
666
+ let idx = res. unwrap ( ) ;
667
+ println ! ( "Task finished: {}" , idx) ;
668
+ }
669
+ }
525
670
}
0 commit comments