Skip to content

Commit a3552d3

Browse files
author
Fabien Coelho
committed
add more tests
1 parent e532924 commit a3552d3

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on top of [cachetools](https://pypi.org/project/cachetools/),
55
[redis](https://redis.io/) and [memcached](https://memcached.org/).
66

77
![Status](https://github.com/zx80/cachetools-utils/actions/workflows/ctu.yml/badge.svg?branch=main&style=flat)
8-
![Tests](https://img.shields.io/badge/tests-24%20✓-success)
8+
![Tests](https://img.shields.io/badge/tests-25%20✓-success)
99
![Coverage](https://img.shields.io/badge/coverage-100%25-success)
1010
![Issues](https://img.shields.io/github/issues/zx80/cachetools-utils?style=flat)
1111
![Python](https://img.shields.io/badge/python-3-informational)

test.py

+44-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import sys
44
import socket
55
import importlib
6+
import random
7+
import threading
68
import cachetools as ct
79
import CacheToolsUtils as ctu
810
import pytest
@@ -630,24 +632,17 @@ def m1(i: int):
630632
except Exception as e:
631633
assert "method: bad-method" in str(e)
632634

633-
def test_nogil():
635+
def run_thread(cache, nthreads):
634636

635-
try:
636-
if sys._is_gil_enabled():
637-
pytest.skip("gil is enabled")
638-
except AttributeError as e:
639-
assert "_is_gil_enabled" in str(e)
640-
pytest.skip("nogil not supported")
641-
642-
assert not sys._is_gil_enabled()
643-
644-
cache = ctu.StatsCache(ctu.DictCache())
637+
cache.clear()
638+
cache.reset()
639+
assert len(cache) == 0
645640

646-
@ctu.cached(ctu.AutoPrefixedCache(cache))
641+
@ctu.cached(cache=ctu.AutoPrefixedCache(cache))
647642
def repeat(s: str, n: int) -> str:
648643
return s * n
649644

650-
@ctu.cached(ctu.AutoPrefixedCache(cache))
645+
@ctu.cached(cache=ctu.AutoPrefixedCache(cache))
651646
def banged(s: str, n: int) -> str:
652647
return repeat(s, n) + "!"
653648

@@ -656,24 +651,49 @@ def banged(s: str, n: int) -> str:
656651
assert banged("a", 3) == "aaa!" # hit
657652
assert cache.hits() == 0.5
658653

659-
# reset stats
654+
cache.clear()
660655
cache.reset()
656+
assert len(cache) == 0
657+
658+
barrier = threading.Barrier(nthreads, timeout=2)
659+
660+
LS, LI = ["a", "b", "c", "d"], list(range(4))
661661

662-
import threading
663-
NTHREADS = 4
664-
barrier = threading.Barrier(NTHREADS, timeout=2)
665-
666662
def run():
663+
ls, li = LS.copy(), LI.copy()
664+
random.shuffle(ls)
665+
random.shuffle(li)
667666
barrier.wait()
668-
for s in ("a", "b", "c"):
669-
for n in range(3):
667+
for s in ls:
668+
for n in li:
669+
barrier.wait()
670670
assert banged(s, n) == s * n + "!"
671671
barrier.wait()
672672

673-
threads = [ threading.Thread(target=run, name=f"thread {i}") for i in range(NTHREADS) ]
674-
map(lambda t: t.start(), threads)
675-
map(lambda t: t.join(), threads)
673+
threads = [ threading.Thread(target=run, name=f"thread {i}") for i in range(nthreads) ]
674+
list(map(lambda t: t.start(), threads))
675+
list(map(lambda t: t.join(), threads))
676+
677+
assert len(cache) == 32
678+
assert abs(cache.hits() - (1.0 / (nthreads + 1))) < 0.001
679+
680+
def test_threads():
681+
cache = ctu.StatsCache(ctu.DictCache())
682+
run_thread(cache, 2)
683+
del cache
684+
685+
def test_nogil():
686+
try:
687+
if sys._is_gil_enabled():
688+
pytest.skip("gil is enabled")
689+
except AttributeError as e:
690+
assert "_is_gil_enabled" in str(e)
691+
pytest.skip("nogil not supported")
692+
693+
assert not sys._is_gil_enabled()
676694

677-
assert cache.hits() == 0.25
695+
cache = ctu.LockedCache(ctu.StatsCache(ctu.DictCache()), threading.Lock())
696+
run_thread(cache, 4)
697+
del cache
678698

679699
assert not sys._is_gil_enabled()

0 commit comments

Comments
 (0)