fn stop_component_successful_race_with_controller() {
        let mut exec = fasync::Executor::new_with_fake_time().unwrap();

        // Create a controller which takes far longer than allowed to stop the
        // component.
        let stop_timeout = zx::Duration::from_seconds(5);
        let (client, server) =
            endpoints::create_endpoints::<fcrunner::ComponentControllerMarker>().unwrap();
        let server_channel_koid = server
            .as_handle_ref()
            .basic_info()
            .expect("failed to get basic info on server channel")
            .koid;

        // Create a request map which the MockController will fill with
        // requests it received related to mocked component.
        let requests: Arc<Mutex<HashMap<Koid, Vec<ControlMessage>>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let close_delta = zx::Duration::from_millis(10);
        let resp_delay =
            zx::Duration::from_millis(stop_timeout.into_millis() + close_delta.into_millis());
        let controller = MockController::new_with_responses(
            server,
            requests.clone(),
            server_channel_koid,
            // Process the stop message, but fail to close the channel after
            // the timeout of stop_component()
            ControllerActionResponse { close_channel: true, delay: Some(resp_delay) },
            // This is irrelevant because the controller should never receive
            // the kill message
            ControllerActionResponse { close_channel: true, delay: Some(resp_delay) },
        );
        controller.serve();

        let timer = Box::pin(fasync::Timer::new(fasync::Time::after(stop_timeout)));
        let client_proxy = client.into_proxy().expect("failed to convert client to proxy");
        let mut stop_fut = Box::pin(stop_component_internal(&client_proxy, timer));

        // it should be the case we stall waiting for a response from the
        // controller
        assert_eq!(Poll::Pending, exec.run_until_stalled(&mut stop_fut));

        // Roll time passed the stop timeout and beyond when the controller
        // will close the channel
        let new_time = fasync::Time::from_nanos(exec.now().into_nanos() + resp_delay.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();

        // This future waits for the client channel to close. This creates a
        // rendezvous between the controller's execution context and the test.
        // Without this the message map state may be inconsistent.
        let mut check_msgs = Box::pin(async {
            fasync::OnSignals::new(&client_proxy.as_handle_ref(), zx::Signals::CHANNEL_PEER_CLOSED)
                .await
                .expect("failed waiting for channel to close");

            let msg_map = requests.lock().await;
            let msg_list =
                msg_map.get(&server_channel_koid).expect("No messages received on the channel");

            assert_eq!(msg_list, &vec![ControlMessage::Stop]);
        });

        // Expect the message check future to complete because the controller
        // should close the channel.
        assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut check_msgs));

        // At this point stop_component() should now poll to completion because
        // the control channel is closed, but stop_component will perceive this
        // happening after its timeout expired.
        assert_eq!(
            Poll::Ready(Ok(StopComponentSuccess::StoppedWithTimeoutRace)),
            exec.run_until_stalled(&mut stop_fut)
        );
    }
/// In this case we expect success, but that the stop state machine races
/// with the controller. The state machine's timer expires, but when it
/// goes to send the kill message, it finds the control channel is closed,
/// indicating the component stopped.
In this case we expect success, but that the stop state machine races
with the controller. The state machine's timer expires, but when it
goes to send the kill message, it finds the control channel is closed,
indicating the component stopped.
In this case we expect success but that the stop state machine races with the controller . The state machine ' s timer expires but when it goes to send the kill message it finds the control channel is closed indicating the component stopped .
fn stop_component_successful_race_with_controller() {
        let mut exec = fasync::Executor::new_with_fake_time().unwrap();
        // Create a controller which takes far longer than allowed to stop the
        // component.
        let stop_timeout = zx::Duration::from_seconds(5);
        let (client, server) =
            endpoints::create_endpoints::<fcrunner::ComponentControllerMarker>().unwrap();
        let server_channel_koid = server
            .as_handle_ref()
            .basic_info()
            .expect("failed to get basic info on server channel")
            .koid;
        // Create a request map which the MockController will fill with
        // requests it received related to mocked component.
        let requests: Arc<Mutex<HashMap<Koid, Vec<ControlMessage>>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let close_delta = zx::Duration::from_millis(10);
        let resp_delay =
            zx::Duration::from_millis(stop_timeout.into_millis() + close_delta.into_millis());
        let controller = MockController::new_with_responses(
            server,
            requests.clone(),
            server_channel_koid,
            // Process the stop message, but fail to close the channel after
            // the timeout of stop_component()
            ControllerActionResponse { close_channel: true, delay: Some(resp_delay) },
            // This is irrelevant because the controller should never receive
            // the kill message
            ControllerActionResponse { close_channel: true, delay: Some(resp_delay) },
        );
        controller.serve();
        let timer = Box::pin(fasync::Timer::new(fasync::Time::after(stop_timeout)));
        let client_proxy = client.into_proxy().expect("failed to convert client to proxy");
        let mut stop_fut = Box::pin(stop_component_internal(&client_proxy, timer));
        // it should be the case we stall waiting for a response from the
        // controller
        assert_eq!(Poll::Pending, exec.run_until_stalled(&mut stop_fut));
        // Roll time passed the stop timeout and beyond when the controller
        // will close the channel
        let new_time = fasync::Time::from_nanos(exec.now().into_nanos() + resp_delay.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();
        // This future waits for the client channel to close. This creates a
        // rendezvous between the controller's execution context and the test.
        // Without this the message map state may be inconsistent.
        let mut check_msgs = Box::pin(async {
            fasync::OnSignals::new(&client_proxy.as_handle_ref(), zx::Signals::CHANNEL_PEER_CLOSED)
                .await
                .expect("failed waiting for channel to close");
            let msg_map = requests.lock().await;
            let msg_list =
                msg_map.get(&server_channel_koid).expect("No messages received on the channel");
            assert_eq!(msg_list, &vec![ControlMessage::Stop]);
        });
        // Expect the message check future to complete because the controller
        // should close the channel.
        assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut check_msgs));
        // At this point stop_component() should now poll to completion because
        // the control channel is closed, but stop_component will perceive this
        // happening after its timeout expired.
        assert_eq!(
            Poll::Ready(Ok(StopComponentSuccess::StoppedWithTimeoutRace)),
            exec.run_until_stalled(&mut stop_fut)
        );
    }
