Skip to content

Commit 17f8113

Browse files
author
Fabien Coelho
committed
more tests
1 parent f843713 commit 17f8113

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

test.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ def m1(i: int):
634634

635635
def run_thread(cache, nthreads):
636636

637+
# direct tests
637638
cache.clear()
638639
cache.reset()
639640
assert len(cache) == 0
@@ -651,6 +652,7 @@ def banged(s: str, n: int) -> str:
651652
assert banged("a", 3) == "aaa!" # hit
652653
assert cache.hits() == 0.5
653654

655+
# threaded tests
654656
cache.clear()
655657
cache.reset()
656658
assert len(cache) == 0
@@ -660,6 +662,8 @@ def banged(s: str, n: int) -> str:
660662
LS, LI = ["a", "b", "c", "d"], list(range(4))
661663

662664
def run():
665+
name = threading.current_thread().name
666+
log.debug(f"thread start: {name}")
663667
ls, li = LS.copy(), LI.copy()
664668
random.shuffle(ls)
665669
random.shuffle(li)
@@ -669,17 +673,23 @@ def run():
669673
barrier.wait()
670674
assert banged(s, n) == s * n + "!"
671675
barrier.wait()
676+
log.debug(f"thread end: {name}")
672677

673678
threads = [ threading.Thread(target=run, name=f"thread {i}") for i in range(nthreads) ]
674679
list(map(lambda t: t.start(), threads))
675680
list(map(lambda t: t.join(), threads))
676681

682+
# log.info(f"nthreads={nthreads} stats={cache.stats()}")
683+
677684
assert len(cache) == 32
678-
assert abs(cache.hits() - (1.0 / (nthreads + 1))) < 0.001
685+
# FIXME the hit ratio may not be deterministic?
686+
# 16 * 2 gets-no-hit + 16 * (nthreads - 1) get-with-hit
687+
hits = (nthreads - 1) / (nthreads + 1)
688+
assert cache.hits() == hits
679689

680690
def test_threads():
681-
cache = ctu.StatsCache(ctu.DictCache())
682-
run_thread(cache, 2)
691+
cache = ctu.LockedCache(ctu.StatsCache(ctu.DictCache()), threading.RLock())
692+
run_thread(cache, 4)
683693
del cache
684694

685695
def test_nogil():

0 commit comments

Comments
 (0)