def test_call_with_timeout_success(self): # type: () -> None (self.mock_callable(signal, "signal").for_call( signal.SIGALRM, alarm_handler).to_return_value(None).and_assert_called_once()) for val in [2, 0]: (self.mock_callable(signal, "alarm").for_call(val).to_return_value( None).and_assert_called_once()) result = call_with_timeout(math.floor, 2, args=[2.5]) self.assertEqual(result, 2.0)
def pids_holding_file(path): # type: str -> Set[psutil.Process] """ Returns a list of pids holding open file `path`. """ procs = set() for proc in psutil.process_iter(): try: pinfo = call_with_timeout(proc.as_dict, DEFAULT_TIMEOUT, kwargs={"attrs": ["pid", "open_files"]}) except (psutil.NoSuchProcess, TimeoutExpired): continue # Sometimes open_files can be None. open_files = pinfo.get("open_files", []) if not open_files: continue if path in [f.path for f in open_files]: procs.add(proc) return procs
def test_call_with_timeout_real_no_raises_returns_none(self): result = call_with_timeout(time.sleep, 1, raise_=False, args=[2]) self.assertIsNone(result)
def test_call_with_timeout_real_raises(self): with self.assertRaises(TimeoutExpired): call_with_timeout(time.sleep, 1, args=[2])
def test_call_with_timeout_success(self, mock_alarm, mock_signal): result = call_with_timeout(math.floor, 2, args=[2.5]) self.assertEqual(result, 2.0) mock_alarm.assert_has_calls([call(2), call(0)]) mock_signal.assert_called_once_with(signal.SIGALRM, alarm_handler)