def test_does_not_attempt_to_reset_signal_if_signal_fails(monkeypatch): sig = MagicMock(side_effect=ValueError) monkeypatch.setattr(signal, "signal", sig) with pytest.raises(ValueError): with timeout(1): pass assert sig.call_count == 1
def collect_completed_work(self): best_timeout = self.wait_timeout while self.work_in_progress: try: with timeout(best_timeout): pid, result = os.wait() except Timeout: return # Once we've collected one task we want to time out very # quickly on the others so we don't delay rescheduling # work. best_timeout = 0.05 * self.wait_timeout self.progress_callback() work_item = self.work_in_progress.pop(pid) self.runtimes.append(time.monotonic() - work_item.started) with open(work_item.status_file, "w") as o: print(result >> 8, file=o)
def collect_completed_work(self): best_timeout = self.wait_timeout while self.work_in_progress: try: with timeout(best_timeout): pid, result = os.wait() except Timeout: return # Once we've collected one task we want to time out very # quickly on the others so we don't delay rescheduling # work. best_timeout = 0.05 * self.wait_timeout item_in_progress = self.work_in_progress.pop(pid) self.runtimes.append(time.monotonic() - item_in_progress.started) with open(item_in_progress.status_file, "w") as o: print(result >> 8, file=o) if result != 0 and self.failure_counts[item_in_progress.work_item.name] < self.retries: self.work_queue.append(item_in_progress.work_item) self.failure_counts[item_in_progress.work_item.name] += 1 else: self.progress_callback()
def test_will_recover_from_a_timeout_in_finally(monkeypatch): calls = 0 def sig(*args): pass signal.signal(signal.SIGALRM, sig) assert signal.getsignal(signal.SIGALRM) is sig def setitimer(*args): nonlocal calls assert signal.getsignal(signal.SIGALRM) is not sig calls += 1 if calls == 2: raise Timeout() monkeypatch.setattr(signal, "setitimer", setitimer) with timeout(1): assert signal.getsignal(signal.SIGALRM) is not sig assert signal.getsignal(signal.SIGALRM) is sig assert calls == 3
def test_will_time_out(): start = time.monotonic() with pytest.raises(Timeout): with timeout(0.01): time.sleep(10) assert time.monotonic() <= start + 0.5