
    uki              	      X   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!d"d       Z"d!d"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/home/cdr/jupyterlab/.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              #  l  K   t         j                  dk  s| sd yt        j                          t        j	                          t        j                          	 d t        j                          t        j                          y# t        j                          t        j                          w xY ww)aN  Decorator for tests that are not thread-safe.

  Args:
    condition: If True, mark the test as thread-unsafe. If False, the test
      runs normally without acquiring the write lock. Defaults to True.

  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   )	conditions    r    thread_unsafe_testr'   T   s}      q 		
!!#	   s   AB4B )B4*B11B4c                      fd}|S )zDecorator that marks a TestCase class as thread-hostile.

  Args:
    condition: If True, mark the test class as thread-hostile. If False, the
      test class runs normally. Defaults to True.
  c                j    t        | t        j                        sJ t        |              | _        | S N)
issubclassunittestTestCasetyper   )klassr&   s    r    fz#thread_unsafe_test_class.<locals>.ft   s/    eX../<e</$EL     )r&   r0   s   ` r    thread_unsafe_test_classr3   m   s     
(r1   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 r*   )locktest_resultactions)selfr6   r   s      r    __init__zThreadSafeTestResult.__init__   s    DID-/DLr1   c                ~    t         j                  d|j                                t        j                         | _        y )NzTest start: %s)loggerinfoidtime
start_timer9   r   s     r    	startTestzThreadSafeTestResult.startTest   s$    
KK $''),iikDOr1   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 r*   )r@   )r9   s   r    <lambda>z/ThreadSafeTestResult.stopTest.<locals>.<lambda>   s    t r1   c                      S r*   r2   )	stop_times   r    rF   z/ThreadSafeTestResult.stopTest.<locals>.<lambda>   s    y r1   )r<   r=   r>   r?   r6   r   r7   rD   rB   r8   stopTest)r9   r   rD   callbackrH   s   `   @r    rI   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 r*   )r7   
addSuccessrA   s   r    rF   z1ThreadSafeTestResult.addSuccess.<locals>.<lambda>   s     0 0 ; ;D A r1   r8   appendrA   s   ``r    rM   zThreadSafeTestResult.addSuccess   s    LLABr1   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r*   )r7   addSkip)reasonr9   r   s   r    rF   z.ThreadSafeTestResult.addSkip.<locals>.<lambda>   s     0 0 8 8v F r1   rN   )r9   r   rS   s   ```r    rR   zThreadSafeTestResult.addSkip       LLFGr1   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r*   )r7   addErrorerrr9   r   s   r    rF   z/ThreadSafeTestResult.addError.<locals>.<lambda>   s     0 0 9 9$ D r1   rN   r9   r   rY   s   ```r    rW   zThreadSafeTestResult.addError   s    LLDEr1   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r*   )r7   
addFailurerX   s   r    rF   z1ThreadSafeTestResult.addFailure.<locals>.<lambda>   s     0 0 ; ;D# F r1   rN   rZ   s   ```r    r]   zThreadSafeTestResult.addFailure   rT   r1   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r*   )r7   addExpectedFailurerX   s   r    rF   z9ThreadSafeTestResult.addExpectedFailure.<locals>.<lambda>   s     0 0 C CD# N r1   rN   rZ   s   ```r    r`   z'ThreadSafeTestResult.addExpectedFailure   s    LLNOr1   c                J      j                   j                   fd       y )Nc                 <    j                   j                         S r*   )r7   addDuration)elapsedr9   r   s   r    rF   z2ThreadSafeTestResult.addDuration.<locals>.<lambda>   s     0 0 < <T7 K r1   rN   )r9   r   rd   s   ```r    rc   z ThreadSafeTestResult.addDuration   s    LLKLr1   N)r6   zthreading.Lockr   unittest.TestResult)r   unittest.TestCase)r   rf   rS   str)__name__
__module____qualname____doc__r:   rB   rI   rM   rR   rW   r]   r`   rc   r2   r1   r    r   r   {   s:    	0
"5&CHFHPMr1   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 r*   )superr:   list)r9   suiter   s     r    r:   zJaxTestSuite.__init__   s    	GT%[!r1   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   rO   submitr!   )r   subtestr7   executorfuturesr6   r   run_tests      r    rz   z"JaxTestSuite.run.<locals>.run_test   sQ    	D(,,	- 	G
7
	 +48x}dKHIr1   )
r#   r$   ro   runr	   #install_threadsafe_warning_handlersr   	threadingLockr   )	r9   r   debugfuturerx   ry   r6   rz   r   s	    `  @@@@r    r{   zJaxTestSuite.run   s    "W[  99;!"2"8"89H>>DGJ J 
 tn &
 M
 Ms   7 B""B,)rq   zunittest.TestSuite)F)r   re   r   boolreturnre   )rh   ri   rj   rk   r:   r{   __classcell__r   s   @r    rm   rm      s    " r1   rm   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.)	ro   getTestCaseNames_TEST_TARGETSr$   recompilesearchrh   _EXCLUDE_TEST_TARGETS)r9   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)rh   ri   rj   rm   
suiteClassr   r   r   s   @r    r   r      s    *
 
r1   r   )r   rf   r   r   )T)r&   r   ))rk   
__future__r   collections.abcr   concurrent.futuresr   
contextlibr   loggingosr   r}   r?   r,   absl.testingr   jax._srcr   r	   r
   	getLoggerrh   r<   string_flaggetenvr   r   int_flagintr#   Mutexr   r!   r'   r3   r   ru   rm   
TestLoaderr   r2   r1   r    <module>r      s0  	 # $ 1 %  	 	    !  & 			8	$ #"")")).3; +**)"))$>C>  #6??C			*@# FG
<  tzz|#  07M 7Mt"8%% "JH'' r1   