def test_sample_by_process_from_history(self, mock_get_active_processes): fake_process_name_1 = 'fake-process-name-1' fake_process_name_2 = 'fake-process-name-2' task_path = '.' task_monitor = TaskMonitor(task_path, 'fake-task-id') fake_process_status_1 = ProcessStatus(process=fake_process_name_1) fake_process_status_2 = ProcessStatus(process=fake_process_name_2) mock_get_active_processes.return_value = [(fake_process_status_1, 1), (fake_process_status_2, 2)] fake_history = ResourceHistory(2) fake_history.add(time(), ResourceMonitorBase.FullResourceResult( {fake_process_status_1: ResourceMonitorBase.ProcResourceResult(ProcessSample.empty(), 1), fake_process_status_2: ResourceMonitorBase.ProcResourceResult(ProcessSample.empty(), 2), }, 10)) task_resource_monitor = TaskResourceMonitor('fake-task-id', task_monitor, history_provider=self.FakeResourceHistoryProvider(fake_history)) assert task_resource_monitor.name == 'TaskResourceMonitor[fake-task-id]' assert task_resource_monitor.sample_by_process(fake_process_name_1) == ProcessSample.empty() assert task_resource_monitor.sample_by_process(fake_process_name_2) == ProcessSample.empty() _, sample = task_resource_monitor.sample() assert sample.num_procs == 3 # 1 pid in fake_process_status_1 and 2 in fake_process_status_2 assert sample.process_sample == ProcessSample.empty() assert sample.disk_usage == 10 assert mock_get_active_processes.mock_calls == [mock.call(task_monitor), mock.call(task_monitor)]
def test_get(self): resource_stamp = time() + 100 value = ResourceMonitorBase.ResourceResult(1, 1, 0) value_wrong = ResourceMonitorBase.ResourceResult(1, 1, 50) self.resource_history.add(resource_stamp, value) self.resource_history.add(resource_stamp + 1000, value_wrong) self.resource_history.add(resource_stamp + 10000, value_wrong) assert resource_stamp, value == self.resource_history.get(resource_stamp)
def test_add(self): next_resource_stamp = time() + 100 value = ResourceMonitorBase.FullResourceResult({}, 0) assert (next_resource_stamp, value) not in self.resource_history._values self.resource_history.add(next_resource_stamp, value) assert (next_resource_stamp, value) == self.resource_history._values[1]
def _mock_resource_monitor(num_procs=0, process_sample=ProcessSample.empty(), disk_usage=0): mock_resource_monitor = mock.Mock(spec=ResourceMonitorBase) mock_resource_monitor.sample.return_value = ( 12345, # timestamp ResourceMonitorBase.AggregateResourceResult(num_procs, process_sample, disk_usage)) return mock_resource_monitor