
    vh\              	      P   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	Z	ddl
Z
ddlZddlZddlZddlZddlmZ ddlmZ dd	lmZ dd
lmZ  e	j*                  e      Z ej0                  d e
j2                  dd      d      Z ej0                  d e
j2                  dd      d      Z ej8                  d e e
j2                  dd            d      Z ej>                         Z d dZ!ed        Z"d Z# G d d      Z$ G d dejJ                        Z& G d dejN                        Z(y)!aI  
Contains a custom unittest loader and test suite.

Implements:
- A test filter based on the JAX_TEST_TARGETS and JAX_EXCLUDE_TEST_TARGETS
  environment variables.
- A test suite that runs tests in parallel using threads if JAX_TEST_NUM_THREADS
  is >= 1.
- Test decorators that mark a test case or test class as thread-hostile.
    )annotations)Callable)ThreadPoolExecutor)contextmanagerN)absltest)config)test_warning_util)utiltest_targetsJAX_TEST_TARGETS zRegular expression specifying which tests to run, called via re.search on the test name. If empty or unspecified, run all tests.exclude_test_targetsJAX_EXCLUDE_TEST_TARGETSzRegular expression specifying which tests NOT to run, called via re.search on the test name. If empty or unspecified, run all tests.jax_test_num_threadsJAX_TEST_NUM_THREADS0zxNumber of threads to use for running tests. 0 means run everything in the main thread. Using > 1 thread is experimental.)helpThreadSafeTestResultc                \   t        | j                  dd      r2t        j                          	  | |       t        j	                          y t        j                          	  | |       t        j                          y # t        j	                          w xY w# t        j                          w xY w)Nthread_hostileF)getattr	__class___test_rwlockwriter_lockwriter_unlockreader_lockreader_unlock)testresults     O/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/test_loader.py_run_one_testr!   E   sz    T^^-u5#
6l  "#
6l  "   "   "s   A< B <BB+c               #  h  K   t         j                  dk  rd yt        j                          t        j	                          t        j                          	 d t        j                          t        j                          y# t        j                          t        j                          w xY ww)zDecorator for tests that are not thread-safe.

  Note: this decorator (naturally) only applies to what it wraps, not to, say,
  code in separate setUp() or tearDown() methods.
  r   N)TEST_NUM_THREADSvaluer   assert_reader_heldr   r   r   r        r    thread_unsafe_testr(   T   s{      q 	
!!#	   s   AB2B )B2*B//B2c                     d } | S )z8Decorator that marks a TestCase class as thread-hostile.c                h    t        | t        j                        sJ t        |              d| _        | S )NT)
issubclassunittestTestCasetyper   )klasss    r    fz#thread_unsafe_test_class.<locals>.fk   s.    eX../<e</ELr'   r&   )r0   s    r    thread_unsafe_test_classr1   i   s     
(r'   c                  X    e Zd ZdZddZddZddZddZddZddZ	ddZ
dd	Zdd
Zy)r   ak  
  Wraps a TestResult to make it thread safe.

  We do this by accumulating API calls and applying them in a batch under a
  lock at the conclusion of each test case.

  We duck type instead of inheriting from TestResult because we aren't actually
  a perfect implementation of TestResult, and would rather get a loud error
  for things we haven't implemented.
  c                .    || _         || _        g | _        y N)locktest_resultactions)selfr5   r   s      r    __init__zThreadSafeTestResult.__init__}   s    DID-/DLr'   c                ~    t         j                  d|j                                t        j                         | _        y )NzTest start: %s)loggerinfoidtime
start_timer8   r   s     r    	startTestzThreadSafeTestResult.startTest   s$    
KK $''),iikDOr'   c                0    t         j                  d|j                                t        j                          j                  5  t         j                  dd       }	  fd j                  _         j                  j                  |        j                  D ]	  } |         fd j                  _         j                  j                  |       || j                  _        	 d d d        y # || j                  _        w w xY w# 1 sw Y   y xY w)NzTest stop: %stime_getterc                      j                   S r4   )r?   )r8   s   r    <lambda>z/ThreadSafeTestResult.stopTest.<locals>.<lambda>   s    t r'   c                      S r4   r&   )	stop_times   r    rE   z/ThreadSafeTestResult.stopTest.<locals>.<lambda>   s    y r'   )r;   r<   r=   r>   r5   r   r6   rC   rA   r7   stopTest)r8   r   rC   callbackrG   s   `   @r    rH   zThreadSafeTestResult.stopTest   s    
KK+		I	 5 D,,mTBk	5'>$""4( 	H
*	'8$!!$'")4$


&5 5 ")4$


