Ë
    ojÃ+  ã                   óÒ   — U d Z ddlZddlZddlZddlmZ ee   ed<   	 ddlZddl	m
Z
 dZddlmZ  G d„ d	«      Z G d
„ d«      Z G d„ de«      Z G d„ de«      Zy# e$ r dZY Œ:w xY w)z;
Tests for POSIX-based L{IReactorProcess} implementations.
é    N)ÚOptionalÚplatformSkip)Úprocessznon-POSIX platform)ÚTestCasec                   ó(   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zy)ÚFakeFilez>
    A dummy file object which records when it is closed.
    c                 ó    — || _         || _        y ©N)ÚtestcaseÚfd)Úselfr   r   s      úI/usr/lib/python3/dist-packages/twisted/internet/test/test_posixprocess.pyÚ__init__zFakeFile.__init__    s   € Ø ˆŒØˆ�ó    c                 ób   — | j                   j                  j                  | j                  «       y r
   )r   Ú_filesÚremover   ©r   s    r   ÚclosezFakeFile.close$   s   € Ø�‰×Ñ×#Ñ# D§G¡GÕ,r   c                 ó   — | S r
   © r   s    r   Ú	__enter__zFakeFile.__enter__'   s   € Øˆr   c                 ó$   — | j                  «        y r
   )r   )r   Úexc_typeÚ	exc_valueÚ	tracebacks       r   Ú__exit__zFakeFile.__exit__*   s   € Ø�
‰
�r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s   „ ñòò-òór   r   c                   ó    — e Zd ZdZdZd„ Zd„ Zy)ÚFakeResourceModulez¹
    Fake version of L{resource} which hard-codes a particular rlimit for maximum
    open files.

    @ivar _limit: The value to return for the hard limit of number of open files.
    é   c                 ó   — || _         y r
   )Ú_limit©r   Úlimits     r   r   zFakeResourceModule.__init__8   s	   € Øˆ�r   c                 óD   — || j                   k(  rd| j                  gS ddgS )zX
        A fake of L{resource.getrlimit} which returns a pre-determined result.
        r   é{   iÈ  )ÚRLIMIT_NOFILEr&   )r   Únos     r   Ú	getrlimitzFakeResourceModule.getrlimit;   s+   € ð �×#Ñ#Ò#Ø�t—{‘{Ð#Ð#Ø�SˆzÐr   N)r   r   r    r!   r+   r   r-   r   r   r   r#   r#   .   s   „ ñð €Mòór   r#   c                   ót   — e Zd ZdZeZdZdZdZd„ Z	d„ Z
