Skip to content

Commit

Permalink
add threading tests
Browse files Browse the repository at this point in the history
  • Loading branch information
capjamesg committed Oct 18, 2024
1 parent a69a049 commit 1cb3b23
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/concurrency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import random
import threading

from jamesql import JameSQL
from jamesql.index import GSI_INDEX_STRATEGIES


def test_threading():
with open("tests/fixtures/documents.json") as f:
documents = json.load(f)

index = JameSQL()

index.create_gsi("title", strategy=GSI_INDEX_STRATEGIES.CONTAINS)
index.create_gsi("lyric", strategy=GSI_INDEX_STRATEGIES.CONTAINS)

for document in documents * 100:
document = document.copy()
index.add(document, doc_id=str(random.randint(0, 1000000)))

def query(i):
if i == 0:
document = documents[0].copy()
document["title"] = "teal"
index.add(document, "xyz")
index.create_gsi("title", strategy=GSI_INDEX_STRATEGIES.CONTAINS)

assert len(index.string_query_search("teal")["documents"]) == 1

threads = []

for i in range(25):
t = threading.Thread(target=query, args=(i,))
threads.append(t)
t.start()

for t in threads:
t.join()

assert len(index.global_index) == 301
assert index.global_index["xyz"]["title"] == "teal"

0 comments on commit 1cb3b23

Please sign in to comment.