
    Zd                        d 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
Z
ddl
mZ edk     rdZ
ddlmZ n# e$ r dZ
Y nw xY wddlZddlZddlZej                            d          Zd	Zd
 Zej        d             Zd Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Z d Z!dddddZ" ee"d          Z#ej        d             Z$ej        d             Z%ej        d             Z&ej        d             Z'dS )z!pytest configuration and fixtures    N)partial)Thread)version_info)   )IOLoopZMQ_TEST_TIMEOUT
   c                     | D ]K}t          j        |j                  r|                    d           t          j        |j                  rJ LdS )a+  This function is automatically run by pytest passing all collected test
    functions.
    We use it to add asyncio marker to all async tests and assert we don't use
    test functions that are async generators which wouldn't make sense.
    It is no longer required with pytest-asyncio >= 0.17
    asyncioN)inspectiscoroutinefunctionobj
add_markerisasyncgenfunction)itemsitems     2lib/python3.11/site-packages/zmq/tests/conftest.pypytest_collection_modifyitemsr      s`      8 8&tx00 	'OOI&&&-dh7777778 8    c                    K   t           t          j                     t          j                    t          j                    | u sJ j        | u sJ fd}|                    |           S )z4Create tornado io_loop on current asyncio event loopNc                  4                          d           d S )NT)all_fds)close)io_loops   r   _closezio_loop.<locals>._close4   s    d#####r   )	tornadopytestskipr   currentr   get_event_loopasyncio_loopaddfinalizer)
event_looprequestr   r   s      @r   r   r   +   s       nG!##z1111:----$ $ $ $ $    Nr   c                    t          | j                  }d|_        |                                 |                    |           |                                r.dt          j        j        j	        _
        t          d|  d          dS )z"Terminate a context with a timeout)targetTtimeoutNzcontext z8 could not terminate, open sockets likely remain in test)r   termdaemonstartjoinis_alivezmqsugarcontextContext	_instanceRuntimeError)ctxr(   ts      r   term_contextr6   ;   s    chAAHGGIIIFF7Fzz|| 
.2	!+TsTTT
 
 	

 
r   c               #      K   t          j                    } | V  |                                  t          t          j         j                  i k    sJ d S N)r   new_event_loopr   dictr.   
_selectors)loops    r   r#   r#   I   sN      !##D
JJJJJLLL&''2------r   c                      t          t          d          rt          sdS d } t          j        t          j        |            t          j        t                     dS )z~Set timeout using SIGALRM

    Avoids infinite hang in context.term for an unclean context,
    raising an error instead.
    SIGALRMNc                  2    t          dt           d          )NzTest did not complete in z seconds)TimeoutErrortest_timeout_seconds)argss    r   _alarm_timeoutz'sigalrm_timeout.<locals>._alarm_timeout\   s    U7KUUUVVVr   )hasattrsignalrA   r>   alarm)rC   s    r   sigalrm_timeoutrG   R   sb     69%% -A W W W M&..111
L%&&&&&r   c                      t           j        S )zaContext class fixture

    Override in modules to specify a different class (e.g. zmq.green)
    )r.   r1    r   r   r1   r1   c   s     ;r   c              #      K   t                      }|V  |D ]B}	 t          |t                     # t          $ r dt          j        j        j        _         w xY wdS )zFFixture to track contexts used in tests

    For cleanup purposes
    N)	setr6   teardown_timeout	Exceptionr.   r/   r0   r1   r2   )rG   contextsr4   s      r   rN   rN   l   sz       uuH
NNN  	.//// 	 	 	26CI%/	 s	   0'Ac                 D     |             }|                     |           |S )zFixture for shared context)add)r1   rN   r4   s      r   r0   r0   }   s&     '))CLLJr   c              #      K   g }|V  |D ]}|                      |j                   |D ]}|                    d           d S )Nr   )linger)rP   r0   r   )rN   socketssockets      r   rS   rS      sl      G
MMM % %V^$$$$   A r   c                       fd}|S )z:Fixture to create sockets, while tracking them for cleanupc                  L     j         | i |}                    |           |S r8   )rT   append)rB   kwargssr0   rS   s      r   
new_socketzsocket.<locals>.new_socket   s0    GND+F++qr   rI   )r0   rS   rZ   s   `` r   rT   rT      s*         
 r   c              #   *  K   	 d V  t          j        dt          j        |            d           d S # t          j        $ rO}|j        | k    s9J dt          j        |            dt          j        |j                               Y d }~d S d }~ww xY w)Nz	Expected z, no error raisedzwrong error raised, expected z got )r   failr.   ZMQErrorerrno)r^   es     r   assert_raises_errnor`      s      H 	FU 3 3FFFGGGGG < ] ] ]Gu\3<+>+>\\S\RSRYEZEZ\\ ]s   4 BABBr   F)r(   flags	multipartc                *   t          j                    dk    rt          j        d           t          j        | gg g |          \  }}}|s
J d            t           j        |                    dd          z  |d<   |r| j        n| j        } |dd|i|S )zIcall recv[_multipart] in a way that raises if there is nothing to receive)      r   g?r'   zShould have received a messagera   r   rI   )	r.   zmq_version_infotimesleepselectDONTWAITgetrecv_multipartrecv)	rT   r(   ra   rb   rX   rwxrm   s	            r   rm   rm      s    
** 	
3j&2r7;;;GAq!......lVZZ%;%;;F7O$->6  6;D4&&e&v&&&r   T)rb   c                 B     t           j        t           j        df fd	}|S )Nztcp://127.0.0.1c                      |           }d|_         |                    |          } |          }d|_         |                    | d|            ||fS )z/Create a bound socket pair using a random port.r   :)rR   bind_to_random_portconnect)type1type2	interfaces1ports2rT   s         r   create_bound_pairz,create_bound_pair.<locals>.create_bound_pair   sg    VE]]	%%i00VE]]	


i(($(()))2vr   )r.   PAIR)rT   r|   s   ` r   r|   r|      s5     #DU       r   c                      |             S r8   rI   r|   s    r   
bound_pairr      s    r   c                 B     | t           j        t           j                  S r8   )r.   PUSHPULLr   s    r   	push_pullr      s    SXsx000r   c                 B     | t           j        t           j                  S r8   )r.   DEALERROUTERr   s    r   dealer_routerr      s    SZ444r   )(__doc__r   r   osrE   rg   	functoolsr   	threadingr   r   r   tornado.ioloopr   ImportErrorr   r.   zmq.asyncioenvironrk   rA   rL   r   fixturer   r6   r#   rG   r1   rN   r0   rS   rT   r`   rm   rl   r|   r   r   r   rI   r   r   <module>r      s   ' '   				              &NNN$$$$$$ d%%%%%%%    GGG  



    z~~&899  
8 
8 
8   
 
 
 . . . ' ' '            	 	 	   H H H Q% ' ' ' ' '  ...       1 1 1 5 5 5 5 5s   
= AA