memgraph/tests/e2e/concurrent_query_modules/client.py
gvolfing 476968e2c8
Fix concurrent query module race condition (#1158)
Concurrent access to the same query module had a race condition on the
pointer that was used to handle the custom memory management. With this
commit, a mapping has been added to keep information about what
thread used the pointer to handle the memory resources. This should be
fine since the respected query executions are running on a dedicated
thread. Access to the mapping itself is threadsafe. A simple RAII
wrapper for the mapping container has also been added for simpler
client-side use.
2023-08-21 16:45:36 +02:00

34 lines
823 B
Python

import multiprocessing
import mgclient
import pytest
def inner(query, number_of_executions):
connection = mgclient.connect(host="127.0.0.1", port=7687)
connection.autocommit = True
cursor = connection.cursor()
for _ in range(number_of_executions):
cursor.execute(query)
cursor.fetchall()
class MemgraphClient:
def __init__(self):
self.query_list = []
def initialize_to_execute(self, query: str, number_of_executions):
self.query_list.append((query, number_of_executions))
def execute_queries(self):
num_processes = len(self.query_list)
with multiprocessing.Pool(processes=num_processes) as pool:
pool.starmap(inner, self.query_list)
return True
@pytest.fixture
def client() -> MemgraphClient:
return MemgraphClient()