o
    ¯bu+  ã                   @   s°   d Z ddlZddlZddlmZ ddlmZ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mZ ddlmZ dd	lmZ dd
lmZ G dd„ deƒZeƒ  e ¡ ¡ dS )z/
Tests for implementations of L{IReactorCore}.
é    N)Ú	FrameType)ÚCallableÚListÚOptionalÚTupleÚUnionÚcast)ÚFileDescriptor)ÚDeferred)ÚReactorAlreadyRunningÚReactorNotRestartable)ÚReactorBuilder)ÚFailure)ÚSynchronousTestCasec                   @   sœ   e Zd ZdZd dd„Zd dd„Zd dd	„Zd d
d„Zd dd„Zd dd„Z	d dd„Z
d dd„Zd dd„Zd dd„Zd dd„Zd dd„Zd dd„Zd dd„ZdS )!ÚSystemEventTestsBuilderz{
    Builder defining tests relating to L{IReactorCore.addSystemEventTrigger}
    and L{IReactorCore.fireSystemEvent}.
    ÚreturnNc                 C   s    |   ¡ }tt| ƒ t|j¡ dS )zu
        C{reactor.stop()} raises L{RuntimeError} when called when the reactor
        has not been started.
        N)ÚbuildReactorr   r   ÚassertRaisesÚRuntimeErrorÚstop©ÚselfÚreactor© r   úA/usr/lib/python3/dist-packages/twisted/internet/test/test_core.pyÚtest_stopWhenNotStarted   s   z/SystemEventTestsBuilder.test_stopWhenNotStartedc                 C   s6   |   ¡ }| |j¡ |  |¡ tt| ƒ t|j¡ dS )zr
        C{reactor.stop()} raises L{RuntimeError} when called after the reactor
        has been stopped.
        N)r   ÚcallWhenRunningr   Ú
runReactorr   r   r   r   r   r   r   r   Útest_stopWhenAlreadyStopped$   s   
z3SystemEventTestsBuilder.test_stopWhenAlreadyStoppedc                 C   sX   |   ¡ }g }| |jd¡ | |jd¡ | |j¡ |  |¡ tt| ƒ |ddg¡ dS )zm
        Functions are run in the order that they were passed to
        L{reactor.callWhenRunning}.
        ÚfirstÚsecondN)r   r   Úappendr   r   r   r   ÚassertEqual)r   r   Úeventsr   r   r   Útest_callWhenRunningOrder.   s   
z1SystemEventTestsBuilder.test_callWhenRunningOrderc                    sª   |   ¡ ‰ i ‰d‡ ‡fdd„}d‡ ‡fdd„}d‡ ‡fdd„}tt| ƒ}ˆ  d	d
|¡ ˆ  dd
|¡ ˆ  dd
|¡ ˆ  ˆ j¡ | ˆi ¡ |  ˆ ¡ | ˆddddœ¡ dS )z¿
        The reactor is not running when C{"before"} C{"startup"} triggers are
        called and is running when C{"during"} and C{"after"} C{"startup"}
        triggers are called.
        r   Nc                      ó   ˆ j ˆd< d S ©NÚbefore©Úrunningr   ©r   Ústater   r   ÚbeforeStartupD   ó   zKSystemEventTestsBuilder.test_runningForStartupEvents.<locals>.beforeStartupc                      r%   )NÚduringr(   r   r*   r   r   ÚduringStartupG   r-   zKSystemEventTestsBuilder.test_runningForStartupEvents.<locals>.duringStartupc                      r%   ©NÚafterr(   r   r*   r   r   ÚafterStartupJ   r-   zJSystemEventTestsBuilder.test_runningForStartupEvents.<locals>.afterStartupr'   Ústartupr.   r1   FT)r'   r.   r1   ©r   N)r   r   r   ÚaddSystemEventTriggerr   r   r"   r   )r   r,   r/   r2   ÚtestCaser   r*   r   Útest_runningForStartupEvents;   s   

z4SystemEventTestsBuilder.test_runningForStartupEventsc                    sÐ   |   ¡ }d‰ d‡ fdd„}d‡ fdd„}| dd|¡ | d	d|¡ g ‰d
tdtttgdf ddf‡ ‡fdd„}tt| ƒ}| td|¡ | 	|j
¡ | ˆ ¡ | ˆg ¡ |  |¡ | dˆ¡ | ˆ d	¡ dS )ze
        Signal handlers are installed in responsed to the C{"during"}
        C{"startup"}.
        Nr   c                      ó   d‰ d S r&   r   r   ©Úphaser   r   r,   _   ó   zXSystemEventTestsBuilder.test_signalHandlersInstalledDuringStartup.<locals>.beforeStartupc                      r8   r0   r   r   r9   r   r   r2   c   r;   zWSystemEventTestsBuilder.test_signalHandlersInstalledDuringStartup.<locals>.afterStartupr'   r3   r1   ÚsignumÚactionc                    s   ˆ  ˆ ¡ d S ©N©r!   )r<   r=   ©r:   ÚsawPhaser   r   Ú
