Skip to content

Commit

Permalink
Fixes #391
Browse files Browse the repository at this point in the history
  • Loading branch information
technige committed Apr 7, 2015
1 parent c0dc86a commit 5ed3b96
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
19 changes: 16 additions & 3 deletions py2neo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 1411,10 @@ def hydrate(cls, data, inst=None):
new_inst = cls()
new_inst.__stale.update({"labels", "properties"})
inst = cls.cache.setdefault(self, new_inst)
# The check below is a workaround for http://bugs.python.org/issue19542
# See also: https://github.com/nigelsmall/py2neo/issues/391
if inst is None:
inst = cls.cache[self] = new_inst
cls.cache[self] = inst
inst.bind(self, data)
if "data" in data:
Expand Down Expand Up @@ -1756,6 1760,10 @@ def hydrate(cls, data, inst=None):
new_inst = cls()
new_inst.__stale.update({"properties"})
inst = cls.cache.setdefault(self, new_inst)
# The check below is a workaround for http://bugs.python.org/issue19542
# See also: https://github.com/nigelsmall/py2neo/issues/391
if inst is None:
inst = cls.cache[self] = new_inst
cls.cache[self] = inst
inst.bind(self, data)
inst.__type = data.get("type")
Expand Down Expand Up @@ -2407,9 2415,14 @@ def hydrate(cls, data, inst=None):
"""
self = data["self"]
if inst is None:
inst = cls.cache.setdefault(self, cls(Node.hydrate({"self": data["start"]}),
Rel.hydrate(data),
Node.hydrate({"self": data["end"]})))
new_inst = cls(Node.hydrate({"self": data["start"]}),
Rel.hydrate(data),
Node.hydrate({"self": data["end"]}))
inst = cls.cache.setdefault(self, new_inst)
# The check below is a workaround for http://bugs.python.org/issue19542
# See also: https://github.com/nigelsmall/py2neo/issues/391
if inst is None:
inst = cls.cache[self] = new_inst
else:
Node.hydrate({"self": data["start"]}, inst.start_node)
Node.hydrate({"self": data["end"]}, inst.end_node)
Expand Down
1 change: 1 addition & 0 deletions test/core/4-threads/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
__author__ = 'nigel'
62 changes: 62 additions & 0 deletions test/core/4-threads/weakref.py
Original file line number Diff line number Diff line change
@@ -0,0 1,62 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""
Test code for https://github.com/nigelsmall/py2neo/issues/391
"""

import threading
import py2neo
import random
from py2neo import Graph, Node, Relationship

def worker(graph):
"""
target function for thread
generates an edge between two somewhat randomly selected test nodes
"""
nodes = list(graph.find('test'))
source = random.choice(nodes[:len(nodes) // 2])
target = random.choice(nodes[len(nodes) // 2 1:])


relationship = Relationship(source, 'TEST_CONNECT', target)
graph.create(relationship)

def populate(graph):
"""
called once to populate graph with test nodes
"""
for i in range(10):
n = Node('test')
graph.create(n)

nodes = list(graph.find('test'))

# need to create the edges in the main thread in order
# to generate the error; not sure why
for i in range(3):
source = random.choice(nodes[:len(nodes) // 2])
target = random.choice(nodes[len(nodes) // 2 1:])
r = Relationship(source, 'TEST_CONNECT', target)
graph.create(r)

def generate_bug():
graph = Graph("http://neo4j:password@localhost:7474/db/data/")
populate(graph)

thread_pool = []
for thread_id in range(3):
t = threading.Thread(name=thread_id, target=worker, args=(graph,))
t.start()
thread_pool.append(t)

for thread in thread_pool:
t.join()


for r in graph.match(None,'TEST_CONNECT', None):
print(r)

if __name__ == '__main__':
generate_bug()

0 comments on commit 5ed3b96

Please sign in to comment.