Skip to content

Commit 02f4dda

Browse files
committed
Defunct test working :-)
1 parent ad0df84 commit 02f4dda

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

neo4j/v1/connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,27 @@ def on_failure(metadata):
262262
def send(self):
263263
""" Send all queued messages to the server.
264264
"""
265+
if self.closed:
266+
raise ProtocolError("Cannot write to a closed connection")
267+
if self.defunct:
268+
raise ProtocolError("Cannot write to a defunct connection")
265269
self.channel.send()
266270

267271
def fetch_next(self):
268272
""" Receive exactly one message from the server.
269273
"""
274+
if self.closed:
275+
raise ProtocolError("Cannot read from a closed connection")
276+
if self.defunct:
277+
raise ProtocolError("Cannot read from a defunct connection")
270278
raw = BytesIO()
271279
unpack = Unpacker(raw).unpack
272280
try:
273281
raw.writelines(self.channel.chunk_reader())
274282
except ProtocolError:
275283
self.defunct = True
276284
self.close()
277-
return
285+
raise
278286
# Unpack from the raw byte stream and call the relevant message handler(s)
279287
raw.seek(0)
280288
response = self.responses[0]

neokit

test/test_session.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,15 @@ def test_automatic_reset_after_failure(self):
234234
else:
235235
assert False, "A Cypher error should have occurred"
236236

237-
@watch
238237
def test_defunct(self):
239238
from neo4j.v1.connection import ChunkChannel, ProtocolError
240239
with GraphDatabase.driver("bolt://localhost").session() as session:
241240
assert not session.connection.defunct
242241
with patch.object(ChunkChannel, "chunk_reader", side_effect=ProtocolError()):
243-
session.run("RETURN 1")
242+
with self.assertRaises(ProtocolError):
243+
session.run("RETURN 1")
244244
assert session.connection.defunct
245+
assert session.connection.closed
245246

246247

247248
class RecordTestCase(TestCase):

test/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
def watch(f):
2828
""" Decorator to enable log watching for the lifetime of a function.
29-
Useful for debugging unit tests.
29+
Useful for debugging unit tests, simply add `@watch` to the top of
30+
the test function.
3031
3132
:param f: the function to decorate
3233
:return: a decorated function

0 commit comments

Comments
 (0)