o
    Òó·ih2  ã                   @   s  d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddl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mZmZmZmZmZmZ de
de	f de
de	f fdd„Z G dd„ dej!j"j#ƒZ$G dd„ dej!j"j#ƒZ%dS )z3
Tests for L{twisted.application.runner._pidfile}.
é    N)Úwraps)ÚgetpidÚname)ÚAnyÚCallableÚOptional)ÚverifyObject)ÚNoReturn)ÚFilePath)Úplatform)ÚSkipTesté   )Ú_pidfileé   )ÚAlreadyRunningErrorÚInvalidPIDFileErrorÚIPIDFileÚNonePIDFileÚ
NoPIDFoundÚPIDFileÚStalePIDFileErrorÚf.Úreturnc              	      s*   t ˆ ƒdtdtdtdtf‡ fdd„ƒ}|S )a=  
    Decorator for tests that are not expected to work on all platforms.

    Calling L{PIDFile.isRunning} currently raises L{NotImplementedError} on
    non-POSIX platforms.

    On an unsupported platform, we expect to see any test that calls
    L{PIDFile.isRunning} to raise either L{NotImplementedError}, L{SkipTest},
    or C{self.failureException}.
    (C{self.failureException} may occur in a test that checks for a specific
    exception but it gets NotImplementedError instead.)

    @param f: The test method to decorate.

    @return: The wrapped callable.
    ÚselfÚargsÚkwargsr   c                    sp   t  ¡ dk}|rˆ | g|¢R i |¤ŽS | jtt| jfˆ | g|¢R i |¤Ž}t|tƒr6|  t|ƒ 	d¡¡ d S d S )NÚposixz isRunning is not implemented on )
r   ÚgetTypeÚassertRaisesÚNotImplementedErrorr   ZfailureExceptionÚ
isinstanceÚ
assertTrueÚstrÚ
startswith)r   r   r   Ú	supportedÚe©r   © úrC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/application/runner/test/test_pidfile.pyÚwrapper3   s   
ýüû
ÿz$ifPlatformSupported.<locals>.wrapperN)r   r   )r   r)   r'   r&   r(   ÚifPlatformSupported!   s    r*   c                   @   s  e Zd ZdZd1dee dee fdd„Zd2dd„Z	d2d	d
„Z
d2dd„Zd2dd„Zd2dd„Zd2dd„Zd2dd„Zd2dd„Zd2dd„Zd2dd„Zd2dd„Zed2dd„ƒZed2dd „ƒZed2d!d"„ƒZed2d#d$„ƒZed2d%d&„ƒZed2d'd(„ƒZd2d)d*„Zd2d+d,„Zed2d-d.„ƒZed2d/d0„ƒZdS )3ÚPIDFileTestsz
    Tests for L{PIDFile}.
    NÚcontentr   c                 C   s"   t |  ¡ ƒ}|d ur| |¡ |S ©N)r
   ÚmktempZ
setContent)r   r,   ÚfilePathr'   r'   r(   r/   L   s   
zPIDFileTests.filePathc                 C   s   t |  ¡ ƒ}tt|ƒ dS )z5
        L{PIDFile} conforms to L{IPIDFile}.
        N)r   r/   r   r   ©r   ÚpidFiler'   r'   r(   Útest_interfaceR   s   zPIDFileTests.test_interfacec                 C   s   |   tjdd�d¡ dS )zR
        L{PIDFile._format} returns the expected format when given a PID.
        é9  ©Úpids   1337
