a
    ze	                     @   sf   d 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 Z	G dd	 d	ej
Zed
krbe  dS )zTests for asyncio/threads.py    N)
ContextVar)mock)utilsc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   4/usr/lib/python3.9/test/test_asyncio/test_threads.pytearDownModule   s    r	   c                       sX   e Zd Z fddZ fddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
  ZS )ToThreadTestsc                    s$   t    t | _t| j d S r   )supersetUpr   Znew_event_looploopset_event_loopself	__class__r   r   r      s    

zToThreadTests.setUpc                    s:   | j | j   | j   td  d | _ t   d S r   )r   run_until_completeZshutdown_default_executorcloser   r   r   tearDownr   r   r   r   r      s    

zToThreadTests.tearDownc                 C   s&   dd }| j | }| |d d S )Nc                      s   t tddgI d H S )N(      )r   	to_threadsumr   r   r   r   main   s    z*ToThreadTests.test_to_thread.<locals>.main*   )r   r   assertEqualr   r   resultr   r   r   test_to_thread   s    zToThreadTests.test_to_threadc                    sR   dd   fdd}|  td | j|  W d    n1 sD0    Y  d S )Nc                   S   s   t dd S )Ntest)RuntimeErrorr   r   r   r   raise_runtime%   s    z=ToThreadTests.test_to_thread_exception.<locals>.raise_runtimec                      s   t  I d H  d S r   r   r   r   r"   r   r   r   (   s    z4ToThreadTests.test_to_thread_exception.<locals>.mainr    )ZassertRaisesRegexr!   r   r   r   r   r   r$   r   test_to_thread_exception$   s    z&ToThreadTests.test_to_thread_exceptionc                    s.   t    fdd}| j|     d S )Nc                      s   t  I d H  d S r   r#   r   funcr   r   r   1   s    z/ToThreadTests.test_to_thread_once.<locals>.main)r   Mockr   r   Zassert_called_oncer%   r   r'   r   test_to_thread_once.   s    z!ToThreadTests.test_to_thread_oncec                    s4   t    fdd}| j|  |  jd d S )Nc                     s:   g } t dD ]}t }| | qtj|  I d H  d S )N
   )ranger   r   appendZgather)Zfuts_Zfutr'   r   r   r   :   s
    
z5ToThreadTests.test_to_thread_concurrent.<locals>.mainr+   )r   r)   r   r   r   Z
call_countr%   r   r'   r   test_to_thread_concurrent7   s    z'ToThreadTests.test_to_thread_concurrentc                    s4   t    fdd}| j|   jddd d S )Nc                      s   t j dddI d H  d S )Nr    TZ	somethingr#   r   r'   r   r   r   H   s    z6ToThreadTests.test_to_thread_args_kwargs.<locals>.mainr    Tr0   )r   r)   r   r   Zassert_called_once_withr%   r   r'   r   test_to_thread_args_kwargsD   s    z(ToThreadTests.test_to_thread_args_kwargsc                    s@   t dfdd  fdd}| j| }| |d d S )Ntest_ctxc                      s      S r   )getr   )r2   r   r   get_ctxQ   s    z9ToThreadTests.test_to_thread_contextvars.<locals>.get_ctxc                      s    d t I d H S )Nparrot)setr   r   r   r4   r2   r   r   r   T   s    
z6ToThreadTests.test_to_thread_contextvars.<locals>.mainr5   )r   r   r   r   r   r   r7   r   test_to_thread_contextvarsN   s
    z(ToThreadTests.test_to_thread_contextvars)__name__
__module____qualname__r   r   r   r&   r*   r/   r1   r8   __classcell__r   r   r   r   r
      s   
	
r
   __main__)__doc__r   ZunittestZcontextvarsr   r   Ztest.test_asyncior   Z
test_utilsr	   ZTestCaser
   r9   r   r   r   r   r   <module>   s   M