Skip to content

Commit 0746419

Browse files
authored
Fix logging missing hint as invalid value (#570)
If the server sends not connection.recv_timeout_seconds hint, the driver should not log this as an invalid value.
1 parent e81fbd2 commit 0746419

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

neo4j/io/_bolt4.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,17 @@ def hello(self):
381381
def on_success(metadata):
382382
self.configuration_hints.update(metadata.pop("hints", {}))
383383
self.server_info.update(metadata)
384-
recv_timeout = self.configuration_hints.get(
385-
"connection.recv_timeout_seconds"
386-
)
387-
if isinstance(recv_timeout, int) and recv_timeout > 0:
388-
self.socket.settimeout(recv_timeout)
389-
else:
390-
log.info("[#%04X] Server supplied an invalid value for "
391-
"connection.recv_timeout_seconds (%r). Make sure the "
392-
"server and network is set up correctly.",
393-
self.local_port, recv_timeout)
384+
if "connection.recv_timeout_seconds" in self.configuration_hints:
385+
recv_timeout = self.configuration_hints[
386+
"connection.recv_timeout_seconds"
387+
]
388+
if isinstance(recv_timeout, int) and recv_timeout > 0:
389+
self.socket.settimeout(recv_timeout)
390+
else:
391+
log.info("[#%04X] Server supplied an invalid value for "
392+
"connection.recv_timeout_seconds (%r). Make sure "
393+
"the server and network is set up correctly.",
394+
self.local_port, recv_timeout)
394395

395396
headers = self.get_base_headers()
396397
headers.update(self.auth_dict)

tests/unit/io/test_class_bolt4x3.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,35 +199,41 @@ def test_hello_passes_routing_metadata(fake_socket_pair):
199199
assert fields[0]["routing"] == {"foo": "bar"}
200200

201201

202-
@pytest.mark.parametrize(("recv_timeout", "valid"), (
203-
(1, True),
204-
(42, True),
205-
(-1, False),
206-
(0, False),
207-
(2.5, False),
208-
(None, False),
209-
("1", False),
202+
@pytest.mark.parametrize(("hints", "valid"), (
203+
({"connection.recv_timeout_seconds": 1}, True),
204+
({"connection.recv_timeout_seconds": 42}, True),
205+
({}, True),
206+
({"whatever_this_is": "ignore me!"}, True),
207+
({"connection.recv_timeout_seconds": -1}, False),
208+
({"connection.recv_timeout_seconds": 0}, False),
209+
({"connection.recv_timeout_seconds": 2.5}, False),
210+
({"connection.recv_timeout_seconds": None}, False),
211+
({"connection.recv_timeout_seconds": False}, False),
212+
({"connection.recv_timeout_seconds": "1"}, False),
210213
))
211-
def test_hint_recv_timeout_seconds(fake_socket_pair, recv_timeout, valid,
214+
def test_hint_recv_timeout_seconds(fake_socket_pair, hints, valid,
212215
caplog):
213216
address = ("127.0.0.1", 7687)
214217
sockets = fake_socket_pair(address)
215218
sockets.client.settimeout = MagicMock()
216-
sockets.server.send_message(0x70, {
217-
"server": "Neo4j/4.2.0",
218-
"hints": {"connection.recv_timeout_seconds": recv_timeout},
219-
})
219+
sockets.server.send_message(0x70, {"server": "Neo4j/4.3.0", "hints": hints})
220220
connection = Bolt4x3(address, sockets.client,
221221
PoolConfig.max_connection_lifetime)
222222
with caplog.at_level(logging.INFO):
223223
connection.hello()
224-
invalid_value_logged = any(repr(recv_timeout) in msg
225-
and "recv_timeout_seconds" in msg
226-
and "invalid" in msg
227-
for msg in caplog.messages)
228224
if valid:
229-
sockets.client.settimeout.assert_called_once_with(recv_timeout)
230-
assert not invalid_value_logged
225+
if "connection.recv_timeout_seconds" in hints:
226+
sockets.client.settimeout.assert_called_once_with(
227+
hints["connection.recv_timeout_seconds"]
228+
)
229+
else:
230+
sockets.client.settimeout.assert_not_called()
231+
assert not any("recv_timeout_seconds" in msg
232+
and "invalid" in msg
233+
for msg in caplog.messages)
231234
else:
232235
sockets.client.settimeout.assert_not_called()
233-
assert invalid_value_logged
236+
assert any(repr(hints["connection.recv_timeout_seconds"]) in msg
237+
and "recv_timeout_seconds" in msg
238+
and "invalid" in msg
239+
for msg in caplog.messages)

0 commit comments

Comments
 (0)