
    Yf&                        d Z ddlZddlZddlmZmZmZ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 ddlmZ dd	lmZ d
Zd
Z	 ej                            d          rddlmZmZ dZdZn# e$ r Y nw xY w e                                            Z eee          Z e!                                  G d dee          Z"dS )z/
Tests for L{twisted.internet.asyncioreactor}.
    N)	AbstractEventLoopAbstractEventLoopPolicyDefaultEventLoopPolicyFutureSelectorEventLoopget_event_loopget_event_loop_policyset_event_loopset_event_loop_policy)skipIf)AsyncioSelectorReactor)platform)SynchronousTestCase   )ReactorBuilderFwin32)WindowsProactorEventLoopPolicyWindowsSelectorEventLoopPolicyTc            
          e Zd ZdZd ZdedefdZ ee	 d
                     ee          ej        j        ej        j         ej                                        d             Z ee	 d
                     ee          ej        j        ej        j         ej                                        d             Z ee	d	
                     ee          ej        j        ej        j         ej                                        d
             Z ee d          d             Z ee d          d             Z ee d          d             Z ee d          d             Zd Zd Zd Zd ZdS )AsyncioSelectorReactorTestsz*
    L{AsyncioSelectorReactor} tests.
    c                    t                      }g fd}|                    |           |                    d           |                     g            |                     d           |                     dg           dS )zn
        Ensure that C{reactor} has an event loop that works
        properly with L{asyncio.Future}.
        c                 ~                         |                                                                             d S N)appendresultstop)futurereactorr   s    [/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_asyncioreactor.py	completedzRAsyncioSelectorReactorTests.assertReactorWorksWithAsyncioFuture.<locals>.completed=   s.    MM&--//***LLNNNNN    Tr   )timeoutN)r   add_done_callback
set_resultassertEqual
runReactor)selfr   r   r    r   s    `  @r   #assertReactorWorksWithAsyncioFuturez?AsyncioSelectorReactorTests.assertReactorWorksWithAsyncioFuture5   s    
 	 	 	 	 	 	 	  +++$$$$+++$(((((r!   policyreturnc                     t                      t                      |                                | j        fd            }S )z
        Make a new asyncio loop from a policy for use with a reactor, and add
        appropriate cleanup to restore any global state.
        c                  l                                      t                      t                     d S r   )closer
   r   )existingLoopexistingPolicyr   s   r   cleanUpz4AsyncioSelectorReactorTests.newLoop.<locals>.cleanUpQ   s2    LLNNN<(((!.11111r!   )r   r	   new_event_loop
addCleanup)r'   r)   r0   r.   r/   r   s      @@@r   newLoopz#AsyncioSelectorReactorTests.newLoopH   sd    
 &''.00&&((		2 	2 	2 	2 	2 	2 
	2
 r!   zLdefault event loop: {}
is not of type SelectorEventLoop on Python {}.{} ({})c                 L    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   s     r   -test_defaultSelectorEventLoopFromGlobalPolicyzIAsyncioSelectorReactorTests.test_defaultSelectorEventLoopFromGlobalPolicyY   s)    " )**0099999r!   c                     |                      t                                }t          |          }t          |           |                     |           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)r3   r   r   r
   r(   r'   
event_loopr   s      r   3test_newSelectorEventLoopFromDefaultEventLoopPolicyzOAsyncioSelectorReactorTests.test_newSelectorEventLoopFromDefaultEventLoopPolicym   sQ      \\"8":":;;
(44z"""0099999r!   zHdefault event loop: {}
is of type SelectorEventLoop on Python {}.{} ({})c                 F    |                      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'   s    r   1test_defaultNotASelectorEventLoopFromGlobalPolicyzMAsyncioSelectorReactorTests.test_defaultNotASelectorEventLoopFromGlobalPolicy   s!    ( 	)%;<<<<<r!   z&WindowsProactorEventLoop not availablec                     |                      t                                }|                     t          t          |           dS )z
        L{AsyncioSelectorReactor} will raise a L{TypeError}
        if instantiated with a L{asyncio.WindowsProactorEventLoop}
        N)r3   r   r<   r=   r   )r'   r9   s     r   test_WindowsProactorEventLoopz9AsyncioSelectorReactorTests.test_WindowsProactorEventLoop   s;     \\"@"B"BCC
)%;ZHHHHHr!   z(WindowsSelectorEventLoop only on Windowsc                     |                      t                                }t          |          }t          |           |                     |           dS )zR
        L{WindowsSelectorEventLoop} works with L{AsyncioSelectorReactor}
        N)r3   r   r   r
   r(   r8   s      r   test_WindowsSelectorEventLoopz9AsyncioSelectorReactorTests.test_WindowsSelectorEventLoop   sQ     \\"@"B"BCC
(44z"""0099999r!   z.WindowsProactorEventLoopPolicy only on Windowsc                     t          t                                 |                     d            |                     t                    5  t                       ddd           dS # 1 swxY w Y   dS )z
        L{AsyncioSelectorReactor} will raise a L{TypeError}
        if L{asyncio.WindowsProactorEventLoopPolicy} is default.
        c                       t          d           S r   r    r!   r   <lambda>zQAsyncioSelectorReactorTests.test_WindowsProactorEventLoopPolicy.<locals>.<lambda>        5d ; ; r!   N)r   r   r2   r<   r=   r   r>   s    r   #test_WindowsProactorEventLoopPolicyz?AsyncioSelectorReactorTests.test_WindowsProactorEventLoopPolicy   s     	<>>???;;<<<y)) 	% 	%"$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   A((A,/A,z.WindowsSelectorEventLoopPolicy only on Windowsc                     t          t                                 |                     d            t                      }|                     |           dS )zy
        L{AsyncioSelectorReactor} will work if
        if L{asyncio.WindowsSelectorEventLoopPolicy} is default.
        c                       t          d           S r   rF   rG   r!   r   rH   zQAsyncioSelectorReactorTests.test_WindowsSelectorEventLoopPolicy.<locals>.<lambda>   rI   r!   N)r   r   r2   r   r(   r5   s     r   #test_WindowsSelectorEventLoopPolicyz?AsyncioSelectorReactorTests.test_WindowsSelectorEventLoopPolicy   sT     	<>>???;;<<<(**0099999r!   c                    t           r1t          t                                 |                     d            t	                      }|                                }|                     |d           |                     |d           dS )z0L{seconds} should return a plausible epoch time.c                       t          d           S r   rF   rG   r!   r   rH   z:AsyncioSelectorReactorTests.test_seconds.<locals>.<lambda>       $9$$?$? r!   i ^l    #G4 N)!hasWindowsSelectorEventLoopPolicyr   r   r2   r   secondsassertGreater
assertLess)r'   r   r   s      r   test_secondsz(AsyncioSelectorReactorTests.test_seconds   s    , 	A!"@"B"BCCCOO??@@@(**"" 	6:... 	
+++++r!   c                    t           r1t          t                                 |                     d            t	                      dgfd}                                }                    d|          }|                    d                               dj                   	                                 | 
                    d                    |                     d         |z
  d           dS )zQ
        L{DelayedCall.reset()} properly reschedules timer to later time
        c                       t          d           S r   rF   rG   r!   r   rH   zJAsyncioSelectorReactorTests.test_delayedCallResetToLater.<locals>.<lambda>   rP   r!   Nc                  6                                      d<   d S Nr   rR   r   timer_called_ats   r   on_timerzJAsyncioSelectorReactorTests.test_delayedCallResetToLater.<locals>.on_timer       !(!2!2OAr!   r         ?r   皙?)rQ   r   r   r2   r   rR   	callLaterresetr   runassertIsNotNonerS   )r'   r]   
start_timedcr   r\   s       @@r   test_delayedCallResetToLaterz8AsyncioSelectorReactorTests.test_delayedCallResetToLater   s    - 	A!"@"B"BCCCOO??@@@(**&	3 	3 	3 	3 	3 	3 __&&
q(++
!W\***_Q/000?1-
:C@@@@@r!   c                    t           rt          t                                 t                      dgfd}                                }                    d|          }|                    d                               dj                   ddl}ddl	m
} |                                } ||          5                                   ddd           n# 1 swxY w Y   |                     |                                d           |                     d                    |                     d         |z
  d           t           rt          d           dS dS )	zS
        L{DelayedCall.reset()} properly reschedules timer to earlier time
        Nc                  6                                      d<   d S rY   rZ   r[   s   r   r]   zLAsyncioSelectorReactorTests.test_delayedCallResetToEarlier.<locals>.on_timer   r^   r!   r_   r   r   )redirect_stderr r`   )rQ   r   r   r   rR   ra   rb   r   io
