ó
§«Õ\c           @   s  d  Z  d d d „  ƒ  YZ d d l m Z d d l Z d d l m Z d d l m	 Z	 y d d l
 m Z Wn* e k
 r• d d	 l
 m Z d
 „  Z n Xd „  Z d Z e d Z d \ Z Z e d e d e ƒ d „  ƒ Z e e e ƒ ƒ Z d „  Z d „  Z d e j f d „  ƒ  YZ d S(   s»  The Python interpreter may switch between threads inbetween bytecode
execution.  Bytecode execution in fastcache may occur during:
(1) Calls to make_key which will call the __hash__ methods of the args and
(2) `PyDict_Get(Set)Item` calls rely on Python comparisons (i.e, __eq__)
    to determine if a match has been found

A good test for threadsafety is then to cache a function which takes user
defined Python objects that have __hash__ and __eq__ methods which live in
Python land rather built-in land.

The test should not only ensure that the correct result is acheived (and no
segfaults) but also assess memory leaks.

The thread switching interval can be altered using sys.setswitchinterval.
t	   PythonIntc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sD    Wrapper for an integer with python versions of __eq__ and __hash__.c         C   s   | |  _  d  S(   N(   t   value(   t   selft   val(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   __init__   s    c         C   s   t  |  j ƒ S(   N(   t   hashR   (   R   (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   __hash__   s    c         C   s8   t  | t ƒ s( t d t | ƒ ƒ ‚ n  |  j | j k S(   Ns"   PythonInt cannot be compared to %s(   t
   isinstanceR    t	   TypeErrort   typeR   (   R   t   other(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   __eq__   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyR       s   		iÿÿÿÿ(   t   randintN(   t
   clru_cache(   t   Thread(   t   setswitchinterval(   t   setcheckintervalc         C   s   t  t |  ƒ ƒ S(   N(   R   t   int(   t   i(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   setinterval(   s    c         C   s:   x |  D] } | j  ƒ  q Wx |  D] } | j ƒ  q" Wd  S(   N(   t   startt   join(   t   threadst   t(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   run_threads,   s    i-  i   i
   t   maxsizet   typedc         C   sA   |  j  } | d k  r | St t | d ƒ ƒ t t | d ƒ ƒ S(   s$   Terrible Fibonacci number generator.i   i   (   R   t   fibR    (   t   nt   v(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyR   6   s    	c         C   st   xm t  |  ƒ D]_ } t t t ƒ t k r5 t j ƒ  n  t t t ƒ ƒ } t | k r t	 d t | f ƒ ‚ q q Wd S(   s"    Run Fibonacci generator r times. s   Expected %d, Got %dN(
   t   rangeR   t   RAND_MINt   RAND_MAXR   t   cache_clearR    t   FIBt   RESULTt
   ValueError(   t   rR   t   res(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   run_fib_with_clear?   s    c         C   sR   xK t  |  ƒ D]= } t t t ƒ ƒ } t | k r t d t | f ƒ ‚ q q Wd S(   s"    Run Fibonacci generator r times. s   Expected %d, Got %dN(   R!   R   R    R%   R&   R'   (   R(   R   R)   (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   run_fib_with_statsH   s    t   Test_Threadingc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s#    Threadsafety Tests for lru_cache. c         C   s    t  d ƒ d |  _ d |  _ d  S(   Ngíµ ÷Æ°>i   iè  (   R   t
   numthreadst   repeat(   R   (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   setUpS   s    
	c         C   sU   g  t  |  j ƒ D]! } t d t d |  j f ƒ ^ q } t | ƒ |  j d d ƒ d S(   s/    randomly clear the cache during calls to fib. t   targett   argsi    N(   R!   R-   R   R*   R.   R   t   assertEqual(   R   t   _R   (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   test_thread_random_cache_clearsX   s    4
c         C   s‡   t  j ƒ  g  t |  j ƒ D]! } t d t d |  j f ƒ ^ q } t | ƒ t  j ƒ  \ } } } } |  j	 | t
 ƒ |  j	 | t
 ƒ d S(   sN    Run thread safety test to make sure the cache statistics
        are correct.R0   R1   N(   R   R$   R!   R-   R   R+   R.   R   t
   cache_infoR2   t
   CACHE_SIZE(   R   R3   R   t   hitst   missesR   t   currsize(    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   test_thread_cache_infoa   s    
4
(   R   R   R   R/   R4   R:   (    (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyR,   P   s   			(    (   i   i
   (   R   R    t   randomR   t   unittestt	   fastcacheR   t	   lru_cachet	   threadingR   t   sysR   R   t   ImportErrorR   R   R6   R%   R"   R#   t   FalseR   R&   R*   R+   t   TestCaseR,   (    (    (    s:   lib/python2.7/site-packages/fastcache/tests/test_thread.pyt   <module>   s&   	
			