def test_switch_off(self, mocker):
     mocker.patch("batsim_py.simulator.SetResourceStateBatsimRequest")
     mocker.patch.object(batsim_py.resources.Host, '_switch_off')
     s = SimulatorHandler()
     s.start("p", "w")
     s.switch_off([0])
     ps = s.platform.get_host(0).get_sleep_pstate()
     batsim_py.resources.Host._switch_off.assert_called_once()
     simulator.SetResourceStateBatsimRequest.assert_called_once_with(  # type: ignore
         0, [0], ps.id)
    def test_switch_off_must_dispatch_host_event(self, mocker):
        def foo(h: Host):
            self.__nb_called += 1

        self.__nb_called = 0
        mocker.patch.object(batsim_py.resources.Host, '_switch_off')
        s = SimulatorHandler()
        s.start("p", "w")
        s.subscribe(HostEvent.STATE_CHANGED, foo)
        s.switch_off([0, 1])
        assert self.__nb_called == 2
    def test_agenda_with_job_not_running(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        s.switch_off([h.id for h in s.platform.hosts])

        e = BatsimEventAPI.get_job_submitted(res=1, walltime=100)
        e = JobSubmittedBatsimEvent(0, e['data'])
        msg = BatsimMessage(0, [e])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        s.allocate(e.job.id, [0])

        msg = BatsimMessage(10, [RequestedCallBatsimEvent(10)])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        agenda = list(s.agenda)
        assert s.current_time == 10
        assert agenda[0].host.id == 0 and agenda[0].release_time == e.job.walltime
    def test_on_batsim_host_ps_changed_must_set_off_and_dispatch_event(self, mocker):
        def foo_h(h: Host):
            self.__h_called, self.__h_id = True, h.id
        self.__j_id = self.__h_id = -1
        s = SimulatorHandler()
        s.start("p", "w")

        s.switch_off([0])
        assert s.platform.get_host(0).is_switching_off

        # Setup
        p_id = s.platform.get_host(0).get_sleep_pstate().id
        e = BatsimEventAPI.get_resource_state_changed(150, [0], p_id)
        e = ResourcePowerStateChangedBatsimEvent(150, e['data'])
        msg = BatsimMessage(150, [e])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.subscribe(HostEvent.STATE_CHANGED, foo_h)
        s.proceed_time()

        assert s.platform.get_host(0).is_sleeping
        assert self.__h_called and self.__h_id == 0
 def test_switch_off_not_found_must_raise(self):
     s = SimulatorHandler()
     s.start("p", "w")
     with pytest.raises(LookupError) as excinfo:
         s.switch_off([10])
     assert 'resources' in str(excinfo.value)
 def test_switch_off_not_running_must_raise(self):
     s = SimulatorHandler()
     with pytest.raises(RuntimeError) as excinfo:
         s.switch_off([0])
     assert 'running' in str(excinfo.value)