o
    ¯bo$  ã                   @   sÆ   d Z ddlZddlZddlmZmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ d
Zd
Zzej d¡rMddlmZmZ dZdZW n	 eyW   Y nw G dd„ deeƒZdS )z/
Tests for L{twisted.internet.asyncioreactor}.
é    N)ÚDefaultEventLoopPolicyÚFutureÚSelectorEventLoopÚset_event_loopÚset_event_loop_policy)ÚskipIf)ÚAsyncioSelectorReactor)Úplatform)ÚSynchronousTestCaseé   )ÚReactorBuilderFÚwin32)ÚWindowsProactorEventLoopPolicyÚWindowsSelectorEventLoopPolicyTc                	   @   s*  e Zd ZdZeƒ  ¡ ZeeeƒZ	dd„ Z
ee	 d eeƒejjejje ¡ ¡ƒdd„ ƒZee	 d eeƒejjejje ¡ ¡ƒdd„ ƒZee	d	 eeƒejjejje ¡ ¡ƒd
d„ ƒZee dƒdd„ ƒZee dƒdd„ ƒZee dƒdd„ ƒZee dƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Z d S )!ÚAsyncioSelectorReactorTestsz*
    L{AsyncioSelectorReactor} tests.
    c                    sX   t ƒ }g ‰‡ ‡fdd„}| |¡ | d¡ |  ˆg ¡ | jˆ dd� |  ˆdg¡ dS )zn
        Ensure that C{reactor} has an event loop that works
        properly with L{asyncio.Future}.
        c                    s   ˆ  |  ¡ ¡ ˆ  ¡  d S ©N)ÚappendÚresultÚstop)Úfuture©Úreactorr   © úK/usr/lib/python3/dist-packages/twisted/internet/test/test_asyncioreactor.pyÚ	completed7   s   zRAsyncioSelectorReactorTests.assertReactorWorksWithAsyncioFuture.<locals>.completedTr   )ÚtimeoutN)r   Úadd_done_callbackÚ
set_resultÚassertEqualÚ
runReactor)Úselfr   r   r   r   r   r   Ú#assertReactorWorksWithAsyncioFuture/   s   

z?AsyncioSelectorReactorTests.assertReactorWorksWithAsyncioFuturezLdefault event loop: {}
is not of type SelectorEventLoop on Python {}.{} ({})c                 C   s   t ƒ }|  |¡ dS )a  
        L{AsyncioSelectorReactor} wraps the global policy's event loop
        by default.  This ensures that L{asyncio.Future}s and
        coroutines created by library code that uses
        L{asyncio.get_event_loop} are bound to the same loop.
        N)r   r!   ©r    r   r   r   r   Ú-test_defaultSelectorEventLoopFromGlobalPolicyB   s   zIAsyncioSelectorReactorTests.test_defaultSelectorEventLoopFromGlobalPolicyc                 C   ó0   t ƒ  ¡ }t|ƒ}t|ƒ |  |¡ tdƒ dS )zä
        If we use the L{asyncio.DefaultLoopPolicy} to create a new event loop,
        and then pass that event loop to a new L{AsyncioSelectorReactor},
        this reactor should work properly with L{asyncio.Future}.
        N)r   Únew_event_loopr   r   r!   r   ©r    Ú
event_loopr   r   r   r   Ú3test_newSelectorEventLoopFromDefaultEventLoopPolicyV   s
   

zOAsyncioSelectorReactorTests.test_newSelectorEventLoopFromDefaultEventLoopPolicyzHdefault event loop: {}
is of type SelectorEventLoop on Python {}.{} ({})c                 C   s   |   tt¡ dS )ay  
        On Windows Python 3.5 to 3.7, L{get_event_loop()} returns a
        L{WindowsSelectorEventLoop} by default.
        On Windows Python 3.8+, L{get_event_loop()} returns a
        L{WindowsProactorEventLoop} by default.
        L{AsyncioSelectorReactor} should raise a
        L{TypeError} if the default event loop is not a
        L{WindowsSelectorEventLoop}.
        N)ÚassertRaisesÚ	TypeErrorr   ©r    r   r   r   Ú1test_defaultNotASelectorEventLoopFromGlobalPolicyl   s   zMAsyncioSelectorReactorTests.test_defaultNotASelectorEventLoopFromGlobalPolicyz&WindowsProactorEventLoop not availablec                 C   s   t ƒ  ¡ }|  tt|¡ dS )zˆ
        L{AsyncioSelectorReactor} will raise a L{TypeError}
        if instantiated with a L{asyncio.WindowsProactorEventLoop}
        N)r   r%   r)   r*   r   )r    r'   r   r   r   Útest_WindowsProactorEventLoop‚   s   
z9AsyncioSelectorReactorTests.test_WindowsProactorEventLoopz(WindowsSelectorEventLoop only on Windowsc                 C   r$   )zR
        L{WindowsSelectorEventLoop} works with L{AsyncioSelectorReactor}
        N)r   r%   r   r   r!   r   r&   r   r   r   Útest_WindowsSelectorEventLoop�   s
   