fakeSignall   r-   zUSystemEventTestsBuilder.test_signalHandlersInstalledDuringStartup.<locals>.fakeSignalÚsignalr4   )r   r5   Úintr   r   r   r   ÚpatchrC   r   r   ÚassertIsNoner"   r   ÚassertIn)r   r   r,   r2   rB   r6   r   r@   r   Ú)test_signalHandlersInstalledDuringStartupW   s    (


zASystemEventTestsBuilder.test_signalHandlersInstalledDuringStartupc                    s~   |   ¡ }g ‰ | dd‡ fdd„¡ | dd‡ fdd„¡ | dd‡ fdd„¡ | |j¡ |  |¡ tt| ƒ ˆ g d	¢¡ d
S )z†
        C{reactor.stop()} fires all three phases of shutdown event triggers
        before it makes C{reactor.run()} return.
        r'   Úshutdownc                      ó
   ˆ   d¡S ©N©r'   rI   r?   r   ©r#   r   r   Ú<lambda>�   ó   
 zASystemEventTestsBuilder.test_stopShutDownEvents.<locals>.<lambda>r.   c                      rJ   )N©r.   rI   r?   r   rM   r   r   rN   „   rO   r1   c                      rJ   )N©r1   rI   r?   r   rM   r   r   rN   ‡   rO   )rL   rP   rQ   N)r   r5   r   r   r   r   r   r"   r   r   rM   r   Útest_stopShutDownEventsy   s"   ÿÿÿ

þz/SystemEventTestsBuilder.test_stopShutDownEventsc                    sl   |   ¡ ‰g ‰ ˆ ddˆ jd¡ d	‡ ‡fdd„}tt| ƒ}ˆ |¡ | ˆ g ¡ |  ˆ¡ | ˆ ddg¡ dS )
zl
        C{"before"} C{"shutdown"} triggers are not run synchronously from
        L{reactor.stop}.
        r'   rI   zbefore shutdownr   Nc                      s   ˆ  ¡  ˆ  d¡ d S )NÚstopped)r   r!   r   ©r#   r   r   r   ÚstopIt›   s   zPSystemEventTestsBuilder.test_shutdownFiresTriggersAsynchronously.<locals>.stopItrS   r4   )r   r5   r!   r   r   r   r"   r   )r   rU   r6   r   rT   r   Ú(test_shutdownFiresTriggersAsynchronously�   s   
ÿ


z@SystemEventTestsBuilder.test_shutdownFiresTriggersAsynchronouslyc                    s˜   dg‰G dd„ dt ƒ}G ‡fdd„dt ƒ}tt| ƒ}|  ¡ }t|ƒ |ƒ gƒ‰ ‡ fdd„|_| |j¡ |  |¡ | 	t
| t¡ƒd¡ | ˆd	 ¡ d
S )zÔ
        A L{IFileDescriptor.connectionLost} implementation which raises an
        exception does not prevent the remaining L{IFileDescriptor}s from
        having their C{connectionLost} method called.
        Fc                   @   s   e Zd Zdeddfdd„ZdS )zZSystemEventTestsBuilder.test_shutdownDisconnectsCleanly.<locals>.ProblematicFileDescriptorÚreasonr   Nc                 S   s   t dƒ‚)Nzsimulated connectionLost error)r   ©r   rW   r   r   r   ÚconnectionLost°   s   ziSystemEventTestsBuilder.test_shutdownDisconnectsCleanly.<locals>.ProblematicFileDescriptor.connectionLost©Ú__name__Ú
__module__Ú__qualname__r   rY   r   r   r   r   ÚProblematicFileDescriptor¯   s    r^   c                       s"   e Zd Zdeddf‡ fdd„ZdS )zQSystemEventTestsBuilder.test_shutdownDisconnectsCleanly.<locals>.OKFileDescriptorrW   r   Nc                    s   dˆ d< d S )NTr   r   rX   ©ÚlostOKr   r   rY   ´   s   z`SystemEventTestsBuilder.test_shutdownDisconnectsCleanly.<locals>.OKFileDescriptor.connectionLostrZ   r   r_   r   r   ÚOKFileDescriptor³   s    ra   c                      s   ˆ S r>   r   r   )Úfdsr   r   rN   À   s    zISystemEventTestsBuilder.test_shutdownDisconnectsCleanly.<locals>.<lambda>é   r   N)r	   r   r   r   ÚiterÚ	removeAllr   r   r   r"   ÚlenÚflushLoggedErrorsr   Ú
assertTrue)r   r^   ra   r6   r   r   )rb   r`   r   Útest_shutdownDisconnectsCleanly¦   s   

z7SystemEventTestsBuilder.test_shutdownDisconnectsCleanlyc                    sZ   g ‰ t t| ƒ‰d‡ ‡‡fdd„}|  ¡ ‰ˆ |¡ ˆ ˆj¡ |  ˆ¡ ˆ ˆ dg¡ dS )z{
        C{reactor.run()} raises L{ReactorAlreadyRunning} when called when
        the reactor is already running.
        r   Nc                      ó   ˆ  tˆj¡ ˆ  d¡ d S ©NÚtested)r   r   Úrunr!   r   ©r#   r   r6   r   r   ÚreentrantRunÏ   ó   z>SystemEventTestsBuilder.test_multipleRun.<locals>.reentrantRunrl   r4   )r   r   r   r   r   r   r"   )r   ro   r   rn   r   Útest_multipleRunÆ   s   


z(SystemEventTestsBuilder.test_multipleRunc                    sp   g ‰dt t f‡ ‡‡fdd„}dtddf‡‡fdd„‰ |  ¡ ‰ˆ dd	|¡ |  ˆ¡ tt| ƒ ˆd
dg¡ dS )zØ
        When there is a C{'before'} C{'startup'} trigger which returns an
        unfired L{Deferred}, C{reactor.run()} starts the reactor and does not
        return until after C{reactor.stop()} is called
        r   c                     s.   ˆ  d¡ tƒ } |  ˆ ¡ ˆ d| jd ¡ | S )NÚtriggerr   )r!   r
   ÚaddCallbackÚ	callLaterÚcallback)Úd©ru   r#   r   r   r   rr   á   s
   

zUSystemEventTestsBuilder.test_runWithAsynchronousBeforeStartupTrigger.<locals>.triggerÚignoredNc                    ó   ˆ   d¡ ˆ ¡  d S )Nru   )r!   r   )rx   rT   r   r   ru   è   ó   
zVSystemEventTestsBuilder.test_runWithAsynchronousBeforeStartupTrigger.<locals>.callbackr'   r3   rr   ru   )r
   Úobjectr   r5   r   r   r   r"   )r   rr   r   rw   r   Ú,test_runWithAsynchronousBeforeStartupTriggerÙ   s   
zDSystemEventTestsBuilder.test_runWithAsynchronousBeforeStartupTriggerc                 C   sT   |   ¡ }| d|j¡}t ¡ }| d¡ t ¡ | }tt| ƒ |dk ¡ | ¡  dS )z6
        C{reactor.iterate()} does not block.
        é   r   é   N)	r   rt   ÚcrashÚtimeÚiterater   r   rh   Úcancel)r   r   ÚtÚstartÚelapsedr   r   r   Útest_iterateñ   s   
z$SystemEventTestsBuilder.test_iteratec                    sd   |   ¡ }g ‰ | dd‡ fdd„¡ | |jd|j¡ |  |¡ tt| ƒ}| |j	¡ | ˆ d¡ dS )zc
        C{reactor.crash()} stops the reactor and does not fire shutdown
        triggers.
        r'   rI   c                      rJ   rK   r?   r   rM   r   r   rN     rO   z4SystemEventTestsBuilder.test_crash.<locals>.<lambda>r   z8Shutdown triggers invoked but they should not have been.N)
r   r5   r   rt   r   r   r   r   ÚassertFalser)   )r   r   r6   r   rM   r   Ú
test_crashÿ   s   ÿ

ÿz"SystemEventTestsBuilder.test_crashc                    sn   g ‰ d	‡ ‡fdd„}|   ¡ ‰ˆ |¡ |  ˆ¡ d	‡ ‡fdd„}ˆ |¡ |  ˆ¡ tt| ƒ ˆ ddg¡ dS )
zp
        C{reactor.run()} restarts the reactor after it has been stopped by
        C{reactor.crash()}.
        r   Nc                      ry   )Nr   )r!   r   r   rT   r   r   r     rz   z9SystemEventTestsBuilder.test_runAfterCrash.<locals>.crashc                      s   ˆ   dˆjf¡ ˆ ¡  d S )Nr   )r!   r)   r   r   rT   r   r   r      s   z8SystemEventTestsBuilder.test_runAfterCrash.<locals>.stopr   )r   Tr4   )r   r   r   r   r   r"   )r   r   r   r   rT   r   Útest_runAfterCrash  s   



z*SystemEventTestsBuilder.test_runAfterCrashc                    s^   g ‰ t t| ƒ‰d‡ ‡‡fdd„}|  ¡ ‰ˆ ˆj¡ ˆ dd|¡ |  ˆ¡ ˆ ˆ dg¡ dS )	z“
        C{reactor.run()} raises L{ReactorNotRestartable} when called when
        the reactor is being run after getting stopped priorly.
        r   Nc                      rj   rk   )r   r   rm   r!   r   rn   r   r   Úrestart1  rp   z:SystemEventTestsBuilder.test_runAfterStop.<locals>.restartr1   rI   rl   r4   )r   r   r   r   r   r5   r   r"   )r   rŠ   r   rn   r   Útest_runAfterStop(  s   

z)SystemEventTestsBuilder.test_runAfterStopr4   )r[   r\   r]   Ú__doc__r   r   r$   r7   rH   rR   rV   ri   rq   r|   r†   rˆ   r‰   r‹   r   r   r   r   r      s     






"


 



r   )rŒ   rC   r€   Útypesr   Útypingr   r   r   r   r   r   Útwisted.internet.abstractr	   Útwisted.internet.deferr
   Útwisted.internet.errorr   r   Ú#twisted.internet.test.reactormixinsr   Útwisted.python.failurer   Útwisted.trial.unittestr   r   ÚglobalsÚupdateÚmakeTestCaseClassesr   r   r   r   Ú<module>   s      (