Skip to content

Commit 3f25565

Browse files
committed
More robust test
1 parent a21885a commit 3f25565

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

‎tests/test_cache.py‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from threadpoolctl import ThreadpoolController
7+
from threadpoolctl import ThreadpoolController, _CDLL_CACHE
88

99

1010
def test_cdlls_are_cached():
@@ -24,21 +24,36 @@ def test_cdlls_are_cached():
2424
assert cached_cdll is controller2.lib_controllers[0].dynlib
2525

2626

27-
def test_cache_methods_on_dynlib():
27+
def test_cache_methods_on_dynlib(request):
2828
"""
2929
``_CDLLCache.cache_method_on_dynlib()`` caches the result, tied to the
3030
underlying ``CDLL`` as an invalidation key.
3131
"""
3232
pytest.importorskip("numpy")
3333

34-
# We assume all BLAS libs have ``get_version()`` wrapped with
35-
# ``cache_method_on_dynlib()``, which is currently the case.
3634
controller = ThreadpoolController()
3735
libs = controller.select(user_api="blas").lib_controllers
3836
if not libs:
3937
pytest.skip("No libraries loaded")
4038

41-
assert libs[0].get_version() is libs[0].get_version()
39+
@_CDLL_CACHE.cache_method_on_dynlib
40+
def my_extra_method(self):
41+
return object()
4242

43-
# Access underlying, uncached get_version():
44-
assert libs[0].get_version.__wrapped__(libs[0]) is not libs[0].get_version()
43+
# Can't use pytest's monkeypatch since this method doesn't already exist,
44+
# so add it manually:
45+
libs[0].__class__.my_extra_method = my_extra_method
46+
47+
def cleanup():
48+
del libs[0].__class__.my_extra_method
49+
50+
request.addfinalizer(cleanup)
51+
52+
# Accessing underlying, uncached method returns different objects each time:
53+
initial = libs[0].my_extra_method()
54+
assert libs[0].my_extra_method.__wrapped__(libs[0]) is not initial
55+
assert libs[0].my_extra_method.__wrapped__(libs[0]) is not initial
56+
57+
# Only one call should ever happen when using the cached method, however:
58+
assert libs[0].my_extra_method() is initial
59+
assert libs[0].my_extra_method() is initial

0 commit comments

Comments
 (0)