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: //usr/lib/python3/dist-packages/twisted/runner/test/__pycache__/test_procmon.cpython-38.pyc
U


W[2[�@s�dZddlZddlmZddlmZmZddlmZm	Z	m
Z
ddlmZddl
mZddlmZdd	lmZGd
d�de�ZGdd
�d
ee�ZGdd�dej�ZGdd�dej�ZdS)z&
Tests for L{twisted.runner.procmon}.
�N)�unittest)�LoggingProtocol�ProcessMonitor)�ProcessDone�ProcessTerminated�ProcessExitedAlready)�Clock)�Failure)�log)�
MemoryReactorc@s6eZdZdZdZdZdZddd�Zdd�Zd	d
�Z	dS)�DummyProcessa 
    An incomplete and fake L{IProcessTransport} implementation for testing how
    L{ProcessMonitor} behaves when its monitored processes exit.

    @ivar _terminationDelay: the delay in seconds after which the DummyProcess
        will appear to exit when it receives a TERM signal
    �NrcCs@||_||_||_||_||_||_||_||_|	|_|
|_	dS�N)
�proto�_reactorZ_executableZ_args�_environment�_pathZ_uidZ_gidZ_usePTYZ	_childFDs)�self�reactor�
executable�argsZenvironment�pathr�uid�gid�usePTY�childFDs�r�B/usr/lib/python3/dist-packages/twisted/runner/test/test_procmon.py�__init__"szDummyProcess.__init__cCsL|jdfdd�}|jdkr t��||krH||\}}|j�||j|�|_dS)a�
        A partial implementation of signalProcess which can only handle TERM and
        KILL signals.
         - When a TERM signal is given, the dummy process will appear to exit
           after L{DummyProcess._terminationDelay} seconds with exit code 0
         - When a KILL signal is given, the dummy process will appear to exit
           immediately with exit code 1.

        @param signalID: The signal name or number to be issued to the process.
        @type signalID: C{str}
        r�rr
)ZTERM�KILLN)�_terminationDelay�pidrrZ	callLater�processEndedZ_signalHandler)rZsignalIDZparams�delay�statusrrr�
signalProcess2s
�
�zDummyProcess.signalProcesscCs,d|_ttd�}|j�t|||���dS)zC
        Deliver the process ended event to C{self.proto}.
        Nr)r"rrrr#r	)rr%Z	statusMaprrrr#Ls
�zDummyProcess.processEnded)NNrN)
�__name__�
__module__�__qualname__�__doc__r"rr!rr&r#rrrrrs�
rc@s0eZdZdZdd�Zdidddddfdd�ZdS)	�DummyProcessReactorz�
    @ivar spawnedProcesses: a list that keeps track of the fake process
        instances built by C{spawnProcess}.
    @type spawnedProcesses: C{list}
    cCst�|�t�|�g|_dSr)rrr�spawnedProcesses�rrrrr_s

zDummyProcessReactor.__init__rNrc
Cs4t||||||||||	�
}
|�|
�|j�|
�|
S)zz
        Fake L{reactor.spawnProcess}, that logs all the process
        arguments and returns a L{DummyProcess}.
        )rZmakeConnectionr,�append)rZprocessProtocolrr�envrrrrr�procrrr�spawnProcessfs�
z DummyProcessReactor.spawnProcess)r'r(r)r*rr1rrrrr+Ys�r+c@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<d=�Z!d>d?�Z"d@S)A�ProcmonTestsz&
    Tests for L{ProcessMonitor}.
    cCs2t�|_t|jd�|_d|j_d|j_d|j_dS)zL
        Create an L{ProcessMonitor} wrapped around a fake reactor.
        �r��
N)r+rr�pm�minRestartDelay�maxRestartDelay�	thresholdr-rrr�setUp{s
zProcmonTests.setUpcCsL|jjdddgddid�t|j�}|�d|�|�d|�|�d|�d	S)
z+
        Repr includes all details
        �foo�arg1�arg2r
r4�rrr/�1�2N)r6�
addProcess�repr�assertIn�rZrepresentationrrr�test_reprLooksGood�s�
zProcmonTests.test_reprLooksGoodcCs<|jjdddgid�t|j�}|�d|�|�d|�dS)z�
        Repr does not include unneeded details.

        Values of attributes that just mean "inherit from launching
        process" do not appear in the repr of a process.
        r;r<r=)r/�(�)N)r6rArB�assertNotInrDrrr�test_simpleReprLooksGood�s
z%ProcmonTests.test_simpleReprLooksGoodcCsD|jjdddgddid�|�|j��ddddgddifi�dS)	zW
        The list of monitored processes must be included in the pickle state.
        r;r<r=r
