
    Vhh2                     |   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Z  G d dejB                  jD                  jF                        Z$ G d dejB                  jD                  jF                        Z%y)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           	      `     t               dt        dt        dt        dt        f f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                    t        j                         dk(  }|r | g|i |S  | j                  t        t        | j
                  f| g|i |}t        |t              r*| j                  t        |      j                  d             y y )Nposixz isRunning is not implemented on )
r   getTypeassertRaisesNotImplementedErrorr   failureException
isinstance
assertTruestr
startswith)r   r   r   	supporteder   s        \/home/dcms/DCMS/lib/python3.12/site-packages/twisted/application/runner/test/test_pidfile.pywrapperz$ifPlatformSupported.<locals>.wrapper3   s    $$&'1	T+D+F++!!!$h0E0EF 	
 A !01A 1 12T UV 2    )r   r   )r   r+   s   ` r*   ifPlatformSupportedr-   !   sD    $ 1XWc W# W W W W  Nr,   c                   &   e Zd ZdZddee   dee   fdZddZ	ddZ
ddZdd	Zdd
ZddZddZddZddZddZddZedd       Zedd       Zedd       Zedd       Zedd       Zedd       ZddZddZedd       Zedd       Zy)PIDFileTestsz
    Tests for L{PIDFile}.
    Ncontentr   c                 ^    t        | j                               }||j                  |       |S N)r   mktemp
