o
    ¼>hh2  ã                   @   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   ÚfailureExceptionÚ
isinstanceÚ
assertTrueÚstrÚ
startswith)r   r   r   Ú	supportedÚe©r   © úŒ/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/application/runner/test/test_pidfile.pyÚwrapper3   s   
ýüû
ÿz$ifPlatformSupported.<locals>.wrapper)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
   ÚmktempÚ
setContent)r   r-   ÚfilePathr(   r(   r)   r1   L   s   
zPIDFileTests.filePathc                 C   s   t |  ¡ ƒ}tt|ƒ dS )z5
        L{PIDFile} conforms to L{IPIDFile}.
        N)r   r1   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.
        r5   r6   N)r   r1   r9   r8   Úread©r   r7   r3   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   r1   r   r   r;   r8   r#   ©r   ÚpidValuer3   r&   r(   r(   r)   Útest_readEmptyPIDi   ó   zPIDFileTests.test_readEmptyPIDc                 C   s:   d}t |  |¡ƒ}|  t|j¡}|  t|ƒd|›¡ dS )r>   s   $foo!r@   NrA   rB   r(   r(   r)   Útest_readWithBogusPIDt   rE   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   r1   r   r   r;   r8   r#   ©r   r3   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)rK   r(   r(   r)   Úoops   ó   z>PIDFileTests.test_readOpenRaisesOSErrorNotENOENT.<locals>.oopsÚopenN)rJ   )r#   r	   Úpatchr
   r   r1   r   rM   r;   r8   rN   rO   )r   rP   r3   Ú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   r1   Ú_writer8   r;   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.
        ÚburpN)r   r1   r   Ú
ValueErrorrV   r2   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   r1   ÚwriteRunningPIDr8   r;   r   r2   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   r1   r"   ÚexistsÚremoveÚassertFalser2   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.
        r5   r7   Úsignalr   Nc                 S   ó   d S r.   r(   ©r7   rb   r(   r(   r)   ÚkillÇ   ó   z2PIDFileTests.test_isRunningDoesExist.<locals>.killre   ©r   r1   rV   ÚintrS   r   r"   Ú	isRunning©r   r3   re   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   r1   r[   r"   ri   r2   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).
        r5   r7   rb   r   Nc                 S   rL   ©NzNo such process©rM   rN   ÚESRCHrd   r(   r(   r)   re   å   rQ   z5PIDFileTests.test_isRunningDoesNotExist.<locals>.killre   )	r   r1   rV   rh   rS   r   r   r   ri   rj   r(   r(   r)   Útest_isRunningDoesNotExistÜ   ó
   
z'PIDFileTests.test_isRunningDoesNotExistc                 C   ra   )	zx
        L{PIDFile.isRunning} returns true for a process that we are not allowed
        to kill (errno=EPERM).
        r5   r7   rb   r   Nc                 S   rL   )NzOperation not permitted)rM   rN   ÚEPERMrd   r(   r(   r)   re   õ   rQ   z3PIDFileTests.test_isRunningNotAllowed.<locals>.killre   rg   rj   r(   r(   r)   Útest_isRunningNotAllowedì   rq   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   r1   rV   r"   ri   r2   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.
        r7   rb   r   Nc                 S   rL   )NzFile exists)rM   rN   ÚEEXISTrd   r(   r(   r)   re     rQ   z5PIDFileTests.test_isRunningUnknownErrno.<locals>.killre   )	r   r1   r[   rh   rS   r   r   rM   ri   rj   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   r1   r_   ri   r2   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   r1   r_   r]   r"   r8   r;   r   r2   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.
        r5   r7   rb   r   Nc                 S   rL   rm   rn   rd   r(   r(   r)   re   C  rQ   z9PIDFileTests.test_contextManagerDoesntExist.<locals>.killre   z'PID file refers to non-existing process)r   r1   rV   rh   rS   r   r   r   ri   r8   r#   r;   r   )r   r3   re   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.
        r5   r7   rb   r   Nc                 S   rc   r.   r(   rd   r(   r(   r)   re   X  rf   z<PIDFileTests.test_contextManagerAlreadyRunning.<locals>.killre   )r   r1   rV   rh   rS   r   r"   ri   r   r   Ú	__enter__rj   r(   r(   r)   Ú!test_contextManagerAlreadyRunningN  s   
z.PIDFileTests.test_contextManagerAlreadyRunningr.   ©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úbytesr
   r#   r1   r4   r:   r=   rD   rF   rI   rU   rW   rZ   r\   r`   r+   rk   rl   rp   rs   rv   rx   ry   rz   r{   r}   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   r2   r(   r(   r)   r4   g  s   zNonePIDFileTests.test_interfacec                 C   s(   t ƒ }|  t|j¡}|  t|ƒd¡ dS )z;
        L{NonePIDFile.read} raises L{NoPIDFound}.
        rG   N)r   r   r   r;   r8   r#   rH   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   rM   rV   r8   rN   rr   ©r   r3   rT   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   rM   r[   r8   rN   rr   r‡   r(   r(   r)   r\   €  s   z%NonePIDFileTests.test_writeRunningPIDc                 C   r‰   )zZ
        L{NonePIDFile.remove} raises L{OSError} with an errno of L{errno.EPERM}.
        N)r   r   rM   r^   r8   rN   Ú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   r8   ri   r2   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   r2   r(   r(   r)   rz   ›  s   "ÿz$NonePIDFileTests.test_contextManagerr~   )r   r€   r   r‚   r4   r…   rˆ   r\   r`   r‹   rz   r(   r(   r(   r)   r„   b  s    


	
	


	r„   )&r‚   rN   Ú	functoolsr   Úosr   r   ru   Útypingr   r   r   Úzope.interface.verifyr   Útyping_extensionsr	   Útwisted.trial.unittestÚtwistedÚtwisted.python.filepathr
   Útwisted.python.runtimer   r   Úrunnerr   r   r   r   r   r   r   r   r+   ÚtrialÚunittestÚTestCaser,   r„   r(   r(   r(   r)   Ú<module>   s$   $"&  