Ϫf+UdZddlZddlZddlZddlmZeeed< ddlZddl m Z dZ ddl mZGdd ZGd d ZGd d eZGddeZy#e $rdZ Y:wxYw)z; Tests for POSIX-based L{IReactorProcess} implementations. N)Optional platformSkip)processznon-POSIX platform)TestCasec(eZdZdZdZdZdZdZy)FakeFilez> A dummy file object which records when it is closed. c ||_||_yN)testcasefd)selfr r s I/usr/lib/python3/dist-packages/twisted/internet/test/test_posixprocess.py__init__zFakeFile.__init__ s  cb|jjj|jyr )r _filesremover r s rclosezFakeFile.close$s ##DGG,rc|Sr rs r __enter__zFakeFile.__enter__'s rc$|jyr )r)r exc_type exc_value tracebacks r__exit__zFakeFile.__exit__*s  rN)__name__ __module__ __qualname____doc__rrrrrrrrrs-rrc eZdZdZdZdZdZy)FakeResourceModulez Fake version of L{resource} which hard-codes a particular rlimit for maximum open files. @ivar _limit: The value to return for the hard limit of number of open files. c||_yr )_limitr limits rrzFakeResourceModule.__init__8s  rcD||jk(rd|jgSddgS)zX A fake of L{resource.getrlimit} which returns a pre-determined result. r{i) RLIMIT_NOFILEr&)r nos r getrlimitzFakeResourceModule.getrlimit;s+ ## #t{{# #SzrN)rrr r!r+rr-rrrr#r#.sMrr#cteZdZdZeZdZdZdZdZ dZ dZ dZ dZ dZd Zd Zd Zd Zd ZdZdZdZy)FDDetectorTestsa Tests for _FDDetector class in twisted.internet.process, which detects which function to drop in place for the _listOpenFDs method. @ivar devfs: A flag indicating whether the filesystem fake will indicate that /dev/fd exists. @ivar accurateDevFDResults: A flag indicating whether the /dev/fd fake returns accurate open file information. @ivar procfs: A flag indicating whether the filesystem fake will indicate that /proc//fd exists. Fcy)z> Fake os.getpid, always return the same thing r*rrs rgetpidzFDDetectorTests.getpidZsrctt|j}|jr|d|j fzk(r|S|j r|dk(r|j r|SgdSt)z Fake os.listdir, depending on what mode we're in to simulate behaviour. @param arg: the directory to list z /proc/%d/fdz/dev/fd)012)mapstrrprocfsr1devfsaccurateDevFDResultsOSError)r argaccurates rlistdirzFDDetectorTests.listdir`s^ sDKK( ;;3=DKKM3C#CDO ::#*((" "irc t|tttdt|jz }|jj |j |S)aO This is a mock for L{open}. It keeps track of opened files so extra descriptors can be returned from the mock for L{os.listdir} when used on one of the list-of-filedescriptors directories. A L{FakeFile} is returned which can be closed to remove the new descriptor from the open list. )rminsetrangerappendr )r fnamemodefs ropenfilezFDDetectorTests.openfileosG T3s5;/#dkk2BBC D 144 rc*dtjd<y)zp Make the L{resource} module unimportable for the remainder of the current test method. Nresource)sysmodulesrs rhideResourceModulez"FDDetectorTests.hideResourceModule}s #' Jrc<t|tjd<y)a Make a L{FakeResourceModule} instance importable at the L{resource} name. @param limit: The value which will be returned for the hard limit of number of open files by the fake resource module's C{getrlimit} function. rJN)r#rKrLr's rrevealResourceModulez$FDDetectorTests.revealResourceModules#5U"; Jrcr| tjd=y|tjd<y#t$rYywxYw)zI Restore the original resource module to L{sys.modules}. NrJ)rKrLKeyError)r values rreplaceResourceModulez%FDDetectorTests.replaceResourceModules> = KK +',CKK #  s * 66c\tj|_|j|j_|j|j_|j |j_gd|_|j|jtjjdy)z Set up the tests, giving ourselves a detector object to play with and setting up its testable knobs to refer to our mocked versions. rr$rJN) r _FDDetectordetectorr>r1rHr addCleanuprSrKrLgetrs rsetUpzFDDetectorTests.setUpsl  ++- $  #{{ !%   22CKKOOJ4OPrcd}d}fd}|||gj_j|jjy)z L{FDDetector._getImplementation} returns the first method from its C{_implementations} list which returns results which reflect a newly opened file descriptor. ctd)NzThis does not work) ValueErrorrrrfailWithExceptionzBFDDetectorTests.test_selectFirstWorking..failWithExceptions12 2rc gdSNrUrrrrfailWithWrongResultszEFDDetectorTests.test_selectFirstWorking..failWithWrongResults rc"jddSr )rrsrcorrectz8FDDetectorTests.test_selectFirstWorking..corrects;;q> !rNrX_implementationsassertIs_getImplementation)r r_rbres` rtest_selectFirstWorkingz'FDDetectorTests.test_selectFirstWorkingsH 3  "  *  & gt}}??ABrcd}d}||g|j_|j||jjy)z L{FDDetector._getImplementation} returns the last method from its C{_implementations} list if none of the implementations manage to return results which reflect a newly opened file descriptor. c gdS)N) rrrrrbz=FDDetectorTests.test_selectLast..failWithWrongResultsrcrc gdSrarrrrfailWithOtherWrongResultszBFDDetectorTests.test_selectLast..failWithOtherWrongResultsrcrNrf)r rbrqs rtest_selectLastzFDDetectorTests.test_selectLasts@   ! %*  & /1Q1Q1STrc8tj}|jj}|j|jj}|j|jj}|j |||j ||y)z Check that the identity of _listOpenFDs changes after running _listOpenFDs the first time, but not after the second time it's run. In other words, check that the monkey patching actually works. N)rrW _listOpenFDsrassertNotEqual assertEqual)r rXfirstsecondthirds r!test_identityOfListOpenFDsChangesz1FDDetectorTests.test_identityOfListOpenFDsChangess}&&(%%..&&//%%.. E6* 'rcd|_|jt|jjd|_d|_|j gd|jj y)z L{_FDDetector._devFDImplementation} raises L{OSError} if there is no I{/dev/fd} directory, otherwise it returns the basenames of its children interpreted as integers. FTrUN)r9 assertRaisesr;rX_devFDImplementationr:rvrs rtest_devFDImplementationz(FDDetectorTests.test_devFDImplementationsQ   '4==#E#EF $)! DMM$F$F$HIrcd|_|jt|jjd|_|j gd|jj y)z L{_FDDetector._procFDImplementation} raises L{OSError} if there is no I{/proc//fd} directory, otherwise it returns the basenames of its children interpreted as integers. FTrUN)r8r|r;rX_procFDImplementationrvrs rtest_procFDImplementationz)FDDetectorTests.test_procFDImplementationsI   '4==#F#FG  DMM$G$G$IJrc\|jd|jttdt|jj |jd|jttdt|jj y)z L{_FDDetector._fallbackFDImplementation} uses the L{resource} module if it is available, returning a range of integers from 0 to the minimum of C{1024} and the hard I{NOFILE} limit. iir@N)rOrvlistrCrX_fallbackFDImplementationrs rtest_resourceFDImplementationz-FDDetectorTests.test_resourceFDImplementations~ !!#&  s d4==#J#J#LM !!$'  t tDMM$K$K$MN rc|j|jttdt|jj y)z L{_FDDetector._fallbackFDImplementation}, the implementation of last resort, succeeds with a fixed range of integers from 0 to 1024 when the L{resource} module is not importable. r@N)rMrvrrCrXrrs rtest_fallbackFDImplementationz-FDDetectorTests.test_fallbackFDImplementations> !  t tDMM$K$K$MN rN)rrr r!rskipr9r:r8r1r>rHrMrOrSr[rjrrrzr~rrrrrrr/r/Dsi  D E  F  ' < , QC0U((& J K $  rr/c eZdZdZeZdZdZy)FileDescriptorTestsz< Tests for L{twisted.internet.process._listOpenFDs} c  tjD]'} tj|tj)y#t$rD}|j t j|j d||j fzYd}~sd}~wwxYw)z File descriptors returned by L{_listOpenFDs} are mostly open. This test assumes that zero-legth writes fail with EBADF on closed file descriptors. z2fcntl(%d, F_GETFL) failed with unexpected errno %dN)rrtfcntlF_GETFLr;rverrnoEBADF)r r errs r test_openFDsz FileDescriptorTests.test_openFDs"sy&&( B  B .    KKIIH399o& s$A B  :BB c:ttj}tj}|j |j |tj|j }|j||j kDd |j|j |tjtj||j|tjy#tj|wxYw)zB L{_listOpenFDs} lists expected file descriptors. z>Expected duplicate file descriptor to be greater than originalN) openosdevnullrrtassertInfilenodup assertTruer assertNotIn)r rGopenfdsr s rtest_expectedFDsz$FileDescriptorTests.test_expectedFDs4s  &&( ahhj'*VVAHHJ   O L   GGI MM"g224 5 HHRL W1134 HHRLs 4DDN)rrr r!rrrrrrrrrs D$)5rr)r!rrrKtypingrr7__annotations__rtwisted.internetrr ImportErrortwisted.trial.unittestrrr#r/rrrrrs  sm)L+&,T hT nB5(B5U('L(sAA&%A&