
    Yfh2                     Z   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j!        j"        j#                  Z$ G 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           	      v     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                 4   t          j                    dk    }|r | g|R i |S  | j        t          t          | j        f| g|R i |}t          |t                    r7|                     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er   s        ^/var/www/html/env/lib/python3.11/site-packages/twisted/application/runner/test/test_pidfile.pywrapperz$ifPlatformSupported.<locals>.wrapper3   s    $&&'1	 	W1T+D+++F+++!!$h0EF 	  
  A !011 WA 1 12T U UVVVVVW W    )r   r   )r   r+   s   ` r*   ifPlatformSupportedr-   !   s`    $ 1XXWc W# W W W W W W W XW  Nr,   c                   d   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dS )PIDFileTestsz
    Tests for L{PIDFile}.
    Ncontentr   c                 v    t          |                                           }||                    |           |S N)r   mktemp
setContent)r   r0   filePaths      r*   r5   zPIDFileTests.filePathL   s6    DKKMM**(((r,   c                 r    t          |                                           }t          t          |           dS )z5
        L{PIDFile} conforms to L{IPIDFile}.
        N)r   r5   r	   r   r   pidFiles     r*   test_interfacezPIDFileTests.test_interfaceR   s/     $--//**Xw'''''r,   c                 X    |                      t          j        d          d           dS )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222I>>>>>r,   c                     d}t          |                     t          j        |                              }|                     ||                                           dS )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_   sS     $--C(@(@(@AABBgllnn-----r,   c                     d}t          |                     d                    }|                     t          |j                  }|                     t          |          d|           dS )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   se    
 $--,,--17<@@Q!Sx!S!STTTTTr,   c                     d}t          |                     |                    }|                     t          |j                  }|                     t          |          d|           dS )rF   s   $foo!rG   NrH   rI   s       r*   test_readWithBogusPIDz"PIDFileTests.test_readWithBogusPIDt   se    
 $--112217<@@Q!Sx!S!STTTTTr,   c                     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   r5   r!   r   rB   r>   r&   r   r8   r)   s      r*   test_readDoesntExistz!PIDFileTests.test_readDoesntExist   sS    
 $--//**j',77Q!:;;;;;r,   c                 0   ddt           dt          f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                 6    t          t          j        d          )Nz	I/O error)OSErrorerrnoEIO)rT   s    r*   oopsz>PIDFileTests.test_readOpenRaisesOSErrorNotENOENT.<locals>.oops   s    %)[111r,   openN)rS   )r&   r
   patchr   r   r5   r!   rV   rB   r>   rW   rX   )r   rY   r8   errors       r*   #test_readOpenRaisesOSErrorNotENOENTz0PIDFileTests.test_readOpenRaisesOSErrorNotENOENT   s    	2 	2s 	2X 	2 	2 	2 	2 	

8VT***$--//**!!'7<88ei00000r,   c                     d}t          |                                           }|                    |           |                     |                                |           dS )z9
        L{PIDFile._write} stores the given PID.
        i  N)r   r5   _writer>   rB   rC   s      r*   test_writePIDzPIDFileTests.test_writePID   sT     $--//**s-----r,   c                     t          |                                           }|                     t          |j        d           dS )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   s9     $--//***gnf=====r,   c                     t          |                                           }|                                 |                     |                                t                                 dS )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   sS     $--//**!!!22222r,   c                 "   t          |                     d                    }|                     |j                                                   |                                 |                     |j                                                   dS )z9
        L{PIDFile.remove} removes the PID file.
        r,   N)r   r5   r%   existsremoveassertFalser7   s     r*   test_removezPIDFileTests.test_remove   sw     $--,,--(//11222)002233333r,   c                     t          |                                           }|                    d           dt          dt          ddfd}|                     t
          d|           |                     |                                           dS )zR
        L{PIDFile.isRunning} returns true for a process that does exist.
        r;   r=   signalr   Nc                     d S r2    r=   rn   s     r*   killz2PIDFileTests.test_isRunningDoesExist.<locals>.kill       Fr,   rr   r   r5   r_   intr[   r   r%   	isRunningr   r8   rr   s      r*   test_isRunningDoesExistz$PIDFileTests.test_isRunningDoesExist   s    
 $--//**t	c 	3 	4 	 	 	 	 	

8VT***))++,,,,,r,   c                     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   r5   rf   r%   rv   r7   s     r*   test_isRunningThiszPIDFileTests.test_isRunningThis   sO     $--//**!!!))++,,,,,r,   c                    t          |                                           }|                    d           dt          dt          ddfd}|                     t
          d|           |                     t          |j                   dS )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rV   rW   ESRCHrq   s     r*   rr   z5PIDFileTests.test_isRunningDoesNotExist.<locals>.kill       %+'8999r,   rr   )	r   r5   r_   ru   r[   r   r!   r   rv   rw   s      r*   test_isRunningDoesNotExistz'PIDFileTests.test_isRunningDoesNotExist   s     $--//**t	:c 	:3 	:4 	: 	: 	: 	: 	