z9AsyncioSelectorReactorTests.test_WindowsSelectorEventLoopz.WindowsProactorEventLoopPolicy only on Windowsc                 C   sF   t tƒ ƒ |  t¡� tƒ  W d  ƒ n1 sw   Y  t dƒ dS )z†
        L{AsyncioSelectorReactor} will raise a L{TypeError}
        if L{asyncio.WindowsProactorEventLoopPolicy} is default.
        N)r   r   r)   r*   r   r+   r   r   r   Ú#test_WindowsProactorEventLoopPolicy›   s
   
	ÿz?AsyncioSelectorReactorTests.test_WindowsProactorEventLoopPolicyz.WindowsSelectorEventLoopPolicy only on Windowsc                 C   s&   t tƒ ƒ tƒ }|  |¡ t dƒ dS )zy
        L{AsyncioSelectorReactor} will work if
        if L{asyncio.WindowsSelectorEventLoopPolicy} is default.
        N)r   r   r   r!   r"   r   r   r   Ú#test_WindowsSelectorEventLoopPolicy©   s   
	
z?AsyncioSelectorReactorTests.test_WindowsSelectorEventLoopPolicyc                 C   sH   t rttƒ ƒ tƒ }| ¡ }|  |d¡ |  |d¡ t r"tdƒ dS dS )z0L{seconds} should return a plausible epoch time.i á^l    #G4 N)Ú!hasWindowsSelectorEventLoopPolicyr   r   r   ÚsecondsÚassertGreaterÚ
assertLess)r    r   r   r   r   r   Útest_seconds·   s   
ÿz(AsyncioSelectorReactorTests.test_secondsc                    s’   t rttƒ ƒ tƒ ‰ dg‰‡ ‡fdd„}ˆ  ¡ }ˆ  d|¡}| d¡ ˆ  dˆ j¡ ˆ  ¡  |  	ˆd ¡ |  
ˆd | d¡ t rGtdƒ dS dS )zQ
        L{DelayedCall.reset()} properly reschedules timer to later time
        Nc                      ó   ˆ   ¡ ˆd< d S ©Nr   ©r2   r   ©r   Útimer_called_atr   r   Úon_timerÑ   ó   zJAsyncioSelectorReactorTests.test_delayedCallResetToLater.<locals>.on_timerr   ç      à?r   çš™™™™™Ù?)r1   r   r   r   r2   Ú	callLaterÚresetr   ÚrunÚassertIsNotNoner3   )r    r;   Ú
start_timeÚdcr   r9   r   Útest_delayedCallResetToLaterÆ   s   

ÿz8AsyncioSelectorReactorTests.test_delayedCallResetToLaterc                    sæ   t rttƒ ƒ tƒ ‰ dg‰‡ ‡fdd„}ˆ  ¡ }ˆ  d|¡}| d¡ ˆ  dˆ j¡ ddl}ddl	m
} | ¡ }||ƒ� ˆ  ¡  W d  ƒ n1 sKw   Y  |  | ¡ d¡ |  ˆd ¡ |  ˆd | d	¡ t rqtdƒ dS dS )
zS
        L{DelayedCall.reset()} properly reschedules timer to earlier time
        Nc                      r6   r7   r8   r   r9   r   r   r;   é   r<   zLAsyncioSelectorReactorTests.test_delayedCallResetToEarlier.<locals>.on_timerr=   r   r   )Úredirect_stderrÚ r>   )r1   r   r   r   r2   r?   r@   r   ÚioÚ
contextlibrF   ÚStringIOrA   r   ÚgetvaluerB   r4   )r    r;   rC   rD   rH   rF   Ústderrr   r9   r   Útest_delayedCallResetToEarlierß   s*   



ÿÿz:AsyncioSelectorReactorTests.test_delayedCallResetToEarlierc                 C   s°   t rttƒ ƒ t ¡ }t ¡  z6tt ¡ ƒ}d}tƒ }t	|ƒD ]
}| 
ddd„ ¡ q| ¡  tt ¡ ƒ}|  || | d¡ W |rEt ¡  n|rMt ¡  w w t rVtdƒ dS dS )zW
        L{AsyncioSelectorReactor.callLater()} doesn't leave cyclic references
        iè  r   c                   S   s   d S r   r   r   r   r   r   Ú<lambda>  s    zOAsyncioSelectorReactorTests.test_noCycleReferencesInCallLater.<locals>.<lambda>r   N)r1   r   r   ÚgcÚ	isenabledÚdisableÚlenÚget_objectsr   Úranger?   ÚrunUntilCurrentr4   Úenable)r    Úgc_was_enabledÚobjects_beforeÚtimer_countr   Ú_Úobjects_afterr   r   r   Ú!test_noCycleReferencesInCallLaterþ   s,   
€ÿ
ÿÿz=AsyncioSelectorReactorTests.test_noCycleReferencesInCallLaterN)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   Ú_defaultEventLoopÚ
isinstancer   Ú_defaultEventLoopIsSelectorr!   r   ÚformatÚtypeÚsysÚversion_infoÚmajorÚminorr	   ÚgetTyper#   r(   r,   Ú!hasWindowsProactorEventLoopPolicyr-   r1   r.   r/   r0   r5   rE   rM   r\   r   r   r   r   r   '   sz    

üý


üý

üý

ÿ
þ

þ

þ

r   )r`   rO   rf   Úasyncior   r   r   r   r   Úunittestr   Útwisted.internet.asyncioreactorr   Útwisted.python.runtimer	   Útwisted.trial.unittestr
   Úreactormixinsr   rk   r1   Ú
startswithr   r   ÚImportErrorr   r   r   r   r   Ú<module>   s*   €ÿ