3
3
import sys
4
4
import socket
5
5
import importlib
6
+ import random
7
+ import threading
6
8
import cachetools as ct
7
9
import CacheToolsUtils as ctu
8
10
import pytest
@@ -630,24 +632,17 @@ def m1(i: int):
630
632
except Exception as e :
631
633
assert "method: bad-method" in str (e )
632
634
633
- def test_nogil ( ):
635
+ def run_thread ( cache , nthreads ):
634
636
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
645
640
646
- @ctu .cached (ctu .AutoPrefixedCache (cache ))
641
+ @ctu .cached (cache = ctu .AutoPrefixedCache (cache ))
647
642
def repeat (s : str , n : int ) -> str :
648
643
return s * n
649
644
650
- @ctu .cached (ctu .AutoPrefixedCache (cache ))
645
+ @ctu .cached (cache = ctu .AutoPrefixedCache (cache ))
651
646
def banged (s : str , n : int ) -> str :
652
647
return repeat (s , n ) + "!"
653
648
@@ -656,24 +651,49 @@ def banged(s: str, n: int) -> str:
656
651
assert banged ("a" , 3 ) == "aaa!" # hit
657
652
assert cache .hits () == 0.5
658
653
659
- # reset stats
654
+ cache . clear ()
660
655
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 ))
661
661
662
- import threading
663
- NTHREADS = 4
664
- barrier = threading .Barrier (NTHREADS , timeout = 2 )
665
-
666
662
def run ():
663
+ ls , li = LS .copy (), LI .copy ()
664
+ random .shuffle (ls )
665
+ random .shuffle (li )
667
666
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 ()
670
670
assert banged (s , n ) == s * n + "!"
671
671
barrier .wait ()
672
672
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 ()
676
694
677
- assert cache .hits () == 0.25
695
+ cache = ctu .LockedCache (ctu .StatsCache (ctu .DictCache ()), threading .Lock ())
696
+ run_thread (cache , 4 )
697
+ del cache
678
698
679
699
assert not sys ._is_gil_enabled ()
0 commit comments