8VT***+W->?????r,   c                     t          |                                           }|                    d           dt          dt          ddfd}|                     t
          d|           |                     |                                           dS )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)rV   rW   EPERMrq   s     r*   rr   z3PIDFileTests.test_isRunningNotAllowed.<locals>.kill   s    %+'@AAAr,   rr   rt   rw   s      r*   test_isRunningNotAllowedz%PIDFileTests.test_isRunningNotAllowed   s     $--//**t	Bc 	B3 	B4 	B 	B 	B 	B 	

8VT***))++,,,,,r,   c                     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   r5   r_   r%   rv   r7   s     r*   test_isRunningInitzPIDFileTests.test_isRunningInit   si     '!!4555$--//**q))++,,,,,r,   c                    t          |                                           }|                                 dt          dt          ddf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.
        r=   rn   r   Nc                 6    t          t          j        d          )NzFile exists)rV   rW   EEXISTrq   s     r*   rr   z5PIDFileTests.test_isRunningUnknownErrno.<locals>.kill  s    %,666r,   rr   )	r   r5   rf   ru   r[   r   r!   rV   rv   rw   s      r*   test_isRunningUnknownErrnoz'PIDFileTests.test_isRunningUnknownErrno  s     $--//**!!!	7c 	73 	74 	7 	7 	7 	7 	

8VT***'7#455555r,   c                     t          |                                           }|                     |                                           dS )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                                                   |5  |                     |j                                                   |                     |                                t                                 ddd           n# 1 swxY 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   r5   rk   ri   r%   r>   rB   r   r7   s     r*   test_contextManagerz PIDFileTests.test_contextManager+  s   
 $--//**)0022333 	7 	7OOG,3355666W\\^^VXX666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	)002233333s   A!B==CCc                    t          |                                           }|                    d           dt          dt          ddfd}|                     t
          d|           |                     t          |j                  }| 	                    t          |          d           |5  | 	                    |                                t                                 ddd           dS # 1 swxY w 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.
        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***/1BCCQ!JKKK 	7 	7W\\^^VXX666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   ,5C..C25C2c                 `   t          |                                           }|                    d           dt          dt          ddf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.
        r;   r=   rn   r   Nc                     d S 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  s     $--//**t	c 	3 	4 	 	 	 	 	

8VT***))++,,,-w/@AAAA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   sk          8C=    ( ( ( (? ? ? ?. . . .	U 	U 	U 	U	U 	U 	U 	U< < < <1 1 1 1 	. 	. 	. 	.> > > >3 3 3 34 4 4 4 - - - - - - - - @ @ @ @ - - - - - - - -, 6 6 6 6. . . .4 4 4 4 7 7 7 7( B B B B B Br,   r/   c                   J    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
dS )NonePIDFileTestsz#
    Tests for L{NonePIDFile}.
    r   Nc                 L    t                      }t          t          |           dS )z9
        L{NonePIDFile} conforms to L{IPIDFile}.
        N)r   r	   r   r7   s     r*   r9   zNonePIDFileTests.test_interfaceg  s#     --Xw'''''r,   c                     t                      }|                     t          |j                  }|                     t          |          d           dS )z;
        L{NonePIDFile.read} raises L{NoPIDFound}.
        rO   N)r   r!   r   rB   r>   r&   rP   s      r*   	test_readzNonePIDFileTests.test_readn  sG     --j',77Q!:;;;;;r,   c                     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!   rV   r_   r>   rW   r   r   r8   r\   s      r*   
test_writezNonePIDFileTests.test_writew  sF     --!!'7>1==ek22222r,   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!   rV   rf   r>   rW   r   r   s      r*   rg   z%NonePIDFileTests.test_writeRunningPID  sE    
 --!!'7+BCCek22222r,   c                     t                      }|                     t          |j                  }|                     |j        t
          j                   dS )zZ
        L{NonePIDFile.remove} raises L{OSError} with an errno of L{errno.EPERM}.
        N)r   r!   rV   rj   r>   rW   ENOENTr   s      r*   rl   zNonePIDFileTests.test_remove  sD     --!!'7>::el33333r,   c                 r    t                      }|                     |                                d           dS )z<
        L{NonePIDFile.isRunning} returns L{False}.
        FN)r   r>   rv   r7   s     r*   test_isRunningzNonePIDFileTests.test_isRunning  s5     --**,,e44444r,   c                 Z    t                      }|5  	 ddd           dS # 1 swxY w Y   dS )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  sz    
 -- 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    $$r   )r   r   r   r   r9   r   r   rg   rl   r   r   rp   r,   r*   r   r   b  s         ( ( ( (< < < <3 3 3 33 3 3 34 4 4 45 5 5 5     r,   r   )&r   rW   	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 XB XB XB XB7=)2 XB XB XBvA A A A Aw}-6 A A A A Ar,   