& #5 5s%   D A6C3D3D		DDc                F      j                   j                   fd       y )Nc                 :     j                   j                        S r4   )r6   
addSuccessr@   s   r    rE   z1ThreadSafeTestResult.addSuccess.<locals>.<lambda>   s     0 0 ; ;D A r'   r7   appendr@   s   ``r    rL   zThreadSafeTestResult.addSuccess   s    LLABr'   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r4   )r6   addSkip)reasonr8   r   s   r    rE   z.ThreadSafeTestResult.addSkip.<locals>.<lambda>   s     0 0 8 8v F r'   rM   )r8   r   rR   s   ```r    rQ   zThreadSafeTestResult.addSkip       LLFGr'   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r4   )r6   addErrorerrr8   r   s   r    rE   z/ThreadSafeTestResult.addError.<locals>.<lambda>   s     0 0 9 9$ D r'   rM   r8   r   rX   s   ```r    rV   zThreadSafeTestResult.addError   s    LLDEr'   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r4   )r6   
addFailurerW   s   r    rE   z1ThreadSafeTestResult.addFailure.<locals>.<lambda>   s     0 0 ; ;D# F r'   rM   rY   s   ```r    r\   zThreadSafeTestResult.addFailure   rS   r'   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r4   )r6   addExpectedFailurerW   s   r    rE   z9ThreadSafeTestResult.addExpectedFailure.<locals>.<lambda>   s     0 0 C CD# N r'   rM   rY   s   ```r    r_   z'ThreadSafeTestResult.addExpectedFailure   s    LLNOr'   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r4   )r6   addDuration)elapsedr8   r   s   r    rE   z2ThreadSafeTestResult.addDuration.<locals>.<lambda>   s     0 0 < <T7 K r'   rM   )r8   r   rc   s   ```r    rb   z ThreadSafeTestResult.addDuration   s    LLKLr'   N)r5   zthreading.Lockr   unittest.TestResult)r   unittest.TestCase)r   re   rR   str)__name__
__module____qualname____doc__r9   rA   rH   rL   rQ   rV   r\   r_   rb   r&   r'   r    r   r   r   s:    	0
"5&CHFHPMr'   c                  2     e Zd ZdZd fdZdd fdZ xZS )JaxTestSuitezRuns tests in parallel using threads if TEST_NUM_THREADS is > 1.

  Caution: this test suite does not run setUpClass or setUpModule methods if
  thread parallelism is enabled.
  c                6    t         |   t        |             y r4   )superr9   list)r8   suiter   s     r    r9   zJaxTestSuite.__init__   s    	GT%[!r'   c                ^   t         j                  dk  rt        |         S t	        j
                          t        t         j                        t        j                         g fd5   |        D ]  }|j                           	 d d d        S # 1 sw Y   S xY w)Nr   c                    t        | t        j                        r| D ]
  } |        yt              }j	                  j                  t        | |             y)z4Recursively runs tests in a test suite or test case.N)
isinstancer,   	TestSuiter   rN   submitr!   )r   subtestr6   executorfuturesr5   r   run_tests      r    ry   z"JaxTestSuite.run.<locals>.run_test   sQ    	D(,,	- 	G
7
	 +48x}dKHIr'   )
r#   r$   rn   runr	   #install_threadsafe_warning_handlersr   	threadingLockr   )	r8   r   debugfuturerw   rx   r5   ry   r   s	    `  @@@@r    rz   zJaxTestSuite.run   s    "W[  99;!"2"8"89H>>DGJ J 
 tn &
 M
 Ms   7 B""B,)rp   zunittest.TestSuite)F)r   rd   r~   boolreturnrd   )rg   rh   ri   rj   r9   rz   __classcell__r   s   @r    rl   rl      s    " r'   rl   c                  "     e Zd ZeZ fdZ xZS )JaxTestLoaderc                   t         |   |      }t        j                  rRt	        j
                  t        j                        }|D cg c]$  }|j                  |j                   d|       r|& }}t        j                  rRt	        j
                  t        j                        }|D cg c]$  }|j                  |j                   d|       s|& }}|S c c}w c c}w )N.)	rn   getTestCaseNames_TEST_TARGETSr$   recompilesearchrg   _EXCLUDE_TEST_TARGETS)r8   testCaseClassnamespatternnamer   s        r    r   zJaxTestLoader.getTestCaseNames   s    G$]3E

=../g % F..M$:$:#;1TF!CD  Fe F""

0667g % Jnn(>(>'?q%GH  Je JLFJs   )C*)C)rg   rh   ri   rl   
suiteClassr   r   r   s   @r    r   r      s    *
 
r'   r   )r   re   r   r   ))rj   
__future__r   collections.abcr   concurrent.futuresr   
contextlibr   loggingosr   r|   r>   r,   absl.testingr   jax._srcr   r	   r
   	getLoggerrg   r;   string_flaggetenvr   r   int_flagintr#   Mutexr   r!   r(   r1   r   rt   rl   
TestLoaderr   r&   r'   r    <module>r      s0  	 # $ 1 %  	 	    !  & 			8	$ #"")")).3; +**)"))$>C>  #6??C			*@# FG
<  tzz|#  (7M 7Mt"8%% "JH'' r'   