contextlibrj   StringIOrc   r%   getvaluerd   rT   )	r'   r]   re   rf   rl   rj   stderrr   r\   s	          @@r   test_delayedCallResetToEarlierz:AsyncioSelectorReactorTests.test_delayedCallResetToEarlier   s    - 	D!"@"B"BCCC(**&	3 	3 	3 	3 	3 	3 __&&
sH--
!W\***			......_V$$ 	 	KKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	**B///_Q/000*Z7===, 	(!$'''''	( 	(s   ?C  C$'C$c                 |   t           rt          t                                 t          j                    }t          j                     	 t          t          j                              }d}t                      }t          |          D ]}|
                    dd            |                                 t          t          j                              }|                     ||z
  |z  d           |rt          j                     n# |rt          j                     w w xY wt           rt          d           dS dS )zW
        L{AsyncioSelectorReactor.callLater()} doesn't leave cyclic references
        i  r   c                      d S r   rG   rG   r!   r   rH   zOAsyncioSelectorReactorTests.test_noCycleReferencesInCallLater.<locals>.<lambda>  s    T r!   r   N)rQ   r   r   gc	isenableddisablelenget_objectsr   rangera   runUntilCurrentrT   enable)r'   gc_was_enabledobjects_beforetimer_countr   _objects_afters          r   !test_noCycleReferencesInCallLaterz=AsyncioSelectorReactorTests.test_noCycleReferencesInCallLater  s3    - 	D!"@"B"BCCC

	 !1!122NK,..G;'' 3 3!!!\\2222##%%% 0 011MOO]^;{JANNN 	  	, 	(!$'''''	( 	(s   
