Skip to content

Commit c5bc8a4

Browse files
committed
Fix concurrency test bugs
- Move process_query function to module level for multiprocessing pickling - Fix race condition in simultaneous_read_write_protected test by using shared counter for unique indices All tests now pass: 936 passed, 8 skipped, 0 failed
1 parent ec5bc55 commit c5bc8a4

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

tests/unit/test_concurrency.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
from python_prtree import PRTree2D, PRTree3D, PRTree4D
2020

2121

22+
# Module-level function for multiprocessing (must be picklable)
23+
def _process_query_helper(query_data):
24+
"""Helper function for multiprocessing tests."""
25+
tree_class, idx_data, boxes_data, query_box = query_data
26+
# Recreate tree in subprocess
27+
tree = tree_class(idx_data, boxes_data)
28+
return tree.query(query_box)
29+
30+
2231
class TestPythonThreading:
2332
"""Test Python threading safety."""
2433

@@ -202,12 +211,6 @@ def test_process_pool_queries(self, PRTree, dim):
202211
for i in range(dim):
203212
boxes[:, i + dim] += boxes[:, i] + 1
204213

205-
def process_query(query_data):
206-
tree_class, idx_data, boxes_data, query_box = query_data
207-
# Recreate tree in subprocess
208-
tree = tree_class(idx_data, boxes_data)
209-
return tree.query(query_box)
210-
211214
# Prepare queries
212215
queries = []
213216
for _ in range(20):
@@ -217,7 +220,7 @@ def process_query(query_data):
217220
queries.append((PRTree, idx, boxes, query_box))
218221

219222
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
220-
results = list(executor.map(process_query, queries))
223+
results = list(executor.map(_process_query_helper, queries))
221224

222225
assert len(results) == 20
223226
for result in results:
@@ -465,6 +468,7 @@ def test_simultaneous_read_write_protected(self, PRTree, dim):
465468
tree = PRTree(idx, boxes)
466469
lock = threading.Lock()
467470
errors = []
471+
next_idx = [n] # Shared counter for unique indices
468472

469473
def reader():
470474
try:
@@ -479,13 +483,15 @@ def reader():
479483

480484
def writer():
481485
try:
482-
for i in range(50):
486+
for _ in range(50):
483487
box = np.random.rand(2 * dim) * 100
484488
for d in range(dim):
485489
box[d + dim] += box[d] + 1
486490

487491
with lock:
488-
tree.insert(idx=n + i, bb=box)
492+
insert_idx = next_idx[0]
493+
next_idx[0] += 1
494+
tree.insert(idx=insert_idx, bb=box)
489495
time.sleep(0.001)
490496
except Exception as e:
491497
errors.append(("writer", e))

0 commit comments

Comments
 (0)