From 94435d0af5f7f19a4a8648fb3d5658c0452957a7 Mon Sep 17 00:00:00 2001 From: James Hewitt Date: Wed, 24 Apr 2024 19:19:53 +0100 Subject: [PATCH] Add a test for threading (#21) Something in python 3.12.2 seems to have changed the threading behaviour, which causes odd results. --- test/test_minidb.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/test_minidb.py b/test/test_minidb.py index 6a485dc..75356e8 100644 --- a/test/test_minidb.py +++ b/test/test_minidb.py @@ -1,3 +1,4 @@ +import concurrent.futures import minidb import pytest import datetime @@ -662,3 +663,29 @@ class Thing(minidb.Model): db.delete_all(Thing) assert db.count_rows(Thing) == 0 + +def test_threaded_query(): + class Thing(minidb.Model): + s = str + i = int + + with minidb.Store(debug=True, vacuum_on_close=False) as db: + db.register(Thing) + + for i in range(100): + db.save(Thing(s=str(i), i=i)) + + def query(i): + things = list(Thing.query(db, Thing.c.s // Thing.c.i, where=Thing.c.i==i)) + assert len(things) == 1 + + thing = things[0] + assert thing is not None + assert thing.s == str(i) + assert thing.i == i + + return i + + executor = concurrent.futures.ThreadPoolExecutor(max_workers=10) + # Wrap in list to resolve all the futures + list(executor.map(query, range(100)))