N)ÚassertEqualr   Ú_format)r   r'   r'   r(   Útest_formatWithPIDY   s   zPIDFileTests.test_formatWithPIDc                 C   s.   d}t |  t j|d�¡ƒ}|  || ¡ ¡ dS )zK
        L{PIDFile.read} returns the PID from the given file path.
        r3   r4   N)r   r/   r7   r6   Úread©r   r5   r1   r'   r'   r(   Útest_readWithPID_   s   zPIDFileTests.test_readWithPIDc                 C   s:   d}t |  d¡ƒ}|  t|j¡}|  t|ƒd|›�¡ dS )úf
        L{PIDFile.read} raises L{InvalidPIDFileError} when given an empty file
        path.
        ó    ú#non-integer PID value in PID file: N©r   r/   r   r   r9   r6   r"   ©r   ZpidValuer1   r%   r'   r'   r(   Útest_readEmptyPIDi   ó   zPIDFileTests.test_readEmptyPIDc                 C   s:   d}t |  |¡ƒ}|  t|j¡}|  t|ƒd|›�¡ dS )r<   s   $foo!r>   Nr?   r@   r'   r'   r(   Útest_readWithBogusPIDt   rB   z"PIDFileTests.test_readWithBogusPIDc                 C   s.   t |  ¡ ƒ}|  t|j¡}|  t|ƒd¡ dS )zc
        L{PIDFile.read} raises L{NoPIDFound} when given a non-existing file
        path.
        úPID file does not existN)r   r/   r   r   r9   r6   r"   ©r   r1   r%   r'   r'   r(   Útest_readDoesntExist   s   z!PIDFileTests.test_readDoesntExistc                 C   sP   ddt dtfdd„}|  td|¡ t|  ¡ ƒ}|  t|j¡}|  	|j
t
j¡ dS )	z�
        L{PIDFile.read} re-raises L{OSError} if the associated C{errno} is
        anything other than L{errno.ENOENT}.
        ÚrÚmoder   c                 S   ó   t tjdƒ‚)Nz	I/O error)ÚOSErrorÚerrnoÚEIO)rH   r'   r'   r(   Úoops�   ó   z>PIDFileTests.test_readOpenRaisesOSErrorNotENOENT.<locals>.oopsÚopenN)rG   )r"   r	   Úpatchr
   r   r/   r   rJ   r9   r6   rK   rL   )r   rM   r1   Úerrorr'   r'   r(   Ú#test_readOpenRaisesOSErrorNotENOENT‰   s
   z0PIDFileTests.test_readOpenRaisesOSErrorNotENOENTc                 C   s.   d}t |  ¡ ƒ}| |¡ |  | ¡ |¡ dS )z9
        L{PIDFile._write} stores the given PID.
        iË  N)r   r/   Ú_writer6   r9   r:   r'   r'   r(   Útest_writePID™   s   
zPIDFileTests.test_writePIDc                 C   s    t |  ¡ ƒ}|  t|jd¡ dS )zS
        L{PIDFile._write} raises L{ValueError} when given an invalid PID.
        ZburpN)r   r/   r   Ú
ValueErrorrS   r0   r'   r'   r(   Útest_writePIDInvalid¤   s   z!PIDFileTests.test_writePIDInvalidc                 C   s*   t |  ¡ ƒ}| ¡  |  | ¡ tƒ ¡ dS )zT
        L{PIDFile.writeRunningPID} stores the PID for the current process.
        N)r   r/   ÚwriteRunningPIDr6   r9   r   r0   r'   r'   r(   Útest_writeRunningPID¬   s   z!PIDFileTests.test_writeRunningPIDc                 C   s:   t |  d¡ƒ}|  |j ¡ ¡ | ¡  |  |j ¡ ¡ dS )z9
        L{PIDFile.remove} removes the PID file.
        r=   N)r   r/   r!   ÚexistsÚremoveÚassertFalser0   r'   r'   r(   Útest_removeµ   s   zPIDFileTests.test_removec                 C   óL   t |  ¡ ƒ}| d¡ dtdtddfdd„}|  td|¡ |  | ¡ ¡ dS )	zR
        L{PIDFile.isRunning} returns true for a process that does exist.
        r3   r5   Úsignalr   Nc                 S   ó   d S r-   r'   ©r5   r^   r'   r'   r(   ÚkillÇ   ó   z2PIDFileTests.test_isRunningDoesExist.<locals>.killra   ©r   r/   rS   ÚintrP   r   r!   Ú	isRunning©r   r1   ra   r'   r'   r(   Útest_isRunningDoesExist¿   s
   