d„ Zd„ Zd„ Zd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚFDDetectorTestsaë  
    Tests for _FDDetector class in twisted.internet.process, which detects
    which function to drop in place for the _listOpenFDs method.

    @ivar devfs: A flag indicating whether the filesystem fake will indicate
        that /dev/fd exists.

    @ivar accurateDevFDResults: A flag indicating whether the /dev/fd fake
        returns accurate open file information.

    @ivar procfs: A flag indicating whether the filesystem fake will indicate
        that /proc/<pid>/fd exists.
    Fc                  ó   — y)z>
        Fake os.getpid, always return the same thing
        r*   r   r   s    r   ÚgetpidzFDDetectorTests.getpidZ   s   € ð r   c                 óÚ   — t        t        | j                  «      }| j                  r|d| j	                  «       fz  k(  r|S | j
                  r|dk(  r| j                  r|S g d¢S t        «       ‚)z„
        Fake os.listdir, depending on what mode we're in to simulate behaviour.

        @param arg: the directory to list
        z/proc/%d/fdz/dev/fd)Ú0Ú1Ú2)ÚmapÚstrr   Úprocfsr1   ÚdevfsÚaccurateDevFDResultsÚOSError)r   ÚargÚaccurates      r   ÚlistdirzFDDetectorTests.listdir`   s^   € ô ”s˜DŸK™KÓ(ˆØ�;Š;˜3 =°D·K±K³MÐ3CÑ#CÒDØˆOØ�:Š:˜# Ò*Ø×(Ò(Ø�Ú"Ð"Ü‹iˆr   c           
      óÊ   — t        | t        t        t        d«      «      t        | j                  «      z
  «      «      }| j                  j                  |j                  «       |S )aO  
        This is a mock for L{open}.  It keeps track of opened files so extra
        descriptors can be returned from the mock for L{os.listdir} when used on
        one of the list-of-filedescriptors directories.

        A L{FakeFile} is returned which can be closed to remove the new
        descriptor from the open list.
        é   )r   ÚminÚsetÚranger   Úappendr   )r   ÚfnameÚmodeÚfs       r   ÚopenfilezFDDetectorTests.openfileo   sG   € ô �Tœ3œs¤5¨£;Ó/´#°d·k±kÓ2BÑBÓCÓDˆØ�‰×Ñ˜1Ÿ4™4Ô Øˆr   c                 ó*   — dt         j                  d<   y)zp
        Make the L{resource} module unimportable for the remainder of the
        current test method.
        NÚresource)ÚsysÚmodulesr   s    r   ÚhideResourceModulez"FDDetectorTests.hideResourceModule}   s   € ð
 #'Œ�‰�JÒr   c                 ó<   — t        |«      t        j                  d<   y)a  
        Make a L{FakeResourceModule} instance importable at the L{resource}
        name.

        @param limit: The value which will be returned for the hard limit of
            number of open files by the fake resource module's C{getrlimit}
            function.
        rJ   N)r#   rK   rL   r'   s     r   ÚrevealResourceModulez$FDDetectorTests.revealResourceModule„   s   € ô #5°UÓ";Œ�‰�JÒr   c                 ór   — |€	 t         j                  d= y|t         j                  d<   y# t        $ r Y yw xY w)zI
        Restore the original resource module to L{sys.modules}.
        NrJ   )rK   rL   ÚKeyError)r   Úvalues     r   ÚreplaceResourceModulez%FDDetectorTests.replaceResourceModule�   s>   € ð ˆ=ðÜ—K‘K 
Ñ+ð ',ŒC�K‰K˜
Ò#øô ò Ùðús   „* ª	6µ6c                 ó\  — t        j                  «       | _        | j                  | j                  _        | j                  | j                  _        | j
                  | j                  _        g d¢| _        | j                  | j                  t        j                  j                  d«      «       y)zž
        Set up the tests, giving ourselves a detector object to play with and
        setting up its testable knobs to refer to our mocked versions.
        ©r   r$   é   rJ   N)r   Ú_FDDetectorÚdetectorr>   r1   rH   r   Ú
addCleanuprS   rK   rL   Úgetr   s    r   ÚsetUpzFDDetectorTests.setUp›   sl   € ô
  ×+Ñ+Ó-ˆŒØ $§¡ˆ�‰ÔØ#Ÿ{™{ˆ�‰ÔØ!%§¡ˆ�‰ÔÚˆŒØ�‰˜×2Ñ2´C·K±K·O±OÀJÓ4OÕPr   c                 ó˜   ‡ — d„ }d„ }ˆ fd„}|||g‰ j                   _        ‰ j                  |‰ j                   j                  «       «       y)zÁ
        L{FDDetector._getImplementation} returns the first method from its
        C{_implementations} list which returns results which reflect a newly
        opened file descriptor.
        c                  ó   — t        d«      ‚)NzThis does not work)Ú
ValueErrorr   r   r   ÚfailWithExceptionzBFDDetectorTests.test_selectFirstWorking.<locals>.failWithException®   s   € ÜÐ1Ó2Ð2r   c                  ó
   — g d¢S ©NrU   r   r   r   r   ÚfailWithWrongResultszEFDDetectorTests.test_selectFirstWorking.<locals>.failWithWrongResults±   ó   € ÚÐr   c                  ó"   •— ‰ j                   d d  S r
   )r   r   s   €r   Úcorrectz8FDDetectorTests.test_selectFirstWorking.<locals>.correct´   s   ø€ Ø—;‘;™q�>Ð!r   N©rX   Ú_implementationsÚassertIsÚ_getImplementation)r   r_   rb   re   s   `   r   Útest_selectFirstWorkingz'FDDetectorTests.test_selectFirstWorking§   sH   ø€ ò	3ò	ô	"ð Ø Øð*