fn stop_component_successful_race_with_controller ( ) { let mut exec = fasync :: Executor :: new_with_fake_time ( ) . unwrap ( ) ; // Create a controller which takes far longer than allowed to stop the // component. let stop_timeout = zx :: Duration :: from_seconds ( 5 ) ; let ( client , server ) = endpoints :: create_endpoints :: < fcrunner :: ComponentControllerMarker > ( ) . unwrap ( ) ; let server_channel_koid = server . as_handle_ref ( ) . basic_info ( ) . expect ( " " ) . koid ; // Create a request map which the MockController will fill with // requests it received related to mocked component. let requests : Arc < Mutex < HashMap < Koid , Vec < ControlMessage > > > > = Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ; let close_delta = zx :: Duration :: from_millis ( 10 ) ; let resp_delay = zx :: Duration :: from_millis ( stop_timeout . into_millis ( ) + close_delta . into_millis ( ) ) ; let controller = MockController :: new_with_responses ( server , requests . clone ( ) , server_channel_koid , // Process the stop message, but fail to close the channel after // the timeout of stop_component() ControllerActionResponse { close_channel : true , delay : Some ( resp_delay ) } , // This is irrelevant because the controller should never receive // the kill message ControllerActionResponse { close_channel : true , delay : Some ( resp_delay ) } , ) ; controller . serve ( ) ; let timer = Box :: pin ( fasync :: Timer :: new ( fasync :: Time :: after ( stop_timeout ) ) ) ; let client_proxy = client . into_proxy ( ) . expect ( " " ) ; let mut stop_fut = Box :: pin ( stop_component_internal ( & client_proxy , timer ) ) ; // it should be the case we stall waiting for a response from the // controller assert_eq ! ( Poll Pending exec run_until_stalled ( mut stop_fut ) ) ; // Roll time passed the stop timeout and beyond when the controller // will close the channel let new_time = fasync :: Time :: from_nanos ( exec . now ( ) . into_nanos ( ) + resp_delay . into_nanos ( ) ) ; exec . set_fake_time ( new_time ) ; exec . wake_expired_timers ( ) ; // This future waits for the client channel to close. This creates a // rendezvous between the controller's execution context and the test. // Without this the message map state may be inconsistent. let mut check_msgs = Box :: pin ( async { fasync :: OnSignals :: new ( & client_proxy . as_handle_ref ( ) , zx :: Signals :: CHANNEL_PEER_CLOSED ) . await . expect ( " " ) ; let msg_map = requests . lock ( ) . await ; let msg_list = msg_map . get ( & server_channel_koid ) . expect ( " " ) ; assert_eq ! ( msg_list vec [ ControlMessage Stop ] ) ; } ) ; // Expect the message check future to complete because the controller // should close the channel. assert_eq ! ( Poll Ready ( ( ) ) exec run_until_stalled ( mut check_msgs ) ) ; // At this point stop_component() should now poll to completion because // the control channel is closed, but stop_component will perceive this // happening after its timeout expired. assert_eq ! ( Poll Ready ( Ok ( StopComponentSuccess StoppedWithTimeoutRace ) ) exec run_until_stalled ( mut stop_fut ) ) ; }
In this case we expect success, but that the stop state machine races
with the controller.
In this case we expect success but that the stop state machine races with the controller .