z$PIDFileTests.test_isRunningDoesExistc                 C   s&   t |  ¡ ƒ}| ¡  |  | ¡ ¡ dS )a@  
        L{PIDFile.isRunning} returns true for this process (which is running).

        @note: This differs from L{PIDFileTests.test_isRunningDoesExist} in
        that it actually invokes the C{kill} system call, which is useful for
        testing of our chosen method for probing the existence of a process.
        N)r   r/   rW   r!   re   r0   r'   r'   r(   Útest_isRunningThisÎ   s   	zPIDFileTests.test_isRunningThisc                 C   sL   t |  ¡ ƒ}| d¡ dtdtddfdd„}|  td|¡ |  t|j¡ dS )	z{
        L{PIDFile.isRunning} raises L{StalePIDFileError} for a process that
        does not exist (errno=ESRCH).
        r3   r5   r^   r   Nc                 S   rI   ©NzNo such process©rJ   rK   ZESRCHr`   r'   r'   r(   ra   å   rN   z5PIDFileTests.test_isRunningDoesNotExist.<locals>.killra   )	r   r/   rS   rd   rP   r   r   r   re   rf   r'   r'   r(   Útest_isRunningDoesNotExistÜ   ó
   
z'PIDFileTests.test_isRunningDoesNotExistc                 C   r]   )	zx
        L{PIDFile.isRunning} returns true for a process that we are not allowed
        to kill (errno=EPERM).
        r3   r5   r^   r   Nc                 S   rI   )NzOperation not permitted)rJ   rK   ÚEPERMr`   r'   r'   r(   ra   õ   rN   z3PIDFileTests.test_isRunningNotAllowed.<locals>.killra   rc   rf   r'   r'   r(   Útest_isRunningNotAllowedì   rl   z%PIDFileTests.test_isRunningNotAllowedc                 C   s8   t dkrtdƒ‚t|  ¡ ƒ}| d¡ |  | ¡ ¡ dS )ac  
        L{PIDFile.isRunning} returns true for a process that we are not allowed
        to kill (errno=EPERM).

        @note: This differs from L{PIDFileTests.test_isRunningNotAllowed} in
        that it actually invokes the C{kill} system call, which is useful for
        testing of our chosen method for probing the existence of a process
        that we are not allowed to kill.

        @note: In this case, we try killing C{init}, which is process #1 on
        POSIX systems, so this test is not portable.  C{init} should always be
        running and should not be killable by non-root users.
        r   zThis test assumes POSIXé   N)ÚSYSTEM_NAMEr   r   r/   rS   r!   re   r0   r'   r'   r(   Útest_isRunningInitü   s
   
zPIDFileTests.test_isRunningInitc                 C   sJ   t |  ¡ ƒ}| ¡  dtdtddfdd„}|  td|¡ |  t|j¡ dS )zŠ
        L{PIDFile.isRunning} re-raises L{OSError} if the attached C{errno}
        value from L{os.kill} is not an expected one.
        r5   r^   r   Nc                 S   rI   )NzFile exists)rJ   rK   ÚEEXISTr`   r'   r'   r(   ra     rN   z5PIDFileTests.test_isRunningUnknownErrno.<locals>.killra   )	r   r/   rW   rd   rP   r   r   rJ   re   rf   r'   r'   r(   Útest_isRunningUnknownErrno  s
   z'PIDFileTests.test_isRunningUnknownErrnoc                 C   s   t |  ¡ ƒ}|  | ¡ ¡ dS )zS
        L{PIDFile.isRunning} returns false if the PID file doesn't exist.
        N)r   r/   r[   re   r0   r'   r'   r(   Útest_isRunningNoPIDFile#  s   z$PIDFileTests.test_isRunningNoPIDFilec                 C   sv   t |  ¡ ƒ}|  |j ¡ ¡ |� |  |j ¡ ¡ |  | ¡ tƒ ¡ W d  ƒ n1 s,w   Y  |  |j ¡ ¡ dS )zŽ
        When used as a context manager, a L{PIDFile} will store the current pid
        on entry, then removes the PID file on exit.
        N)r   r/   r[   rY   r!   r6   r9   r   r0   r'   r'   r(   Útest_contextManager+  s   þz PIDFileTests.test_contextManagerc                 C   s”   t |  ¡ ƒ}| d¡ dtdtddfdd„}|  td|¡ |  t|j¡}|  	t
|ƒd	¡ |� |  	| ¡ tƒ ¡ W d  ƒ dS 1 sCw   Y  dS )
zß
        When used as a context manager, a L{PIDFile} will replace the
        underlying PIDFile rather than raising L{AlreadyRunningError} if the
        contained PID file exists but refers to a non-running PID.
        r3   r5   r^   r   Nc                 S   rI   ri   rj   r`   r'   r'   r(   ra   C  rN   z9PIDFileTests.test_contextManagerDoesntExist.<locals>.killra   z'PID file refers to non-existing process)r   r/   rS   rd   rP   r   r   r   re   r6   r"   r9   r   )r   r1   ra   r%   r'   r'   r(   Útest_contextManagerDoesntExist9  s   
"ÿz+PIDFileTests.test_contextManagerDoesntExistc                 C   sZ   t |  ¡ ƒ}| d¡ dtdtddfdd„}|  td|¡ |  | ¡ ¡ |  t	|j
¡ dS )	z²
        When used as a context manager, a L{PIDFile} will raise
        L{AlreadyRunningError} if the there is already a running process with
        the contained PID.
        r3   r5   r^   r   Nc                 S   r_   r-   r'   r`   r'   r'   r(   ra   X  rb   z<PIDFileTests.test_contextManagerAlreadyRunning.<locals>.killra   )r   r/   rS   rd   rP   r   r!   re   r   r   Ú	__enter__rf   r'   r'   r(   Ú!test_contextManagerAlreadyRunningN  s   
