@@ -789,11 +789,19 @@ impl<N: Network> Gateway<N> {
789789 }
790790}
791791
792- mod tests {
792+ #[ cfg( test) ]
793+ pub mod gateway_tests {
793794 use crate :: {
794- helpers:: tests:: { CommitteeInput , Validator } ,
795+ helpers:: {
796+ committee_tests:: { CommitteeInput , Validator } ,
797+ init_worker_channels,
798+ storage_tests:: StorageInput ,
799+ WorkerSender ,
800+ } ,
795801 Gateway ,
802+ Worker ,
796803 MAX_COMMITTEE_SIZE ,
804+ MAX_WORKERS ,
797805 MEMORY_POOL_PORT ,
798806 } ;
799807 use indexmap:: IndexMap ;
@@ -808,12 +816,15 @@ mod tests {
808816
809817 type N = Testnet3 ;
810818
811- #[ derive( Arbitrary , Debug ) ]
812- struct GatewayInput {
819+ #[ derive( Arbitrary , Debug , Clone ) ]
820+ pub struct GatewayInput {
813821 #[ filter( CommitteeInput :: is_valid) ]
814- committee_input : CommitteeInput ,
815- node_validator : Validator ,
816- dev : Option < u8 > ,
822+ pub committee_input : CommitteeInput ,
823+ pub node_validator : Validator ,
824+ pub dev : Option < u8 > ,
825+ #[ strategy( 0 ..MAX_WORKERS ) ]
826+ pub workers_count : u8 ,
827+ pub worker_storage : StorageInput ,
817828 }
818829
819830 impl GatewayInput {
@@ -826,6 +837,30 @@ mod tests {
826837 } ;
827838 Gateway :: new ( Arc :: new ( RwLock :: new ( committee) ) , account, dev) . unwrap ( )
828839 }
840+
841+ pub async fn generate_workers (
842+ & self ,
843+ gateway : & Gateway < N > ,
844+ ) -> ( IndexMap < u8 , Worker < N > > , IndexMap < u8 , WorkerSender < N > > ) {
845+ // Construct a map of the worker senders.
846+ let mut tx_workers = IndexMap :: new ( ) ;
847+ let mut workers = IndexMap :: new ( ) ;
848+
849+ // Initialize the workers.
850+ for id in 0 ..self . workers_count {
851+ // Construct the worker channels.
852+ let ( tx_worker, rx_worker) = init_worker_channels ( ) ;
853+ // Construct the worker instance.
854+ let mut worker = Worker :: new ( id, gateway. clone ( ) , self . worker_storage . to_storage ( ) ) . unwrap ( ) ;
855+ // Run the worker instance.
856+ worker. run ( rx_worker) . await . unwrap ( ) ;
857+
858+ // Add the worker and the worker sender to maps
859+ workers. insert ( id, worker) ;
860+ tx_workers. insert ( id, tx_worker) ;
861+ }
862+ ( workers, tx_workers)
863+ }
829864 }
830865
831866 #[ proptest]
@@ -857,18 +892,20 @@ mod tests {
857892 #[ proptest( async = "tokio" ) ]
858893 async fn gateway_start ( #[ filter( |x| x. dev. is_some( ) ) ] input : GatewayInput ) {
859894 let Some ( dev) = input. dev else { unreachable ! ( ) } ;
895+ println ! ( "{input:?}" ) ;
860896 let mut gateway = input. to_gateway ( ) ;
861897 let tcp_config = gateway. tcp ( ) . config ( ) ;
862898 assert_eq ! ( tcp_config. listener_ip, Some ( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) ) ) ;
863899 assert_eq ! ( tcp_config. desired_listening_port, Some ( MEMORY_POOL_PORT + ( dev as u16 ) ) ) ;
864900
865- match gateway. run ( IndexMap :: new ( ) ) . await {
901+ let ( workers, worker_senders) = input. generate_workers ( & gateway) . await ;
902+ match gateway. run ( worker_senders) . await {
866903 Ok ( _) => {
867904 assert_eq ! (
868905 gateway. local_ip( ) ,
869906 SocketAddr :: new( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) , MEMORY_POOL_PORT + ( dev as u16 ) )
870907 ) ;
871- assert_eq ! ( gateway. num_workers( ) , 0 ) ;
908+ assert_eq ! ( gateway. num_workers( ) , workers . len ( ) as u8 ) ;
872909 }
873910 Err ( err) => {
874911 panic ! ( "Shouldn't fail because of {err}" ) ;
0 commit comments