//! Does the span we put for tracing in the mailbox get replaced if we spawn another actor from within the handler? //! Does the span persist if we spawn other non-actor tasks? -> No, Should it? //! mod common; use { common :: { import::* } , futures :: { executor::LocalSpawner } , // tracing_test :: { traced_test } , }; #[ derive( Actor ) ] pub struct Creator; #[ derive( Actor ) ] pub struct Created; #[ derive( Actor ) ] pub struct Create; impl Message for Create { type Return = (); } impl Handler for Creator { #[async_fn] fn handle( &mut self, _: Create ) { trace!( "creating new actor" ); async { trace!( "i am awaited within the handler on Creator" ); }.await; let mut created = Addr::builder().name( "created".into() ).spawn( Created, &AsyncStd ).expect( "create new actor" ); created.call( Create ).await.expect( "call created" ); } } impl Handler for Created { #[async_fn] fn handle( &mut self, _: Create ) { trace!( "in created handler" ); let span = error_span!( parent: tracing::Span::current(), "handle request" ); let exec = TokioTpBuilder::new().build().expect( "create tokio threadpool" ) .instrument( span ) ; exec.spawn_handle( async { trace!( "a spawned subtask" ); }).expect( "spawn subtask" ).await; } } // The purpose of this test is to verify that the tracing span with actor id and name is // replaced when one actor creates a second one, rather than having both ids. // // It requires looking at the log output in order to see it. // // #[traced_test] #[async_std::test] // async fn tracing_nested_pool() -> Result<(), Box > { let _ = tracing_subscriber::fmt::Subscriber::builder() .with_max_level(tracing::Level::TRACE) .try_init() ; let mut addr = Addr::builder().name( "creator".into() ).start_local( Creator, &AsyncStd )?; addr.call( Create ).await?; // assert!( logs_contain( r#"tracing: creating new actor"# ) ); // assert!( logs_contain( r#"actor{id=1 name="creator"}: tracing: creating new actor"# ) ); // assert!( logs_contain( r#"actor{id=2 name="created"}: thespis_impl::mailbox: mailbox: started for: Created (2, created)"# ) ); Ok(()) } // Single threaded version, to see if the tracing spans are persistant if the code runs on the same thread. #[ derive( Actor ) ] pub struct CreatorLocal { exec: LocalSpawner } #[ derive( Actor ) ] pub struct CreatedLocal { exec: LocalSpawner } impl Handler for CreatorLocal { #[async_fn] fn handle( &mut self, _: Create ) { unreachable!(); } #[async_fn_local] fn handle_local( &mut self, _: Create ) { trace!( "creating new actor" ); async { trace!( "i am awaited within the handler on CreatorLocal" ); }.await; let mut created = Addr::builder().spawn_local( CreatedLocal{ exec: self.exec.clone() }, &self.exec ).expect( "create new actor" ); created.call( Create ).await.expect( "call created" ); } } impl Handler for CreatedLocal { #[async_fn] fn handle( &mut self, _: Create ) { unreachable!(); } #[async_fn_local] fn handle_local( &mut self, _: Create ) { trace!( "in created handler" ); self.exec.spawn_handle_local( async { trace!( "a spawned subtask" ); }).expect( "spawn subtask" ).await; } } // The purpose of this test is to verify that the tracing span with actor id and name is // replaced when one actor creates a second one, rather than having both ids. // // It requires looking at the log output in order to see it. // // #[ test ] // // // fn tracing_nested_local() // { // let _ = tracing_subscriber::fmt::Subscriber::builder() // .with_max_level(tracing::Level::TRACE) // .try_init() // ; // let mut pool = LocalPool::new(); // let exec = pool.spawner(); // let task = async // { // let mut addr = Addr::builder().spawn_local( Creator, &exec ).expect( "start Creator" ); // addr.call( Create ).await.expect( "call Creator" ); // }; // pool.run_until( task ); // }