r4r>�	processesN)r6rA�assertEqual�__getstate__r-rrr�test_getStateIncludesProcesses�s��z+ProcmonTests.test_getStateIncludesProcessescCs|�d|j���dS)z~
        The private L{ProcessMonitor._reactor} instance variable should not be
        included in the pickle state.
        rN)rHr6rLr-rrr�test_getStateExcludesReactor�sz)ProcmonTests.test_getStateExcludesReactorcCs~|jjdddgddid�|�|jji�|�|jjdddgddifi�|j��|j�d�|�t|jj�	��dg�dS)	z�
        L{ProcessMonitor.addProcess} only starts the named program if
        L{ProcessMonitor.startService} has been called.
        r;r<r=r
r4r>rN)
r6rArK�	protocolsrJ�startServicer�advance�list�keysr-rrr�test_addProcess�s�
�
zProcmonTests.test_addProcessc	Cs>|jjdddgddid�|jt|jjdddgddid�dS)z|
        L{ProcessMonitor.addProcess} raises a C{KeyError} if a process with the
        given name already exists.
        r;r<r=r
r4r>N)r6rA�assertRaises�KeyErrorr-rrr� test_addProcessDuplicateKeyError�s��z-ProcmonTests.test_addProcessDuplicateKeyErrorcCsPddi}|j��|jjddgdd|d�|j�d�|�|jjdj|�dS)	z�
        L{ProcessMonitor.addProcess} takes an C{env} parameter that is passed to
        L{IReactorProcess.spawnProcess}.
        ZKEY�valuer;r
r4r>rN)r6rPrArrQrKr,r)rZfakeEnvrrr�test_addProcessEnv�s
�zProcmonTests.test_addProcessEnvcCsD|j��|jjddgdd�|j�d�|�|jjdjd�dS)z�
        L{ProcessMonitor.addProcess} takes an C{cwd} parameter that is passed
        to L{IReactorProcess.spawnProcess}.
        r;z	/mnt/lala)�cwdrN)r6rPrArrQrKr,rr-rrr�test_addProcessCwd�s
�zProcmonTests.test_addProcessCwdcCsR|j��|j�ddg�|�t|jj�d�|j�d�|�t|jj�d�dS)zm
        L{ProcessMonitor.removeProcess} removes the process from the public
        processes list.
        r;r
rN)r6rPrArK�lenrJ�
removeProcessr-rrr�test_removeProcess�s

zProcmonTests.test_removeProcesscCs |j��|�t|jjd�dS)zz
        L{ProcessMonitor.removeProcess} raises a C{KeyError} if the given
        process name isn't recognised.
        r;N)r6rPrUrVr]r-rrr�!test_removeProcessUnknownKeyError�s
z.ProcmonTests.test_removeProcessUnknownKeyErrorcCsH|j�ddg�|j�d�|�|jjdt�|�d|jj���dS)a
        When a process has been started, an instance of L{LoggingProtocol} will
        be added to the L{ProcessMonitor.protocols} dict and the start time of
        the process will be recorded in the L{ProcessMonitor.timeStarted}
        dictionary.
        r;N)	r6rA�startProcessZassertIsInstancerOrrC�timeStartedrSr-rrr�test_startProcess�szProcmonTests.test_startProcesscCs2|j�ddg�|j�d�|�|j�d��dS)zr
        L{ProcessMonitor.startProcess} silently returns if the named process is
        already started.
        r;N)r6rAr`�assertIsNoner-rrr�test_startProcessAlreadyStartedsz,ProcmonTests.test_startProcessAlreadyStartedcCs|�t|jjd�dS)zy
        L{ProcessMonitor.startProcess} raises a C{KeyError} if the given
        process name isn't recognised.
        r;N)rUrVr6r`r-rrr� test_startProcessUnknownKeyErrorsz-ProcmonTests.test_startProcessUnknownKeyErrorcCs�|j��|j�ddg�|�d|jj�d}|jjdj_|j�|jj	�|j�
d�|j�|�|�|j��|jj
d�dS)zm
        L{ProcessMonitor.stopProcess} immediately sends a TERM signal to the
        named process.
        r;r
N)r6rPrArCrO�	transportr!rrQr9�stopProcessrK�secondsra)rZ	timeToDierrr�"test_stopProcessNaturalTerminations

�z/ProcmonTests.test_stopProcessNaturalTerminationcCs�|j��|j�ddg�|�d|jj�|j�|jj�|jjdj}|jj	d|_
|j�d�|j�|jj	d�|�d|jj
d�|j�d�|�|j��|jj
d�dS)z�
        L{ProcessMonitor.stopProcess} kills a process which fails to terminate
        naturally within L{ProcessMonitor.killTime} seconds.
        r;r