z.PIDFileTests.test_contextManagerAlreadyRunningr-   ©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úbytesr
   r"   r/   r2   r8   r;   rA   rC   rF   rR   rT   rV   rX   r\   r*   rg   rh   rk   rn   rq   rs   rt   ru   rv   rx   r'   r'   r'   r(   r+   G   s@    












	


r+   c                   @   sV   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S )ÚNonePIDFileTestsz#
    Tests for L{NonePIDFile}.
    r   Nc                 C   s   t ƒ }tt|ƒ dS )z9
        L{NonePIDFile} conforms to L{IPIDFile}.
        N)r   r   r   r0   r'   r'   r(   r2   g  s   zNonePIDFileTests.test_interfacec                 C   s(   t ƒ }|  t|j¡}|  t|ƒd¡ dS )z;
        L{NonePIDFile.read} raises L{NoPIDFound}.
        rD   N)r   r   r   r9   r6   r"   rE   r'   r'   r(   Ú	test_readn  ó   zNonePIDFileTests.test_readc                 C   s*   t ƒ }|  t|jd¡}|  |jtj¡ dS )zZ
        L{NonePIDFile._write} raises L{OSError} with an errno of L{errno.EPERM}.
        r   N)r   r   rJ   rS   r6   rK   rm   ©r   r1   rQ   r'   r'   r(   Ú
test_writew  s   zNonePIDFileTests.test_writec                 C   ó(   t ƒ }|  t|j¡}|  |jtj¡ dS )zk
        L{NonePIDFile.writeRunningPID} raises L{OSError} with an errno of
        L{errno.EPERM}.
        N)r   r   rJ   rW   r6   rK   rm   r‚   r'   r'   r(   rX   €  s   z%NonePIDFileTests.test_writeRunningPIDc                 C   r„   )zZ
        L{NonePIDFile.remove} raises L{OSError} with an errno of L{errno.EPERM}.
        N)r   r   rJ   rZ   r6   rK   ÚENOENTr‚   r'   r'   r(   r\   Š  r�   zNonePIDFileTests.test_removec                 C   s   t ƒ }|  | ¡ d¡ dS )z<
        L{NonePIDFile.isRunning} returns L{False}.
        FN)r   r6   re   r0   r'   r'   r(   Útest_isRunning“  s   zNonePIDFileTests.test_isRunningc                 C   s0   t ƒ }|�	 W d  ƒ dS 1 sw   Y  dS )zo
        When used as a context manager, a L{NonePIDFile} doesn't raise, despite
        not existing.
        N)r   r0   r'   r'   r(   ru   ›  s   "ÿz$NonePIDFileTests.test_contextManagerry   )rz   r{   r|   r}   r2   r€   rƒ   rX   r\   r†   ru   r'   r'   r'   r(   r   b  s    


	
	


	r   )&r}   rK   Ú	functoolsr   Úosr   r   rp   Útypingr   r   r   Zzope.interface.verifyr   Ztyping_extensionsr	   Ztwisted.trial.unittestZtwistedZtwisted.python.filepathr
   Ztwisted.python.runtimer   r   Úrunnerr   r   r   r   r   r   r   r   r*   ZtrialZunittestZTestCaser+   r   r'   r'   r'   r(   Ú<module>   s$   $"&  