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

        // Create a mock controller which simulates shutting down the component
        // after a delay. The delay is much shorter than the period allotted
        // for the component to stop.
        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 component_stop_delay = zx::Duration::from_millis(stop_timeout.into_millis() / 1_000);
        let controller = MockController::new_with_responses(
            server,
            requests.clone(),
            server_channel_koid,
            // stop the component after 60ms
            ControllerActionResponse { close_channel: true, delay: Some(component_stop_delay) },
            ControllerActionResponse { close_channel: true, delay: Some(component_stop_delay) },
        );
        controller.serve();

        // Create the stop call that we expect to stop the component.
        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_future = Box::pin(stop_component_internal(&client_proxy, timer));

        // Poll the stop component future to where it has asked the controller
        // to stop the component. This should also cause the controller to
        // spawn the future with a delay to close the control channel.
        assert_eq!(Poll::Pending, exec.run_until_stalled(&mut stop_future));

        // Advance the clock beyond where the future to close the channel
        // should fire.
        let new_time =
            fasync::Time::from_nanos(exec.now().into_nanos() + component_stop_delay.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();

        // The controller channel should be closed so we can drive the stop
        // future to completion.
        match exec.run_until_stalled(&mut stop_future) {
            Poll::Ready(Ok(StopComponentSuccess::Stopped)) => {}
            Poll::Ready(Ok(result)) => {
                panic!("unexpected successful stop result {:?}", result);
            }
            Poll::Ready(Err(e)) => {
                panic!("unexpected error stopping component {:?}", e);
            }
            Poll::Pending => {
                panic!("future shoud have completed!");
            }
        }

        // Check that what we expect to be in the message map is there.
        let mut test_fut = Box::pin(async {
            let msg_map = requests.lock().await;
            let msg_list =
                msg_map.get(&server_channel_koid).expect("No messages received on the channel");

            // The controller should have only seen a STOP message since it stops
            // the component before the timeout is hit.
            assert_eq!(msg_list, &vec![ControlMessage::Stop]);
        });
        assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut test_fut));
    }
/// The scenario where the controller stops the component after a delay
/// which is before the controller reaches its timeout.
The scenario where the controller stops the component after a delay
which is before the controller reaches its timeout.
The scenario where the controller stops the component after a delay which is before the controller reaches its timeout .
fn stop_component_successful_stop_with_delay() {
        let mut exec = fasync::Executor::new_with_fake_time().unwrap();
        // Create a mock controller which simulates shutting down the component
        // after a delay. The delay is much shorter than the period allotted
        // for the component to stop.
        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 component_stop_delay = zx::Duration::from_millis(stop_timeout.into_millis() / 1_000);
        let controller = MockController::new_with_responses(
            server,
            requests.clone(),
            server_channel_koid,
            // stop the component after 60ms
            ControllerActionResponse { close_channel: true, delay: Some(component_stop_delay) },
            ControllerActionResponse { close_channel: true, delay: Some(component_stop_delay) },
        );
        controller.serve();
        // Create the stop call that we expect to stop the component.
        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_future = Box::pin(stop_component_internal(&client_proxy, timer));
        // Poll the stop component future to where it has asked the controller
        // to stop the component. This should also cause the controller to
        // spawn the future with a delay to close the control channel.
        assert_eq!(Poll::Pending, exec.run_until_stalled(&mut stop_future));
        // Advance the clock beyond where the future to close the channel
        // should fire.
        let new_time =
            fasync::Time::from_nanos(exec.now().into_nanos() + component_stop_delay.into_nanos());
        exec.set_fake_time(new_time);
        exec.wake_expired_timers();
        // The controller channel should be closed so we can drive the stop
        // future to completion.
        match exec.run_until_stalled(&mut stop_future) {
            Poll::Ready(Ok(StopComponentSuccess::Stopped)) => {}
            Poll::Ready(Ok(result)) => {
                panic!("unexpected successful stop result {:?}", result);
            }
            Poll::Ready(Err(e)) => {
                panic!("unexpected error stopping component {:?}", e);
            }
            Poll::Pending => {
                panic!("future shoud have completed!");
            }
        }
        // Check that what we expect to be in the message map is there.
        let mut test_fut = Box::pin(async {
            let msg_map = requests.lock().await;
            let msg_list =
                msg_map.get(&server_channel_koid).expect("No messages received on the channel");
            // The controller should have only seen a STOP message since it stops
            // the component before the timeout is hit.
            assert_eq!(msg_list, &vec![ControlMessage::Stop]);
        });
        assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut test_fut));
    }
fn stop_component_successful_stop_with_delay ( ) { let mut exec = fasync :: Executor :: new_with_fake_time ( ) . unwrap ( ) ; // Create a mock controller which simulates shutting down the component // after a delay. The delay is much shorter than the period allotted // for the component to stop. 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 component_stop_delay = zx :: Duration :: from_millis ( stop_timeout . into_millis ( ) / 1_000 ) ; let controller = MockController :: new_with_responses ( server , requests . clone ( ) , server_channel_koid , // stop the component after 60ms ControllerActionResponse { close_channel : true , delay : Some ( component_stop_delay ) } , ControllerActionResponse { close_channel : true , delay : Some ( component_stop_delay ) } , ) ; controller . serve ( ) ; // Create the stop call that we expect to stop the component. let timer = Box :: pin ( fasync :: Timer :: new ( fasync :: Time :: after ( stop_timeout ) ) ) ; let client_proxy = client . into_proxy ( ) . expect ( " " ) ; let mut stop_future = Box :: pin ( stop_component_internal ( & client_proxy , timer ) ) ; // Poll the stop component future to where it has asked the controller // to stop the component. This should also cause the controller to // spawn the future with a delay to close the control channel. assert_eq ! ( Poll Pending exec run_until_stalled ( mut stop_future ) ) ; // Advance the clock beyond where the future to close the channel // should fire. let new_time = fasync :: Time :: from_nanos ( exec . now ( ) . into_nanos ( ) + component_stop_delay . into_nanos ( ) ) ; exec . set_fake_time ( new_time ) ; exec . wake_expired_timers ( ) ; // The controller channel should be closed so we can drive the stop // future to completion. match exec . run_until_stalled ( & mut stop_future ) { Poll :: Ready ( Ok ( StopComponentSuccess :: Stopped ) ) => { } Poll :: Ready ( Ok ( result ) ) => { panic ! ( " " result ) ; } Poll :: Ready ( Err ( e ) ) => { panic ! ( " " e ) ; } Poll :: Pending => { panic ! ( " " ) ; } } // Check that what we expect to be in the message map is there. let mut test_fut = Box :: pin ( async { let msg_map = requests . lock ( ) . await ; let msg_list = msg_map . get ( & server_channel_koid ) . expect ( " " ) ; // The controller should have only seen a STOP message since it stops // the component before the timeout is hit. assert_eq ! ( msg_list vec [ ControlMessage Stop ] ) ; } ) ; assert_eq ! ( Poll Ready ( ( ) ) exec run_until_stalled ( mut test_fut ) ) ; }
The scenario where the controller stops the component after a delay
which is before the controller reaches its timeout.
The scenario where the controller stops the component after a delay which is before the controller reaches its timeout .