ϪfddZddlZddlmZmZmZddlmZddlm Z ddl m Z ddl m Z ddlmZmZdd lmZGd d ZGd d e eZGddej*ZGddej.Zy)z& Tests for L{twisted.runner.procmon}. N) ProcessDoneProcessExitedAlreadyProcessTerminated)Clock) MemoryReactor)globalLogPublisher)Failure)LoggingProtocolProcessMonitor)unittestc8eZdZdZdZdZdZ ddZdZdZ y) 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 Nc ||_||_||_||_||_||_||_||_| |_| |_ yN) proto_reactor _executable_args _environment_path_uid_gid_usePTY _childFDs) selfreactor executableargs environmentpathruidgidusePTYchildFDss B/usr/lib/python3/dist-packages/twisted/runner/test/test_procmon.py__init__zDummyProcess.__init__ sL  % '    !c|jdfdd}|j t||vr5||\}}|jj ||j ||_yy)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} rrr)TERMKILLN)_terminationDelaypidrr callLater processEnded_signalHandler)rsignalIDparamsdelaystatuss r& signalProcesszDummyProcess.signalProcess9sk 1115vF 88 &( ( v "8,ME6"&--"9"9t((&#D  r(cd|_ttd}|jj t |||y)zC Deliver the process ended event to C{self.proto}. Nr*)r.rrrr0r )rr5 statusMaps r&r0zDummyProcess.processEndedPs>   (9 &(9&(A BCr()NNrN) __name__ __module__ __qualname____doc__r.rr-r'r6r0r(r&rrs7 C E  "2. Dr(rc,eZdZdZdZdidddddfdZy)DummyProcessReactorz @ivar spawnedProcesses: a list that keeps track of the fake process instances built by C{spawnProcess}. @type spawnedProcesses: C{list} cftj|tj|g|_yr)rr'rspawnedProcessesrs r&r'zDummyProcessReactor.__init__cs$t$ t "r(r=Nrc t|||||||||| } |j| |jj| | S)zz Fake L{reactor.spawnProcess}, that logs all the process arguments and returns a L{DummyProcess}. )rmakeConnectionrAappend) rprocessProtocolrrenvr!r"r#r$r%procs r& spawnProcessz DummyProcessReactor.spawnProcessisU"             &&t, $$T* r()r9r:r;r<r'rIr=r(r&r?r?\s( #    r(r?ceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#y")# ProcmonTestsz& Tests for L{ProcessMonitor}. ct|_t|j|_d|j_d|j_d|j_y)zL Create an L{ProcessMonitor} wrapped around a fake reactor. r N)r?rr pmminRestartDelaymaxRestartDelay thresholdrBs r&setUpzProcmonTests.setUpsB+,  6"#"$r(c|jjdddgddit|j}|jd||jd||jd|y ) z+ Repr includes all details fooarg1arg2rrNr"r#rG12N)rP addProcessreprassertInrrepresentations r&test_reprLooksGoodzProcmonTests.test_reprLooksGoods] 566"2qbIdgg e^, c>* c>*r(c|jjdddgit|j}|jd||jd|y)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. rVrWrX)rG()N)rPr\r] assertNotInr_s r&test_simpleReprLooksGoodz%ProcmonTests.test_simpleReprLooksGoodsO 566"2;dgg n- n-r(c|jjdddgddi|j|jjddddgddifiy) zW The list of monitored processes must be included in the pickle state. rVrWrXrrNrY processesN)rPr\ assertEqual __getstate__rBs r&test_getStateIncludesProcessesz+ProcmonTests.test_getStateIncludesProcessess_ 566"2qbI  GG "; /%66:JAqRT9U1V r(cX|jd|jjy)z~ The private L{ProcessMonitor._reactor} instance variable should not be included in the pickle state. rN)rerPrjrBs r&test_getStateExcludesReactorz)ProcmonTests.test_getStateExcludesReactors! TWW%9%9%;g}|jtj|jtj|j|j j ddg|j j|jjd|jd|j j|jj|j j|j jdjd|jt|d|dd}|dd}|dd}|dd}|j|d |j|d |j|d|j|d y ) zS Getting a complete output line on stdout generates a log message. rVr hello world! r log_namespacestreamtagline%twisted.runner.procmon.ProcessMonitorstdout hello world!N) addCleanuprremoveObserverrE addObserverrPr\rprrqr^rorS outReceived assertEqualsrrevents namespacerrrs r&test_outputReceivedCompleteLinez,ProcmonTests.test_outputReceivedCompleteLineeK *996==I&&v}}5 55'*  Q eTWW../ TWW../ % ,,->? #f+q)1Io. 8$Qiay  )%LM &(+ #u% $/r(c>g}|jtj|jtj|j|j j ddg|j j|jjd|jd|j j|jj|j j|j jdjd|jt|d|dd}|dd}|dd}|dd}|j|d |j|d |j|d|j|d y ) zS Getting a complete output line on stderr generates a log message. rVrrrrrrrrstderrrN)rrrrErrPr\rprrqr^rorS errReceivedrrrs r&!test_ouputReceivedCompleteErrLinez.ProcmonTests.test_ouputReceivedCompleteErrLinerr(cBg}|jtj|jtj|j|j j ddg|j j|jjd|jd|j j|jj|j j|j jdjd|jt|d|d}|d}|d}|d}|d}|j|d |j|d |j|d|j|t!d y ) zN Getting invalid UTF-8 results in the repr of the raw message rVrshello world! rrrrrrrs hello world!N)rrrrErrPr\rprrqr^rorSrrrr])rrmessagerrroutputs r&*test_outputReceivedCompleteLineInvalidUTF8z7ProcmonTests.test_outputReceivedCompleteLineInvalidUTF8sH *996==I&&v}}5 55'*  Q eTWW../ TWW../ % ,,-BC #f+q))O, "en )%LM &(+ #u% &$':";rsnWW'2-*B"GDGDT,-,^v08$$v0r6Cx336Cr(