Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a572c98

Browse filesBrowse files
committed
Fix: Fail early when database server does not respond
1 parent fb045ec commit a572c98
Copy full SHA for a572c98

File tree

5 files changed

+33
-17
lines changed
Filter options

5 files changed

+33
-17
lines changed

‎CHANGES.rst

Copy file name to clipboardExpand all lines: CHANGES.rst
+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Changed connection behaviour to fail early when database server
9+
does not respond
10+
811
2025/01/30 2.0.0
912
================
1013

‎docs/by-example/client.rst

Copy file name to clipboardExpand all lines: docs/by-example/client.rst
+2-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ respond, the request is automatically routed to the next server:
3535
>>> connection = client.connect([invalid_host, crate_host])
3636
>>> connection.close()
3737

38-
If no ``servers`` are given, the default one ``http://127.0.0.1:4200`` is used:
39-
40-
>>> connection = client.connect()
41-
>>> connection.client._active_servers
42-
['http://127.0.0.1:4200']
43-
>>> connection.close()
38+
If no ``servers`` are supplied to the ``connect`` method, the default address
39+
``http://127.0.0.1:4200`` is used.
4440

4541
If the option ``error_trace`` is set to ``True``, the client will print a whole
4642
traceback if a server error occurs:

‎src/crate/client/connection.py

Copy file name to clipboardExpand all lines: src/crate/client/connection.py
+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# software solely pursuant to the terms of the relevant commercial agreement.
2121

2222
from verlib2 import Version
23+
from verlib2.packaging.version import InvalidVersion
2324

2425
from .blob import BlobContainer
2526
from .cursor import Cursor
@@ -197,14 +198,20 @@ def get_blob_container(self, container_name):
197198

198199
def _lowest_server_version(self):
199200
lowest = None
201+
last_connection_error = None
200202
for server in self.client.active_servers:
201203
try:
202204
_, _, version = self.client.server_infos(server)
203205
version = Version(version)
204-
except (ValueError, ConnectionError):
206+
except ConnectionError as ex:
207+
last_connection_error = ex
208+
continue
209+
except (ValueError, InvalidVersion):
205210
continue
206211
if not lowest or version < lowest:
207212
lowest = version
213+
if lowest is None and last_connection_error is not None:
214+
raise last_connection_error
208215
return lowest or Version("0.0.0")
209216

210217
def __repr__(self):

‎tests/client/test_connection.py

Copy file name to clipboardExpand all lines: tests/client/test_connection.py
+16-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from urllib3 import Timeout
55

6+
import crate.client.exceptions
67
from crate.client import connect
78
from crate.client.connection import Connection
89
from crate.client.http import Client
@@ -21,21 +22,27 @@ def test_connection_mock(self):
2122
"""
2223

2324
class MyConnectionClient:
24-
active_servers = ["localhost:4200"]
25+
active_servers = [crate_host]
2526

2627
def __init__(self):
2728
pass
2829

2930
def server_infos(self, server):
30-
return ("localhost:4200", "my server", "0.42.0")
31+
return (crate_host, "my server", "0.42.0")
3132

3233
connection = connect([crate_host], client=MyConnectionClient())
3334
self.assertIsInstance(connection, Connection)
3435
self.assertEqual(
3536
connection.client.server_infos("foo"),
36-
("localhost:4200", "my server", "0.42.0"),
37+
(crate_host, "my server", "0.42.0"),
3738
)
3839

40+
def test_invalid_server_address(self):
41+
client = Client(servers="localhost:4202")
42+
with self.assertRaises(crate.client.exceptions.ConnectionError) as ex:
43+
connect(client=client)
44+
self.assertIn("Server not available", ex.exception.message)
45+
3946
def test_lowest_server_version(self):
4047
infos = [
4148
(None, None, "0.42.3"),
@@ -50,14 +57,14 @@ def test_lowest_server_version(self):
5057
connection.close()
5158

5259
def test_invalid_server_version(self):
53-
client = Client(servers="localhost:4200")
60+
client = Client(servers=crate_host)
5461
client.server_infos = lambda server: (None, None, "No version")
5562
connection = connect(client=client)
5663
self.assertEqual((0, 0, 0), connection.lowest_server_version.version)
5764
connection.close()
5865

5966
def test_context_manager(self):
60-
with connect("localhost:4200") as conn:
67+
with connect(crate_host) as conn:
6168
pass
6269
self.assertEqual(conn._closed, True)
6370

@@ -70,7 +77,7 @@ def test_with_timezone(self):
7077
"""
7178

7279
tz_mst = datetime.timezone(datetime.timedelta(hours=7), name="MST")
73-
connection = connect("localhost:4200", time_zone=tz_mst)
80+
connection = connect(crate_host, time_zone=tz_mst)
7481
cursor = connection.cursor()
7582
self.assertEqual(cursor.time_zone.tzname(None), "MST")
7683
self.assertEqual(
@@ -88,20 +95,20 @@ def test_timeout_float(self):
8895
"""
8996
Verify setting the timeout value as a scalar (float) works.
9097
"""
91-
with connect("localhost:4200", timeout=2.42) as conn:
98+
with connect(crate_host, timeout=2.42) as conn:
9299
self.assertEqual(conn.client._pool_kw["timeout"], 2.42)
93100

94101
def test_timeout_string(self):
95102
"""
96103
Verify setting the timeout value as a scalar (string) works.
97104
"""
98-
with connect("localhost:4200", timeout="2.42") as conn:
105+
with connect(crate_host, timeout="2.42") as conn:
99106
self.assertEqual(conn.client._pool_kw["timeout"], 2.42)
100107

101108
def test_timeout_object(self):
102109
"""
103110
Verify setting the timeout value as a Timeout object works.
104111
"""
105112
timeout = Timeout(connect=2.42, read=0.01)
106-
with connect("localhost:4200", timeout=timeout) as conn:
113+
with connect(crate_host, timeout=timeout) as conn:
107114
self.assertEqual(conn.client._pool_kw["timeout"], timeout)

‎tests/client/tests.py

Copy file name to clipboardExpand all lines: tests/client/tests.py
+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def test_suite():
3434
suite.addTest(makeSuite(KeepAliveClientTest))
3535
suite.addTest(makeSuite(ThreadSafeHttpClientTest))
3636
suite.addTest(makeSuite(ParamsTest))
37-
suite.addTest(makeSuite(ConnectionTest))
3837
suite.addTest(makeSuite(RetryOnTimeoutServerTest))
3938
suite.addTest(makeSuite(RequestsCaBundleTest))
4039
suite.addTest(makeSuite(TestUsernameSentAsHeader))
@@ -65,6 +64,10 @@ def test_suite():
6564
# Integration tests.
6665
layer = ensure_cratedb_layer()
6766

67+
s = makeSuite(ConnectionTest)
68+
s.layer = layer
69+
suite.addTest(s)
70+
6871
s = doctest.DocFileSuite(
6972
"docs/by-example/http.rst",
7073
"docs/by-example/client.rst",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.