-
Notifications
You must be signed in to change notification settings - Fork 606
Add --timeout option and a connect_timeout config default #1622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DiegoDAF
wants to merge
2
commits into
dbcli:main
Choose a base branch
from
DiegoDAF:upstream/connect-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,15 @@ | |
| obfuscate_process_password, | ||
| duration_in_words, | ||
| format_output, | ||
| get_connect_timeout, | ||
| get_editor, | ||
| notify_callback, | ||
| PGCli, | ||
| OutputSettings, | ||
| COLOR_CODE_REGEX, | ||
| ) | ||
| from pgcli.pgexecute import PGExecute | ||
| from psycopg.conninfo import conninfo_to_dict | ||
| from pgspecial.main import PAGER_OFF, PAGER_LONG_OUTPUT, PAGER_ALWAYS | ||
| from utils import dbtest, run | ||
| from collections import namedtuple | ||
|
|
@@ -500,6 +502,7 @@ def test_pg_service_file(tmpdir): | |
| "", | ||
| notify_callback, | ||
| application_name="pgcli", | ||
| connect_timeout="30", | ||
| ) | ||
| del os.environ["PGPASSWORD"] | ||
| del os.environ["PGSERVICEFILE"] | ||
|
|
@@ -548,7 +551,7 @@ def test_application_name_db_uri(tmpdir): | |
| mock_pgexecute.return_value = None | ||
| cli = PGCli(pgclirc_file=str(tmpdir.join("rcfile"))) | ||
| cli.connect_uri("postgres://bar@baz.com/?application_name=cow") | ||
| mock_pgexecute.assert_called_with("bar", "bar", "", "baz.com", "", "", notify_callback, application_name="cow") | ||
| mock_pgexecute.assert_called_with("bar", "bar", "", "baz.com", "", "", notify_callback, application_name="cow", connect_timeout="30") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
|
|
@@ -701,3 +704,76 @@ def test_get_editor_precedence(): | |
| # Nothing set -> None, so click uses its platform default. | ||
| with mock.patch.dict(os.environ, {}, clear=True): | ||
| assert get_editor() is None | ||
|
|
||
|
|
||
| def _effective_connect_timeout(tmpdir, cli_timeout=None, dsn_timeout=None, env=None, cfgval=None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it works... but a |
||
| """The connect_timeout that actually reaches the connection.""" | ||
| rc = str(tmpdir.join("rcfile")) | ||
| with open(rc, "w") as f: | ||
| f.write("[main]\n" + (f"connect_timeout = {cfgval}\n" if cfgval else "")) | ||
| environ = {k: v for k, v in os.environ.items() if k != "PGCONNECT_TIMEOUT"} | ||
| if env: | ||
| environ["PGCONNECT_TIMEOUT"] = env | ||
| with mock.patch.dict(os.environ, environ, clear=True): | ||
| cli_obj = PGCli(pgclirc_file=rc, connect_timeout=cli_timeout) | ||
| dsn = "postgresql://u@h:5432/db" + (f"?connect_timeout={dsn_timeout}" if dsn_timeout else "") | ||
| captured = {} | ||
|
|
||
| def fake(*a, **k): | ||
| captured["dsn"] = k.get("dsn") or (a[5] if len(a) > 5 else None) | ||
| captured["kwargs"] = k | ||
| raise RuntimeError("stop") | ||
|
|
||
| # connect() turns a failed connection into sys.exit(1); let it. | ||
| with mock.patch("pgcli.main.PGExecute", side_effect=fake), pytest.raises(SystemExit): | ||
| cli_obj.connect(dsn=dsn, host="h", port="5432", user="u", database="db") | ||
| from_kwargs = captured.get("kwargs", {}).get("connect_timeout") | ||
| return from_kwargs or conninfo_to_dict(captured.get("dsn") or "").get("connect_timeout") | ||
|
|
||
|
|
||
| DSN_WITH_TIMEOUT = "postgresql://u@h:5432/db?connect_timeout=15" | ||
| DSN_PLAIN = "postgresql://u@h:5432/db" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "explicit, dsn, kwargs, env, expected, why", | ||
| [ | ||
| (None, DSN_PLAIN, {}, None, 30, "nothing else set, so the config default applies"), | ||
| (None, DSN_WITH_TIMEOUT, {}, None, None, "the connection string already says so"), | ||
| (None, DSN_PLAIN, {"connect_timeout": "9"}, None, None, "the caller already says so"), | ||
| (None, DSN_PLAIN, {}, "7", None, "libpq reads $PGCONNECT_TIMEOUT itself"), | ||
| (None, DSN_WITH_TIMEOUT, {}, "7", None, "the connection string beats the environment"), | ||
| (3, DSN_WITH_TIMEOUT, {}, "7", 3, "--timeout beats everything"), | ||
| (0, DSN_WITH_TIMEOUT, {}, None, 0, "--timeout 0 is meaningful, not unset"), | ||
| (None, None, {}, None, 30, "no dsn at all"), | ||
| ], | ||
| ) | ||
| def test_get_connect_timeout(explicit, dsn, kwargs, env, expected, why): | ||
| environ = {k: v for k, v in os.environ.items() if k != "PGCONNECT_TIMEOUT"} | ||
| if env: | ||
| environ["PGCONNECT_TIMEOUT"] = env | ||
| with mock.patch.dict(os.environ, environ, clear=True): | ||
| assert get_connect_timeout(explicit, dsn, kwargs, 30) == expected, why | ||
|
|
||
|
|
||
| def test_connect_timeout_config_default_reaches_the_connection(tmpdir): | ||
| """The helper is actually wired into connect(): libpq's own default of 0 | ||
| waits until the OS gives up, which takes minutes.""" | ||
| assert _effective_connect_timeout(tmpdir) == "30" | ||
|
|
||
|
|
||
| def test_connect_timeout_config_value_used(tmpdir): | ||
| assert _effective_connect_timeout(tmpdir, cfgval=45) == "45" | ||
|
|
||
|
|
||
| def test_connect_timeout_cli_reaches_the_connection(tmpdir): | ||
| assert _effective_connect_timeout(tmpdir, cli_timeout=3, dsn_timeout=15, env="7") == "3" | ||
|
|
||
|
|
||
| def test_connect_timeout_config_value_must_be_a_number(tmpdir): | ||
| """A typo in the config is reported instead of being silently ignored.""" | ||
| rc = str(tmpdir.join("rcfile")) | ||
| with open(rc, "w") as f: | ||
| f.write("[main]\nconnect_timeout = soon\n") | ||
| with pytest.raises(ValueError): | ||
| PGCli(pgclirc_file=rc) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment above is now out-of-date.