Skip to content

Commit ad0df84

Browse files
author
Nigel Small
committed
Test for defunct (VERY BROKEN STILL)
1 parent b5f169b commit ad0df84

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

neo4j/v1/connection.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,6 @@ def chunk_reader(self):
169169
data = self._recv(chunk_size)
170170
yield data
171171

172-
def close(self):
173-
""" Shut down and close the connection.
174-
"""
175-
if __debug__: log_info("~~ [CLOSE]")
176-
socket = self.socket
177-
socket.shutdown(SHUT_RDWR)
178-
socket.close()
179-
180172

181173
class Response(object):
182174
""" Subscriber object for a full response (zero or
@@ -200,10 +192,6 @@ def on_ignored(self, metadata=None):
200192
pass
201193

202194

203-
class Completable(object):
204-
complete = False
205-
206-
207195
class Connection(object):
208196
""" Server connection through which all protocol messages
209197
are sent and received. This class is designed for protocol
@@ -217,6 +205,7 @@ def __init__(self, sock, **config):
217205
self.channel = ChunkChannel(sock)
218206
self.packer = Packer(self.channel)
219207
self.responses = deque()
208+
self.closed = False
220209

221210
# Determine the user agent and ensure it is a Unicode value
222211
user_agent = config.get("user_agent", DEFAULT_USER_AGENT)
@@ -234,6 +223,9 @@ def on_failure(metadata):
234223
while not response.complete:
235224
self.fetch_next()
236225

226+
def __del__(self):
227+
self.close()
228+
237229
def append(self, signature, fields=(), response=None):
238230
""" Add a message to the outgoing queue.
239231
@@ -304,9 +296,13 @@ def fetch_next(self):
304296
raw.close()
305297

306298
def close(self):
307-
""" Shut down and close the connection.
299+
""" Close the connection.
308300
"""
309-
self.channel.close()
301+
if not self.closed:
302+
if __debug__:
303+
log_info("~~ [CLOSE]")
304+
self.channel.socket.close()
305+
self.closed = True
310306

311307

312308
def connect(host, port=None, **config):

neo4j/v1/session.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def session(self):
107107
except IndexError:
108108
session = Session(self)
109109
else:
110-
session.reset()
110+
session.connection.reset()
111111
return session
112112

113113

@@ -347,19 +347,16 @@ def __init__(self, driver):
347347
self.closed = False
348348

349349
def __del__(self):
350-
self.connection.close()
350+
if not self.closed:
351+
self.connection.close()
352+
self.closed = True
351353

352354
def __enter__(self):
353355
return self
354356

355357
def __exit__(self, exc_type, exc_value, traceback):
356358
self.close()
357359

358-
def reset(self):
359-
""" Reset the connection so it can be reused from a clean state.
360-
"""
361-
self.connection.reset()
362-
363360
def run(self, statement, parameters=None):
364361
""" Run a parameterised Cypher statement.
365362
@@ -414,7 +411,6 @@ def run(self, statement, parameters=None):
414411
def close(self):
415412
""" If still usable, return this session to the driver pool it came from.
416413
"""
417-
self.reset()
418414
if not self.connection.defunct:
419415
self.driver.sessions.appendleft(self)
420416
self.closed = True

test/test_session.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from unittest import TestCase
2323

24+
from mock import patch
2425
from neo4j.v1.session import GraphDatabase, CypherError, Record, record
2526
from neo4j.v1.typesystem import Node, Relationship, Path
2627
from test.util import watch
@@ -223,14 +224,6 @@ def test_can_obtain_notification_info(self):
223224

224225
class ResetTestCase(TestCase):
225226

226-
def test_explicit_reset(self):
227-
with GraphDatabase.driver("bolt://localhost").session() as session:
228-
result = session.run("RETURN 1")
229-
assert result[0][0] == 1
230-
session.reset()
231-
result = session.run("RETURN 1")
232-
assert result[0][0] == 1
233-
234227
def test_automatic_reset_after_failure(self):
235228
with GraphDatabase.driver("bolt://localhost").session() as session:
236229
try:
@@ -241,6 +234,15 @@ def test_automatic_reset_after_failure(self):
241234
else:
242235
assert False, "A Cypher error should have occurred"
243236

237+
@watch
238+
def test_defunct(self):
239+
from neo4j.v1.connection import ChunkChannel, ProtocolError
240+
with GraphDatabase.driver("bolt://localhost").session() as session:
241+
assert not session.connection.defunct
242+
with patch.object(ChunkChannel, "chunk_reader", side_effect=ProtocolError()):
243+
session.run("RETURN 1")
244+
assert session.connection.defunct
245+
244246

245247
class RecordTestCase(TestCase):
246248
def test_record_equality(self):

test_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
behave
22
coverage
3+
mock
34
teamcity-messages

0 commit comments

Comments
 (0)