fn stop_component_successful_with_kill_result() {
        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 resp_delay = zx::Duration::from_millis(stop_timeout.into_millis() / 10);
        let controller = MockController::new_with_responses(
            server,
            requests.clone(),
            server_channel_koid,
            // Process the stop message, but fail to close the channel. Channel
            // closure is the indication that a component stopped.
            ControllerActionResponse { close_channel: false, delay: Some(resp_delay) },
            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.
        let mut new_time =
            fasync::Time::from_nanos(exec.now().into_nanos() + stop_timeout.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();

        // At this point stop_component() will have completed, but the
        // controller's future is not polled to completion, since it is not
        // required to complete the stop_component future.
        assert_eq!(
            Poll::Ready(Ok(StopComponentSuccess::Killed)),
            exec.run_until_stalled(&mut stop_fut)
        );

        // 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, ControlMessage::Kill]);
        });
        // On this poll we advance the controller's state to where it has
        // started the timer to close the channel.
        assert_eq!(Poll::Pending, exec.run_until_stalled(&mut check_msgs));

        // Roll time beyond the response delay so the controller closes the
        // channel.
        new_time = fasync::Time::from_nanos(exec.now().into_nanos() + resp_delay.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();

        // Now we expect the message check future to complete because the
        // controller should have closed the channel.
        assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut check_msgs));
    }
/// Test scenario where the controller does not stop the component within
/// the allowed period and the component stop state machine has to send
/// the `kill` message to the controller.
Test scenario where the controller does not stop the component within
the allowed period and the component stop state machine has to send
the `kill` message to the controller.
Test scenario where the controller does not stop the component within the allowed period and the component stop state machine has to send the ` kill ` message to the controller .
fn stop_component_successful_with_kill_result() {
        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 resp_delay = zx::Duration::from_millis(stop_timeout.into_millis() / 10);
        let controller = MockController::new_with_responses(
            server,
            requests.clone(),
            server_channel_koid,
            // Process the stop message, but fail to close the channel. Channel
            // closure is the indication that a component stopped.
            ControllerActionResponse { close_channel: false, delay: Some(resp_delay) },
            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.
        let mut new_time =
            fasync::Time::from_nanos(exec.now().into_nanos() + stop_timeout.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();
        // At this point stop_component() will have completed, but the
        // controller's future is not polled to completion, since it is not
        // required to complete the stop_component future.
        assert_eq!(
            Poll::Ready(Ok(StopComponentSuccess::Killed)),
            exec.run_until_stalled(&mut stop_fut)
        );
        // 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, ControlMessage::Kill]);
        });
        // On this poll we advance the controller's state to where it has
        // started the timer to close the channel.
        assert_eq!(Poll::Pending, exec.run_until_stalled(&mut check_msgs));
        // Roll time beyond the response delay so the controller closes the
        // channel.
        new_time = fasync::Time::from_nanos(exec.now().into_nanos() + resp_delay.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();
        // Now we expect the message check future to complete because the
        // controller should have closed the channel.
        assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut check_msgs));
    }
fn stop_component_successful_with_kill_result ( ) { 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 resp_delay = zx :: Duration :: from_millis ( stop_timeout . into_millis ( ) / 10 ) ; let controller = MockController :: new_with_responses ( server , requests . clone ( ) , server_channel_koid , // Process the stop message, but fail to close the channel. Channel // closure is the indication that a component stopped. ControllerActionResponse { close_channel : false , delay : Some ( resp_delay ) } , 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. let mut new_time = fasync :: Time :: from_nanos ( exec . now ( ) . into_nanos ( ) + stop_timeout . into_nanos ( ) ) ; exec . set_fake_time ( new_time ) ; exec . wake_expired_timers ( ) ; // At this point stop_component() will have completed, but the // controller's future is not polled to completion, since it is not // required to complete the stop_component future. assert_eq ! ( Poll Ready ( Ok ( StopComponentSuccess Killed ) ) exec run_until_stalled ( mut stop_fut ) ) ; // 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 ControlMessage Kill ] ) ; } ) ; // On this poll we advance the controller's state to where it has // started the timer to close the channel. assert_eq ! ( Poll Pending exec run_until_stalled ( mut check_msgs ) ) ; // Roll time beyond the response delay so the controller closes the // channel. new_time = fasync :: Time :: from_nanos ( exec . now ( ) . into_nanos ( ) + resp_delay . into_nanos ( ) ) ; exec . set_fake_time ( new_time ) ; exec . wake_expired_timers ( ) ; // Now we expect the message check future to complete because the // controller should have closed the channel. assert_eq ! ( Poll Ready ( ( ) ) exec run_until_stalled ( mut check_msgs ) ) ; }
Test scenario where the controller does not stop the component within
the allowed period and the component stop state machine has to send
the `kill` message to the controller.
Test scenario where the controller does not stop the component within the allowed period and the component stop state machine has to send the ` kill ` message to the controller .