gN)r6rPrArCrOrrQr9rf�killTimer!rgrKrarh)rr0rrr�test_stopProcessForcedKill3s
z'ProcmonTests.test_stopProcessForcedKillcCs|�t|jjd�dS)zx
        L{ProcessMonitor.stopProcess} raises a C{KeyError} if the given process
        name isn't recognised.
        r;N)rUrVr6rgr-rrr�test_stopProcessUnknownKeyErrorJsz,ProcmonTests.test_stopProcessUnknownKeyErrorcCs&|j�ddg�|�|j�d��dS)z�
        L{ProcessMonitor.stopProcess} silently returns if the named process
        is already stopped. eg Process has crashed and a restart has been
        rescheduled, but in the meantime, the service is stopped.
        r;N)r6rArcrgr-rrr�test_stopProcessAlreadyStoppedRsz+ProcmonTests.test_stopProcessAlreadyStoppedcCs�g}|�tj|j�t�|j�|j�ddg�|j��|j�	d�|�
d|jj�|j�	|jj�|jjd�
d�|�t|�d�|dd}|�|tdg��dS)zI
        Getting a complete output line generates a log message.
        r;rs
hello world!
r
�message�[foo] hello world!N)�
addCleanupr
�removeObserverr.�addObserverr6rArPrrQrCrOr9�outReceived�assertEqualsr\�tuple�r�eventsrnrrr�test_outputReceivedCompleteLine\s
z,ProcmonTests.test_outputReceivedCompleteLinecCs�g}|�tj|j�t�|j�|j�ddg�|j��|j�	d�|�
d|jj�|j�	|jj�|jjd�
d�|�t|�d�|dd}|�t|�d�|d}|�dd�\}}|�|d�|�|td��d	S)
zN
        Getting invalid UTF-8 results in the repr of the raw message
        r;rs�hello world!
r
rn� z[foo]s
�hello world!N)rpr
rqr.rrr6rArPrrQrCrOr9rsrtr\�splitrB)rrwZmessagesrn�tag�outputrrr�*test_outputReceivedCompleteLineInvalidUTF8rs 
z7ProcmonTests.test_outputReceivedCompleteLineInvalidUTF8cCs�g}|�tj|j�t�|j�|j�ddg�|j��|j�	d�|�
d|jj�|j�	|jj�|jjd�
d�|�t|�d�|jjd�ttd���|�t|�d�|dd}|�|tdg��dS)zM
        Getting partial line results in no events until process end
        r;rshello world!r
rnroN)rpr
rqr.rrr6rArPrrQrCrOr9rsrtr\r#r	rrurvrrr�test_outputReceivedPartialLine�s
z+ProcmonTests.test_outputReceivedPartialLinecCs�|j�ddg�|j��|j�d�|�d|jj�|j�|jj�|jjd�t	t
d���|�d|jj�|j�d�|�d|jj�dS)z�
        L{ProcessMonitor.connectionLost} should immediately restart a process
        if it has been running longer than L{ProcessMonitor.threshold} seconds.
        r;rN)r6rArPrrQrCrOr9r#r	rrHr-rrr�#test_connectionLostLongLivedProcess�s
z0ProcmonTests.test_connectionLostLongLivedProcesscCs�|j�ddg�|j��|j�d�|j�d�|�d|jj�|jjd}|�|�	��|j�|jj
djj�|�
|�	��|�d|jj�dS)z�
        L{ProcessMonitor.connectionLost} cancels a scheduled process killer and
        deletes the DelayedCall from the L{ProcessMonitor.murder} list.
        r;r
N)r6rArPrrQrgrCZmurder�
assertTrue�activerOrfr!�assertFalserH)rZdelayedCallrrr�test_connectionLostMurderCancel�s
�z,ProcmonTests.test_connectionLostMurderCancelcCsj|j��|j�ddg�|�d|jj�|jjdj�d�|j�|jjdjj	�|�
d|jj�dS)z�
        L{ProcessMonitor.connectionLost} removes the corresponding
        ProcessProtocol instance from the L{ProcessMonitor.protocols} list.
        r;r N)r6rPrArCrOrfr&rrQr!rHr-rrr�#test_connectionLostProtocolDeletion�s
�z0ProcmonTests.test_connectionLostProtocolDeletioncCs�d|j_d|j_|j��|j�ddg�|�|jjd|jj�|j�|jj	d�|jj
d�tt
d���|�|jjd|jj�dS)z}
        L{ProcessMonitor.connectionLost} will wait at least minRestartDelay s
        and at most maxRestartDelay s
        r4�r;r
rN)r6r7r8rPrArKr$rrQr9rOr#r	rr-rrr�%test_connectionLostMinMaxRestartDelay�s
z2ProcmonTests.test_connectionLostMinMaxRestartDelaycCs�|j��|j�ddg�|j�|jjd�|�d|jj�|�|jj	d|jj
�|jjd�tt
d���|�|jj	d|jj
d�dS)z|
        L{ProcessMonitor.connectionLost} doubles the restart delay each time
        the process dies too quickly.
        r;r
