GeezerVison is showing github.com — Validate ping timeout before reconnecting · PyMySQL/PyMySQL@6ed34dc
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: pymysql/connections.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -625,14 +625,14 @@ def ping(self, reconnect=False, timeout=None): |
625 | 625 | DeprecationWarning, |
626 | 626 | 2, |
627 | 627 | ) |
| 628 | + if timeout is not None and timeout <= 0: |
| 629 | + raise ValueError("timeout should be > 0") |
628 | 630 | if self._sock is None: |
629 | 631 | if reconnect: |
630 | 632 | self.connect() |
631 | 633 | reconnect = False |
632 | 634 | else: |
633 | 635 | raise err.Error("Already closed") |
634 | | - if timeout is not None and timeout <= 0: |
635 | | - raise ValueError("timeout should be > 0") |
636 | 636 | saved_timeouts = self._read_timeout, self._write_timeout |
637 | 637 | if timeout is not None: |
638 | 638 | self._read_timeout = self._write_timeout = timeout |
|
Collapse file
Copy file name to clipboardExpand all lines: pymysql/tests/test_ping_timeout.py +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,3 +56,12 @@ def test_ping_timeout_must_be_positive(): |
56 | 56 | finally: |
57 | 57 | server.close() |
58 | 58 | client.close() |
| 59 | + |
| 60 | + |
| 61 | +def test_ping_timeout_is_checked_before_reconnect(monkeypatch): |
| 62 | + conn = pymysql.connect(defer_connect=True) |
| 63 | + calls = [] |
| 64 | + monkeypatch.setattr(conn, "connect", lambda *args: calls.append(args)) |
| 65 | + with pytest.warns(DeprecationWarning), pytest.raises(ValueError): |
| 66 | + conn.ping(reconnect=True, timeout=0) |
| 67 | + assert calls == [] |