ˆ�‰Ô&ð 	�‰�g˜tŸ}™}×?Ñ?ÓAÕBr   c                 óŠ   — d„ }d„ }||g| j                   _        | j                  || j                   j                  «       «       y)zâ
        L{FDDetector._getImplementation} returns the last method from its
        C{_implementations} list if none of the implementations manage to return
        results which reflect a newly opened file descriptor.
        c                  ó
   — g d¢S )N)é   é   é	   r   r   r   r   rb   z=FDDetectorTests.test_selectLast.<locals>.failWithWrongResultsÆ   rc   r   c                  ó
   — g d¢S ra   r   r   r   r   ÚfailWithOtherWrongResultszBFDDetectorTests.test_selectLast.<locals>.failWithOtherWrongResultsÉ   rc   r   Nrf   )r   rb   rq   s      r   Útest_selectLastzFDDetectorTests.test_selectLast¿   s@   € ò	ò	ð !Ø%ð*
ˆ�‰Ô&ð
 	�‰Ð/°·±×1QÑ1QÓ1SÕTr   c                 ó8  — t        j                  «       }|j                  j                  }|j                  «        |j                  j                  }|j                  «        |j                  j                  }| j	                  ||«       | j                  ||«       y)zä
        Check that the identity of _listOpenFDs changes after running
        _listOpenFDs the first time, but not after the second time it's run.

        In other words, check that the monkey patching actually works.
        N)r   rW   Ú_listOpenFDsr   ÚassertNotEqualÚassertEqual)r   rX   ÚfirstÚsecondÚthirds        r   Ú!test_identityOfListOpenFDsChangesz1FDDetectorTests.test_identityOfListOpenFDsChangesÓ   s}   € ô ×&Ñ&Ó(ˆà×%Ñ%×.Ñ.ˆØ×ÑÔØ×&Ñ&×/Ñ/ˆØ×ÑÔØ×%Ñ%×.Ñ.ˆà×Ñ˜E 6Ô*Ø×Ñ˜ Õ'r   c                 óÚ   — d| _         | j                  t        | j                  j                  «       d| _         d| _        | j                  g d¢| j                  j	                  «       «       y)zÈ
        L{_FDDetector._devFDImplementation} raises L{OSError} if there is no
        I{/dev/fd} directory, otherwise it returns the basenames of its children
        interpreted as integers.
        FTrU   N)r9   ÚassertRaisesr;   rX   Ú_devFDImplementationr:   rv   r   s    r   Útest_devFDImplementationz(FDDetectorTests.test_devFDImplementationæ   sQ   € ð ˆŒ
Ø×Ñœ' 4§=¡=×#EÑ#EÔFØˆŒ
Ø$)ˆÔ!Ø×Ñš D§M¡M×$FÑ$FÓ$HÕIr   c                 óÌ   — d| _         | j                  t        | j                  j                  «       d| _         | j                  g d¢| j                  j	                  «       «       y)zÐ
        L{_FDDetector._procFDImplementation} raises L{OSError} if there is no
        I{/proc/<pid>/fd} directory, otherwise it returns the basenames of its
        children interpreted as integers.
        FTrU   N)r8   r|   r;   rX   Ú_procFDImplementationrv   r   s    r   Útest_procFDImplementationz)FDDetectorTests.test_procFDImplementationò   sI   € ð ˆŒØ×Ñœ' 4§=¡=×#FÑ#FÔGØˆŒØ×Ñš D§M¡M×$GÑ$GÓ$IÕJr   c                 ó\  — | j                  d«       | j                  t        t        d«      «      t        | j                  j                  «       «      «       | j                  d«       | j                  t        t        d«      «      t        | j                  j                  «       «      «       y)z×
        L{_FDDetector._fallbackFDImplementation} uses the L{resource} module if
        it is available, returning a range of integers from 0 to the
        minimum of C{1024} and the hard I{NOFILE} limit.
        i   i   r@   N)rO   rv   ÚlistrC   rX   Ú_fallbackFDImplementationr   s    r   Útest_resourceFDImplementationz-FDDetectorTests.test_resourceFDImplementationý   s~   € ð 	×!Ñ! #Ô&Ø×ÑÜ”�s“Óœd 4§=¡=×#JÑ#JÓ#LÓMô	