rr4N)r6rPrArrQr9rCrOrKr$r7r#r	rr-rrr�&test_connectionLostBackoffDelayDoubles�s
z3ProcmonTests.test_connectionLostBackoffDelayDoublescCs:|j�ddg�|j��|j�d�|�d|jj�dS)zP
        L{ProcessMonitor.startService} starts all monitored processes.
        r;rN)r6rArPrrQrCrOr-rrr�test_startService�s
zProcmonTests.test_startServicecCs�|j�ddg�|j�ddg�|j��|j�|jj�|�d|jj�|�d|jj�|j�d�|j��|j�|jj	d�|�
i|jj�dS)zT
        L{ProcessMonitor.stopService} should stop all monitored processes.
        r;Zbarr
N)r6rArPrrQr9rCrO�stopServicerjrKr-rrr�test_stopService	s

zProcmonTests.test_stopServicecCsl|j�ddg�|j��|j�d�|j��|j�d�t|jj�}|��}|�	|g�|�
|j�dS)zR
        L{ProcessMonitor.restartAll} succeeds when there is one process.
        r;r
N)r6rArPrrQZ
restartAllrRr,�poprtrcr")rrJZ	myProcessrrr�!test_restartAllRestartsOneProcess s

z.ProcmonTests.test_restartAllRestartsOneProcesscCs�|j�ddg�|j��|j�|jj�|�d|jj�|j�d�|jjd�t	t
d���|�|jjd�
��|j��|�|jjd�
��dS)ze
        L{ProcessMonitor.stopService} should cancel any scheduled process
        restarts.
        r;r
rN)r6rArPrrQr9rCrOr#r	rr�Zrestartr�r�r�r-rrr�test_stopServiceCancelRestarts0s

z+ProcmonTests.test_stopServiceCancelRestartscCsxd|j_d|j_|j��|j�ddg�|j�d�|j�d�|j�d�|j��|j�d�|�	|jj
i�dS)ze
        L{ProcessMonitor.stopService} should cancel all scheduled process
        restarts.
        �r;r
�N)r6r9r7rPrArrQrgr�rKrOr-rrr�(test_stopServiceCleanupScheduledRestartsEs

z5ProcmonTests.test_stopServiceCleanupScheduledRestartsN)#r'r(r)r*r:rErIrMrNrTrWrYr[r^r_rbrdrerirkrlrmrxr}r~rr�r�r�r�r�r�r�r�r�rrrrr2vs@


	


r2c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�DeprecationTestszQ
    Tests that check functionality that should be deprecated is deprecated.
    cCst�|_t|jd�|_dS)z5
        Create reactor and process monitor.
        r3N)r+rrr6r-rrrr:dszDeprecationTests.setUpcCsx|j�ddg�|jj}|�t|�d�|��}d}|D]$}|�|dt�d|dkr8d}q8|�|d�	t
|���d	S)
a
        _Process.toTuple is deprecated.

        When getting the deprecated processes property, the actual
        data (kept in the class _Process) is converted to a tuple --
        which produces a DeprecationWarning per process so converted.
        r;r
F�categoryZtoTuplernTzno tuple deprecation found:{}N)r6rArJrtr\�
flushWarnings�assertIs�DeprecationWarningr��formatrB)rZmyprocesses�warningsZfoundToTuple�warningrrr�test_toTuplels�zDeprecationTests.test_toTuplecCsF|jj}|�|i�|��}|�d�}|�|dt�|�|g�dS)a
        Accessing L{ProcessMonitor.processes} results in deprecation warning

        Even when there are no processes, and thus no process is converted
        to a tuple, accessing the L{ProcessMonitor.processes} property
        should generate its own DeprecationWarning.
        rr�N)r6rJrtr�r�r�r�)rZmyProcessesr��firstrrr�test_processes�s
zDeprecationTests.test_processescCs2t�|j�|��}|D]}|�|dt�qdS)zO
        Pickling an L{ProcessMonitor} results in deprecation warnings
        r�N)�pickle�dumpsr6r�r�r�)rr�r�rrr�
test_getstate�szDeprecationTests.test_getstateN)r'r(r)r*r:r�r�r�rrrrr�^s
r�)r*r�Z
twisted.trialrZtwisted.runner.procmonrrZtwisted.internet.errorrrrZtwisted.internet.taskrZtwisted.python.failurer	Ztwisted.pythonr
Ztwisted.test.proto_helpersr�objectrr+ZTestCaser2ZSynchronousTestCaser�rrrr�<module>sEk