| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 893ddab commit aa50766
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -409,6 +409,10 @@ class AutoInterrupt(object): | |||
| 409 | 409 | ||
| 410 | 410 | __slots__ = ("proc", "args", "status") | |
| 411 | 411 | ||
| 412 | + # If this is non-zero it will override any status code during | ||
| 413 | + # _terminate, used to prevent race conditions in testing | ||
| 414 | + _status_code_if_terminate: int = 0 | ||
| 415 | + | ||
| 412 | 416 | def __init__(self, proc: Union[None, subprocess.Popen], args: Any) -> None: | |
| 413 | 417 | self.proc = proc | |
| 414 | 418 | self.args = args | |
@@ -427,11 +431,10 @@ def _terminate(self) -> None: | |||
| 427 | 431 | proc.stdout.close() | |
| 428 | 432 | if proc.stderr: | |
| 429 | 433 | proc.stderr.close() | |
| 430 | - | ||
| 431 | 434 | # did the process finish already so we have a return code ? | |
| 432 | 435 | try: | |
| 433 | 436 | if proc.poll() is not None: | |
| 434 | - self.status = proc.poll() | ||
| 437 | + self.status = self._status_code_if_terminate or proc.poll() | ||
| 435 | 438 | return None | |
| 436 | 439 | except OSError as ex: | |
| 437 | 440 | log.info("Ignored error after process had died: %r", ex) | |
@@ -443,7 +446,9 @@ def _terminate(self) -> None: | |||
| 443 | 446 | # try to kill it | |
| 444 | 447 | try: | |
| 445 | 448 | proc.terminate() | |
| 446 | - self.status = proc.wait() # ensure process goes away | ||
| 449 | + status = proc.wait() # ensure process goes away | ||
| 450 | + | ||
| 451 | + self.status = self._status_code_if_terminate or status | ||
| 447 | 452 | except OSError as ex: | |
| 448 | 453 | log.info("Ignored error after process had died: %r", ex) | |
| 449 | 454 | except AttributeError: | |
@@ -849,7 +854,7 @@ def execute(self, | |||
| 849 | 854 | ||
| 850 | 855 | if is_win: | |
| 851 | 856 | cmd_not_found_exception = OSError | |
| 852 | - if kill_after_timeout: | ||
| 857 | + if kill_after_timeout is not None: | ||
| 853 | 858 | raise GitCommandError(redacted_command, '"kill_after_timeout" feature is not supported on Windows.') | |
| 854 | 859 | else: | |
| 855 | 860 | cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable | |
@@ -916,7 +921,7 @@ def _kill_process(pid: int) -> None: | |||
| 916 | 921 | return | |
| 917 | 922 | # end | |
| 918 | 923 | ||
| 919 | - if kill_after_timeout: | ||
| 924 | + if kill_after_timeout is not None: | ||
| 920 | 925 | kill_check = threading.Event() | |
| 921 | 926 | watchdog = threading.Timer(kill_after_timeout, _kill_process, args=(proc.pid,)) | |
| 922 | 927 | ||
@@ -927,10 +932,10 @@ def _kill_process(pid: int) -> None: | |||
| 927 | 932 | newline = "\n" if universal_newlines else b"\n" | |
| 928 | 933 | try: | |
| 929 | 934 | if output_stream is None: | |
| 930 | - if kill_after_timeout: | ||
| 935 | + if kill_after_timeout is not None: | ||
| 931 | 936 | watchdog.start() | |
| 932 | 937 | stdout_value, stderr_value = proc.communicate() | |
| 933 | - if kill_after_timeout: | ||
| 938 | + if kill_after_timeout is not None: | ||
| 934 | 939 | watchdog.cancel() | |
| 935 | 940 | if kill_check.is_set(): | |
| 936 | 941 | stderr_value = ('Timeout: the command "%s" did not complete in %d ' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -658,10 +658,16 @@ def test_push_error(self, repo): | |||
| 658 | 658 | class TestTimeouts(TestBase): | |
| 659 | 659 | @with_rw_repo('HEAD', bare=False) | |
| 660 | 660 | def test_timeout_funcs(self, repo): | |
| 661 | - for function in ["pull"]: # can't get fetch and push to reliably timeout | ||
| 661 | + # Force error code to prevent a race condition if the python thread is | ||
| 662 | + # slow | ||
| 663 | + default = Git.AutoInterrupt._status_code_if_terminate | ||
| 664 | + Git.AutoInterrupt._status_code_if_terminate = -15 | ||
| 665 | + for function in ["pull", "fetch"]: # can't get push to timeout | ||
| 662 | 666 | f = getattr(repo.remotes.origin, function) | |
| 663 | 667 | assert f is not None # Make sure these functions exist | |
| 664 | - _ = f() # Make sure the function runs | ||
| 668 | + _ = f() # Make sure the function runs | ||
| 665 | 669 | with pytest.raises(GitCommandError, | |
| 666 | - match="kill_after_timeout=0.001 s"): | ||
| 667 | - f(kill_after_timeout=0.001) | ||
| 670 | + match="kill_after_timeout=0 s"): | ||
| 671 | + f(kill_after_timeout=0) | ||
| 672 | + | ||
| 673 | + Git.AutoInterrupt._status_code_if_terminate = default | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments