
    Vh+                         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      W/home/dcms/DCMS/lib/python3.12/site-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    ##DGG,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{{##Sz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^     sDKK(;;3=DKKM3C#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     T3s5;/#dkk2BBCD144 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     #5U";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>     =KK
+ ',CKK
#  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2CKKOOJ4O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		"  *
& 	gt}}??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@    		 !%*
&
 	/1Q1Q1S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}     &&(%%..&&//%%..E6*'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
$)!DMM$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DMM$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d4==#J#J#LM	

 	!!$'ttDMM$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tDMM$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.		    KKIIH399o& 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     &&(ahhj'* VVAHHJ 	OL	

	 GGIMM"g2245 HHRLW1134 HHRLs   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   
  	 
 sm  )L + & ,T
h T
nB5( B5U  ('L(s   A A&%A&