B)D	 	D!N) __name__
__module____qualname____doc__r(   r   r   r3   r   _defaultEventLoopIsSelectorformattype_defaultEventLoopsysversion_infomajorminorr   getTyper6   r:   r?   !hasWindowsProactorEventLoopPolicyrA   rQ   rC   rJ   rM   rU   rg   rq   r   rG   r!   r   r   r   0   s        ) ) )&5 :K    " V''	%vD"##""H	 
  
	 	: :	 	: V''	%vD"##""H	 
  
	 		: 	:	 		: V#	%vD"##""H	 
  
	 	
= 
=	 	
= V--/W I I I V--2 : :	 : V--8 % %	 % V--8 : :	 :, , ,A A A0( ( (>( ( ( ( (r!   r   )#r   rt   r   asyncior   r   r   r   r   r   r	   r
   r   unittestr   twisted.internet.asyncioreactorr   twisted.python.runtimer   twisted.trial.unittestr   reactormixinsr   r   rQ   
startswithr   r   ImportErrorr1   r   
isinstancer   r-   r   rG   r!   r   <module>r      s    
			 




 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
       B B B B B B + + + + + + 6 6 6 6 6 6 ) ) ) ) ) )$) !$) !
	
|w'' 1	
 	
 	
 	
 	
 	
 	
 	

 -1),0) 	 	 	D	 +*,,;;== (j):<MNN       x( x( x( x( x(.2E x( x( x( x( x(s   &A+ +A32A3