HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/internet/test/__pycache__/test_posixprocess.cpython-38.pyc
U


W[�+�@s�dZddlmZmZddlZddlZddlZzddlZWnek
rPdZ	YnXddl
mZdZ	ddlm
Z
ddlmZGdd	�d	e�ZGd
d�de�ZGdd
�d
e�ZGdd�de�ZdS)z;
Tests for POSIX-based L{IReactorProcess} implementations.
�)�division�absolute_importNznon-POSIX platform)�process)�range)�TestCasec@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�FakeFilez>
    A dummy file object which records when it is closed.
    cCs||_||_dS�N)�testcase�fd)�selfr	r
�r�I/usr/lib/python3/dist-packages/twisted/internet/test/test_posixprocess.py�__init__szFakeFile.__init__cCs|jj�|j�dSr)r	�_files�remover
�rrrr
�close!szFakeFile.closecCs|Srrrrrr
�	__enter__%szFakeFile.__enter__cCs|��dSr)r)r�exc_type�	exc_value�	tracebackrrr
�__exit__)szFakeFile.__exit__N)�__name__�
__module__�__qualname__�__doc__rrrrrrrr
rs
rc@s$eZdZdZdZdd�Zdd�ZdS)�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.
    �cCs
||_dSr)�_limit�r�limitrrr
r7szFakeResourceModule.__init__cCs||jkrd|jgSddgS)zX
        A fake of L{resource.getrlimit} which returns a pre-determined result.
        r�{i�)�
RLIMIT_NOFILEr)rZnorrr
�	getrlimit;s

zFakeResourceModule.getrlimitN)rrrrr"rr#rrrr
r.src@s�eZdZdZeZdZdZdZdd�Z	dd�Z
dd�Zd	d
�Zdd�Z
d
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS) �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.
    FcCsdS)z>
        Fake os.getpid, always return the same thing
        r!rrrrr
�getpidZszFDDetectorTests.getpidcCsTtt|j�}|jr(|d|��fkr(|S|jrJ|dkrJ|jr@|SdddgSt��dS)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�2N)�map�strr�procfsr%�devfs�accurateDevFDResults�OSError)r�argZaccuraterrr
�listdiras
zFDDetectorTests.listdircCs2t|tttd��t|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�setrr�appendr
)r�fname�mode�frrr
�openfileqs
 zFDDetectorTests.openfilecCsdtjd<dS)zp
        Make the L{resource} module unimportable for the remainder of the
        current test method.
        N�resource)�sys�modulesrrrr
�hideResourceModule�sz"FDDetectorTests.hideResourceModulecCst|�tjd<dS)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.
        r9N)rr:r;rrrr
�revealResourceModule�s	z$FDDetectorTests.revealResourceModulecCs:|dkr,ztjd=Wq6tk
r(Yq6Xn
|tjd<dS)zI
        Restore the original resource module to L{sys.modules}.
        Nr9)r:r;�KeyError)r�valuerrr
�replaceResourceModule�sz%FDDetectorTests.replaceResourceModulecCsNt��|_|j|j_|j|j_|j|j_dddg|_|�|jt	j
�d��dS)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.
        rr�r9N)r�_FDDetector�detectorr0r%r8rZ
addCleanupr@r:r;�getrrrr
�setUp�s




�zFDDetectorTests.setUpcs@dd�}dd�}�fdd�}|||g�j_��|�j���dS)z�
        L{FDDetector._getImplementation} returns the first method from its
        C{_implementations} list which returns results which reflect a newly
        opened file descriptor.
        cSstd��dS)NzThis does not work)�
ValueErrorrrrr
�failWithException�szBFDDetectorTests.test_selectFirstWorking.<locals>.failWithExceptioncSs
dddgS�NrrrArrrrr
�failWithWrongResults�szEFDDetectorTests.test_selectFirstWorking.<locals>.failWithWrongResultscs�jdd�Sr)rrrrr
�correct�sz8FDDetectorTests.test_selectFirstWorking.<locals>.correctN�rCZ_implementationsZassertIsZ_getImplementation)rrGrIrJrrr
�test_selectFirstWorking�s�z'FDDetectorTests.test_selectFirstWorkingcCs2dd�}dd�}||g|j_|�||j���dS)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.
        cSs
dddgS)N���	rrrrr
rI�sz=FDDetectorTests.test_selectLast.<locals>.failWithWrongResultscSs
dddgSrHrrrrr
�failWithOtherWrongResults�szBFDDetectorTests.test_selectLast.<locals>.failWithOtherWrongResultsNrK)rrIrPrrr
�test_selectLast�s��zFDDetectorTests.test_selectLastcCsLt��}|jj}|��|jj}|��|jj}|�||�|�||�dS)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)rrB�_listOpenFDsrZassertNotEqual�assertEqual)rrC�first�secondZthirdrrr
�!test_identityOfListOpenFDsChanges�sz1FDDetectorTests.test_identityOfListOpenFDsChangescCs>d|_|�t|jj�d|_d|_|�dddg|j���dS)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.
        FTrrrAN)r,�assertRaisesr.rCZ_devFDImplementationr-rSrrrr
�test_devFDImplementation�s
z(FDDetectorTests.test_devFDImplementationcCs8d|_|�t|jj�d|_|�dddg|j���dS)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.
        FTrrrAN)r+rWr.rCZ_procFDImplementationrSrrrr
�test_procFDImplementation�sz)FDDetectorTests.test_procFDImplementationcCsT|�d�|�ttd��t|j����|�d�|�ttd��t|j����dS)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.
        iir1N)r=rS�listrrC�_fallbackFDImplementationrrrr
�test_resourceFDImplementations

�

�z-FDDetectorTests.test_resourceFDImplementationcCs*|��|�ttd��t|j����dS)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.
        r1N)r<rSrZrrCr[rrrr
�test_fallbackFDImplementations�z-FDDetectorTests.test_fallbackFDImplementationN)rrrr�platformSkip�skipr,r-r+r%r0r8r<r=r@rErLrQrVrXrYr\r]rrrr
r$Es&


r$c@s$eZdZdZeZdd�Zdd�ZdS)�FileDescriptorTestsz<
    Tests for L{twisted.internet.process._listOpenFDs}
    cCsdt��D]V}zt�|tj�Wqtk
r\}z |�tj|jd||jf�W5d}~XYqXqdS)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)rrR�fcntlZF_GETFL�IOErrorrS�errnoZEBADF)rr
�errrrr
�test_openFDs's��z FileDescriptorTests.test_openFDsc	Cs�ttj�}t��}|�|��|�t�|���}|�||��kd�z|�	�|�|t���W5t�	|�X|�
|t���dS)zB
        L{_listOpenFDs} lists expected file descriptors.
        z>Expected duplicate file descriptor to be greater than originalN)�open�os�devnullrrRZassertIn�fileno�dupZ
assertTruerZassertNotIn)rr7Zopenfdsr
rrr
�test_expectedFDs8s

�z$FileDescriptorTests.test_expectedFDsN)rrrrr^r_rerkrrrr
r`!sr`)rZ
__future__rrrcrgr:ra�ImportErrorr^Ztwisted.internetrZtwisted.python.compatrZtwisted.trial.unittestr�objectrrr$r`rrrr
�<module>s
]