setContent)r   r0   filePaths      r*   r5   zPIDFileTests.filePathL   s+    DKKM*(r,   c                 V    t        | j                               }t        t        |       y)z5
        L{PIDFile} conforms to L{IPIDFile}.
        N)r   r5   r	   r   r   pidFiles     r*   test_interfacezPIDFileTests.test_interfaceR   s     $--/*Xw'r,   c                 P    | j                  t        j                  d      d       y)zR
        L{PIDFile._format} returns the expected format when given a PID.
        9  pids   1337
N)assertEqualr   _format)r   s    r*   test_formatWithPIDzPIDFileTests.test_formatWithPIDY   s     	T2I>r,   c                     d}t        | j                  t        j                  |                  }| j                  ||j	                                y)zK
        L{PIDFile.read} returns the PID from the given file path.
        r;   r<   N)r   r5   r?   r>   readr   r=   r8   s      r*   test_readWithPIDzPIDFileTests.test_readWithPID_   s;     $--C(@ABglln-r,   c                     d}t        | j                  d            }| j                  t        |j                        }| j                  t        |      d|       y)f
        L{PIDFile.read} raises L{InvalidPIDFileError} when given an empty file
        path.
        r,   #non-integer PID value in PID file: Nr   r5   r!   r   rB   r>   r&   r   pidValuer8   r)   s       r*   test_readEmptyPIDzPIDFileTests.test_readEmptyPIDi   sO    
 $--,-17<<@Q#Fxl!STr,   c                     d}t        | j                  |            }| j                  t        |j                        }| j                  t        |      d|       y)rF   s   $foo!rG   NrH   rI   s       r*   test_readWithBogusPIDz"PIDFileTests.test_readWithBogusPIDt   sO    
 $--1217<<@Q#Fxl!STr,   c                     t        | j                               }| j                  t        |j                        }| j                  t        |      d       y)zc
        L{PIDFile.read} raises L{NoPIDFound} when given a non-existing file
        path.
        PID file does not existN)r   r5   r!   r   rB   r>   r&   r   r8   r)   s      r*   test_readDoesntExistz!PIDFileTests.test_readDoesntExist   s?    
 $--/*j',,7Q!:;r,   c                    ddt         dt        fd}| j                  t        d|       t	        | j                               }| j                  t        |j                        }| j                  |j                  t        j                         y)z
        L{PIDFile.read} re-raises L{OSError} if the associated C{errno} is
        anything other than L{errno.ENOENT}.
        moder   c                 6    t        t        j                  d      )Nz	I/O error)OSErrorerrnoEIO)rS   s    r*   oopsz>PIDFileTests.test_readOpenRaisesOSErrorNotENOENT.<locals>.oops   s    %))[11r,   openN)r)r&   r
   patchr   r   r5   r!   rU   rB   r>   rV   rW   )r   rX   r8   errors       r*   #test_readOpenRaisesOSErrorNotENOENTz0PIDFileTests.test_readOpenRaisesOSErrorNotENOENT   se    	2s 	2X 	2 	

8VT*$--/*!!'7<<8eii0r,   c                     d}t        | j                               }|j                  |       | j                  |j	                         |       y)z9
        L{PIDFile._write} stores the given PID.
        i  N)r   r5   _writer>   rB   rC   s      r*   test_writePIDzPIDFileTests.test_writePID   s;     $--/*s-r,   c                 x    t        | j                               }| j                  t        |j                  d       y)zS
        L{PIDFile._write} raises L{ValueError} when given an invalid PID.
        burpN)r   r5   r!   
ValueErrorr_   r7   s     r*   test_writePIDInvalidz!PIDFileTests.test_writePIDInvalid   s*     $--/**gnnf=r,   c                     t        | j                               }|j                          | j                  |j	                         t                      y)zT
        L{PIDFile.writeRunningPID} stores the PID for the current process.
        N)r   r5   writeRunningPIDr>   rB   r   r7   s     r*   test_writeRunningPIDz!PIDFileTests.test_writeRunningPID   s8     $--/*!2r,   c                     t        | j                  d            }| j                  |j                  j                                |j	                          | j                  |j                  j                                y)z9
        L{PIDFile.remove} removes the PID file.
        r,   N)r   r5   r%   existsremoveassertFalser7   s     r*   test_removezPIDFileTests.test_remove   sY     $--,-((//12))0023r,   c                     t        | j                               }|j                  d       dt        dt        ddfd}| j	                  t
        d|       | j                  |j                                y)zR
        L{PIDFile.isRunning} returns true for a process that does exist.
        r;   r=   signalr   Nc                      y r2    r=   rn   s     r*   killz2PIDFileTests.test_isRunningDoesExist.<locals>.kill       r,   rr   r   r5   r_   intr[   r   r%   	isRunningr   r8   rr   s      r*   test_isRunningDoesExistz$PIDFileTests.test_isRunningDoesExist   sa    
 $--/*t	c 	3 	4 	 	

8VT*))+,r,   c                     t        | j                               }|j                          | j                  |j	                                y)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   r5   rf   r%   rv   r7   s     r*   test_isRunningThiszPIDFileTests.test_isRunningThis   s5     $--/*!))+,r,   c                     t        | j                               }|j                  d       dt        dt        ddfd}| j	                  t
        d|       | j                  t        |j                         y)z{
        L{PIDFile.isRunning} raises L{StalePIDFileError} for a process that
        does not exist (errno=ESRCH).
        r;   r=   rn   r   Nc                 6    t        t        j                  d      NzNo such processrU   rV   ESRCHrq   s     r*   rr   z5PIDFileTests.test_isRunningDoesNotExist.<locals>.kill       %++'899r,   rr   )	r   r5   r_   ru   r[   r   r!   r   rv   rw   s      r*   test_isRunningDoesNotExistz'PIDFileTests.test_isRunningDoesNotExist   sc     $--/*t	:c 	:3 	:4 	: 	

8VT*+W->->?r,   c                     t        | j                               }|j                  d       dt        dt        ddfd}| j	                  t
        d|       | j                  |j                                y)zx
        L{PIDFile.isRunning} returns true for a process that we are not allowed
        to kill (errno=EPERM).
        r;   r=   rn   r   Nc                 6    t        t        j                  d      )NzOperation not permitted)rU   rV   EPERMrq   s     r*   rr   z3PIDFileTests.test_isRunningNotAllowed.<locals>.kill   s    %++'@AAr,   rr   rt   rw   s      r*   test_isRunningNotAllowedz%PIDFileTests.test_isRunningNotAllowed   se     $--/*t	Bc 	B3 	B4 	B 	

8VT*))+,r,   c                     t         dk7  rt        d      t        | j                               }|j	                  d       | j                  |j                                y)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   r5   r_   r%   rv   r7   s     r*   test_isRunningInitzPIDFileTests.test_isRunningInit   sI     '!455$--/*q))+,r,   c                     t        | j                               }|j                          dt        dt        ddfd}| j	                  t
        d|       | j                  t        |j                         y)z
        L{PIDFile.isRunning} re-raises L{OSError} if the attached C{errno}
        value from L{os.kill} is not an expected one.
        r=   rn   r   Nc                 6    t        t        j                  d      )NzFile exists)rU   rV   EEXISTrq   s     r*   rr   z5PIDFileTests.test_isRunningUnknownErrno.<locals>.kill  s    %,,66r,   rr   )	r   r5   rf   ru   r[   r   r!   rU   rv   rw   s      r*   test_isRunningUnknownErrnoz'PIDFileTests.test_isRunningUnknownErrno  sb     $--/*!	7c 	73 	74 	7 	

8VT*'7#4#45r,   c                 t    t        | j                               }| j                  |j                                y)zS
        L{PIDFile.isRunning} returns false if the PID file doesn't exist.
        N)r   r5   rk   rv   r7   s     r*   test_isRunningNoPIDFilez$PIDFileTests.test_isRunningNoPIDFile#  s+     $--/***,-r,   c                    t        | j                               }| j                  |j                  j                                |5  | j	                  |j                  j                                | j                  |j                         t                      ddd       | j                  |j                  j                                y# 1 sw Y   3xY w)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   r5   rk   ri   r%   r>   rB   r   r7   s     r*   test_contextManagerz PIDFileTests.test_contextManager+  s    
 $--/*))0023 	7OOG,,3356W\\^VX6	7 	))0023		7 	7s   AC		Cc                    t        | j                               }|j                  d       dt        dt        ddfd}| j	                  t
        d|       | j                  t        |j                        }| j                  t        |      d       |5  | j                  |j                         t                      ddd       y# 1 sw Y   yxY w)	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.
        r;   r=   rn   r   Nc                 6    t        t        j                  d      r}   r~   rq   s     r*   rr   z9PIDFileTests.test_contextManagerDoesntExist.<locals>.killC  r   r,   rr   z'PID file refers to non-existing process)r   r5   r_   ru   r[   r   r!   r   rv   r>   r&   rB   r   )r   r8   rr   r)   s       r*   test_contextManagerDoesntExistz+PIDFileTests.test_contextManagerDoesntExist9  s     $--/*t	:c 	:3 	:4 	: 	

8VT*/1B1BCQ!JK 	7W\\^VX6	7 	7 	7s   )CCc                 (   t        | j                               }|j                  d       dt        dt        ddfd}| j	                  t
        d|       | j                  |j                                | j                  t        |j                         y)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.
        r;   r=   rn   r   Nc                      y r2   rp   rq   s     r*   rr   z<PIDFileTests.test_contextManagerAlreadyRunning.<locals>.killX  rs   r,   rr   )r   r5   r_   ru   r[   r   r%   rv   r!   r   	__enter__rw   s      r*   !test_contextManagerAlreadyRunningz.PIDFileTests.test_contextManagerAlreadyRunningN  sx     $--/*t	c 	3 	4 	 	

8VT*))+,-w/@/@Ar,   r2   r   N)__name__
__module____qualname____doc__r   bytesr   r&   r5   r9   r@   rD   rK   rM   rQ   r]   r`   rd   rg   rl   r-   rx   rz   r   r   r   r   r   r   r   r   rp   r,   r*   r/   r/   G   s	    8C= (?.	U	U<1 	.>34 - - - - @ @ - - - -, 6 6.4 7 7( B Br,   r/   c                   H    e Zd ZdZd
dZd
dZd
dZd
dZd
dZd
dZ	d
d	Z
y)NonePIDFileTestsz#
    Tests for L{NonePIDFile}.
    Nc                 8    t               }t        t        |       y)z9
        L{NonePIDFile} conforms to L{IPIDFile}.
        N)r   r	   r   r7   s     r*   r9   zNonePIDFileTests.test_interfaceg  s     -Xw'r,   c                     t               }| j                  t        |j                        }| j	                  t        |      d       y)z;
        L{NonePIDFile.read} raises L{NoPIDFound}.
        rO   N)r   r!   r   rB   r>   r&   rP   s      r*   	test_readzNonePIDFileTests.test_readn  s6     -j',,7Q!:;r,   c                     t               }| j                  t        |j                  d      }| j	                  |j
                  t
        j                         y)zZ
        L{NonePIDFile._write} raises L{OSError} with an errno of L{errno.EPERM}.
        r   N)r   r!   rU   r_   r>   rV   r   r   r8   r\   s      r*   
test_writezNonePIDFileTests.test_writew  s;     -!!'7>>1=ekk2r,   c                     t               }| j                  t        |j                        }| j	                  |j
                  t
        j                         y)zk
        L{NonePIDFile.writeRunningPID} raises L{OSError} with an errno of
        L{errno.EPERM}.
        N)r   r!   rU   rf   r>   rV   r   r   s      r*   rg   z%NonePIDFileTests.test_writeRunningPID  s;    
 -!!'7+B+BCekk2r,   c                     t               }| j                  t        |j                        }| j	                  |j
                  t
        j                         y)zZ
        L{NonePIDFile.remove} raises L{OSError} with an errno of L{errno.EPERM}.
        N)r   r!   rU   rj   r>   rV   ENOENTr   s      r*   rl   zNonePIDFileTests.test_remove  s9     -!!'7>>:ell3r,   c                 X    t               }| j                  |j                         d       y)z<
        L{NonePIDFile.isRunning} returns L{False}.
        FN)r   r>   rv   r7   s     r*   test_isRunningzNonePIDFileTests.test_isRunning  s$     -**,e4r,   c                 H    t               }|5  	 ddd       y# 1 sw Y   yxY w)zo
        When used as a context manager, a L{NonePIDFile} doesn't raise, despite
        not existing.
        N)r   r7   s     r*   r   z$NonePIDFileTests.test_contextManager  s&    
 - 		 	 	s   !r   )r   r   r   r   r9   r   r   rg   rl   r   r   rp   r,   r*   r   r   b  s*    (<3345r,   r   )&r   rV   	functoolsr   osr   r   r   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   rp   r,   r*   <module>r      s      * * * . &  , + +   #8CH- #(382D #LXB7==))22 XBvAw}}--66 Ar,   