ð
 	×!Ñ! $Ô'Ø×ÑÜ”�t“Óœt D§M¡M×$KÑ$KÓ$MÓNõ	
r   c                 ó®   — | j                  «        | j                  t        t        d«      «      t        | j                  j                  «       «      «       y)zÔ
        L{_FDDetector._fallbackFDImplementation}, the implementation of last
        resort, succeeds with a fixed range of integers from 0 to 1024 when the
        L{resource} module is not importable.
        r@   N)rM   rv   rƒ   rC   rX   r„   r   s    r   Útest_fallbackFDImplementationz-FDDetectorTests.test_fallbackFDImplementation  s>   € ð 	×ÑÔ!Ø×ÑÜ”�t“Óœt D§M¡M×$KÑ$KÓ$MÓNõ	
r   N)r   r   r    r!   r   Úskipr9   r:   r8   r1   r>   rH   rM   rO   rS   r[   rj   rr   rz   r~   r�   r…   r‡   r   r   r   r/   r/   D   si   „ ñð €Dà€EØ Ðà€Fòòòò'ò	<ò
,ò
QòCò0Uò((ò&
Jò	Kò
ó$	
r   r/   c                   ó    — e Zd ZdZeZd„ Zd„ Zy)ÚFileDescriptorTestsz<
    Tests for L{twisted.internet.process._listOpenFDs}
    c           	      ó   — t        j                  «       D ]'  }	 t        j                  |t        j                  «       Œ) y# t        $ rD}| j                  t        j                  |j                  d||j                  fz  «       Y d}~Œsd}~ww xY w)zµ
        File descriptors returned by L{_listOpenFDs} are mostly open.

        This test assumes that zero-legth writes fail with EBADF on closed
        file descriptors.
        z2fcntl(%d, F_GETFL) failed with unexpected errno %dN)r   rt   ÚfcntlÚF_GETFLr;   rv   ÚerrnoÚEBADF)r   r   Úerrs      r   Útest_openFDsz FileDescriptorTests.test_openFDs"  sy   € ô ×&Ñ&Ó(ò 		ˆBðÜ—‘˜B¤§¡Õ.ñ		øô ò Ø× Ñ Ü—K‘KØ—I‘IØHØ˜3Ÿ9™9�oñ&÷ñ ûðús   ™$A Á 	BÁ	:BÂBc                 ó:  — t        t        j                  «      }t        j                  «       }| j                  |j                  «       |«       t        j                  |j                  «       «      }| j                  ||j                  «       kD  d«       	 |j                  «        | j                  |t        j                  «       «       t        j                  |«       | j                  |t        j                  «       «       y# t        j                  |«       w xY w)zB
        L{_listOpenFDs} lists expected file descriptors.
        z>Expected duplicate file descriptor to be greater than originalN)ÚopenÚosÚdevnullr   rt   ÚassertInÚfilenoÚdupÚ
assertTruer   ÚassertNotIn)r   rG   Úopenfdsr   s       r   Útest_expectedFDsz$FileDescriptorTests.test_expectedFDs4  sÄ   € ô ”—‘ÓˆÜ×&Ñ&Ó(ˆØ�‰�a—h‘h“j 'Ô*ô �V‰V�A—H‘H“JÓˆð 	�‰Ø�—‘“‰OØLô	
ð
	ð �G‰GŒIØ�M‰M˜"œg×2Ñ2Ó4Ô5ô �H‰H�RŒLà×Ñ˜œW×1Ñ1Ó3Õ4øô �H‰H�R�Lús   Â4D ÄDN)r   r   r    r!   r   rˆ   r‘   rœ   r   r   r   rŠ   rŠ     s   „ ñð €Dòó$)5r   rŠ   )r!   rŽ   r”   rK   Útypingr   r7   Ú__annotations__rŒ   Útwisted.internetr   r   ÚImportErrorÚtwisted.trial.unittestr   r   r#   r/   rŠ   r   r   r   ú<module>r¢      s   ðòó
 Û 	Û 
Ý à�s‰mÓ ðÛõ )à€Lå +÷ñ ÷&ñ ô,T
�hô T
ônB5˜(õ B5øðU ò (Ø'‚Lð(ús   ŸA ÁA&Á%A&