Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyc


0Nc@s1dZddlmZmZmZddlmZddlmZddlm	Z	ddlm
Z
ddlmZm
Z
mZmZmZddlmZd	ejfd
YZdejfdYZd
ejfdYZdejfdYZdejfdYZdejfdYZdejfdYZdej fdYZ dejfdYZ!dejfdYZ"dejej#fdYZ$dejfd YZ%d!ejfd"YZ&d#efd$YZ'd%ej(fd&YZ)d'ejfd(YZ*d)S(*s
Test code for policies.
i(t	Interfacet
implementst
implementedBy(tStringIO(tunittest(tStringTransport(t StringTransportWithDisconnection(tprotocoltreactortaddresstdeferttask(tpoliciestSimpleProtocolcBs<eZdZZdZdZdZdZdZRS(itcCs"tj|_tj|_dS(N(R
tDeferredt
dConnectedt
dDisconnected(tself((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt__init__scCsd|_|jjddS(NiR(t	connectedRtcallback(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytconnectionMades	cCsd|_|jjddS(NiR(tdisconnectedRR(Rtreason((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytconnectionLost"s	cCs|j|7_dS(N(tbuffer(Rtdata((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytdataReceived&s(	t__name__t
__module__RRRRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR
s
			tSillyFactorycBseZdZdZRS(cCs
||_dS(N(tp(RR ((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR-scCs|jS(N(R (Rtaddr((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt
buildProtocol0s(RRRR"(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR+s	tEchoProtocolcBs2eZeZdZdZdZdZRS(cCs
t|_dS(N(tTruetpaused(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytpauseProducing7scCs
t|_dS(N(tFalseR%(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytresumeProducing:scCsdS(N((R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt
stopProducing=scCs|jj|dS(N(t	transporttwrite(RR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR@s(RRR'R%R&R(R)R(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR#4s
			tServercBseZdZeZRS(s8
    A simple server factory using L{EchoProtocol}.
    (RRt__doc__R#R(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR,EstTestableThrottlingFactorycBs eZdZdZdZRS(sH
    L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
    cOs#tjj|||||_dS(s
        @param clock: object providing a callLater method that can be used
            for tests.
        @type clock: C{task.Clock} or alike.
        N(RtThrottlingFactoryRtclock(RR0targstkwargs((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRRscCs|jj||S(s0
        Forward to the testable clock.
        (R0t	callLater(Rtperiodtfunc((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR3\s(RRR-RR3(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR.Ms	
tTestableTimeoutFactorycBs eZdZdZdZRS(sE
    L{policies.TimeoutFactory} using a L{task.Clock} for tests.
    cOs#tjj|||||_dS(s
        @param clock: object providing a callLater method that can be used
            for tests.
        @type clock: C{task.Clock} or alike.
        N(RtTimeoutFactoryRR0(RR0R1R2((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRiscCs|jj||S(s0
        Forward to the testable clock.
        (R0R3(RR4R5((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR3ss(RRR-RR3(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR6ds	
tWrapperTestCasecBsDeZdZdZdZdZdZdZdZRS(s>
    Tests for L{WrappingFactory} and L{ProtocolWrapper}.
    cCsPt}tj|}|jtjddd}|j|jj|dS(sf
        Make sure protocol.factory is the wrapped factory, not the wrapping
        factory.
        tTCPs	127.0.0.1i#N(	R,RtWrappingFactoryR"R	tIPv4AddresstassertIdenticaltwrappedProtocoltfactory(RtftwfR ((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_protocolFactoryAttributes	csdtfdYddfdY}ttjtj}tjtjd|}|j||j	j
|jdS(s
        The transport wrapper passed to the wrapped protocol's
        C{makeConnection} provides the same interfaces as are provided by the
        original transport.
        tIStubTransportcBseZRS((RR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRBst
StubTransportcseZeRS((RRR((RB(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRCsN((RRRtProtocolWrapperRtProtocolR:tNonetmakeConnectiont
assertTruet
providedByR*(RRCtprototwrapper((RBs>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_transportInterfacess

cCs2t}tj|}|jd|jdS(s
        L{WrappingFactory.logPrefix} is customized to mention both the original
        factory and the wrapping factory.
        sServer (WrappingFactory)N(R,RR:tassertEqualt	logPrefix(RtserverR>((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_factoryLogPrefixs	cCsHdtfdY}|}tj|}|jd|jdS(s
        If the wrapped factory doesn't have a L{logPrefix} method,
        L{WrappingFactory.logPrefix} falls back to the factory class name.
        t	NoFactorycBseZRS((RR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRQssNoFactory (WrappingFactory)N(tobjectRR:RMRN(RRQROR>((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_factoryLogPrefixFallbacks	cCsPt}tj|}|jtjddd}|jd|jdS(s{
        L{ProtocolWrapper.logPrefix} is customized to mention both the original
        protocol and the wrapper.
        R9s	127.0.0.1i#sEchoProtocol (ProtocolWrapper)N(R,RR:R"R	R;RMRN(RROR>R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_protocolLogPrefixs		cCsodtfdY}t}||_tj|}|jtjddd}|jd|j	dS(s
        If the wrapped protocol doesn't have a L{logPrefix} method,
        L{ProtocolWrapper.logPrefix} falls back to the protocol class name.
        t
NoProtocolcBseZRS((RR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRUsR9s	127.0.0.1i#sNoProtocol (ProtocolWrapper)N(
RRR,RRR:R"R	R;RMRN(RRUROR>R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_protocolLogPrefixFallbacks			(	RRR-RARLRPRSRTRV(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR8{s			
	
	
R:cBseZdZdZRS(cCs|S(N((tsR?R ((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt<lambda>scCs$tjj||jjddS(N(RR:tstartFactorytdeferredRRF(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRYs(RRRRY(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR:s	tThrottlingTestCasecBs)eZdZdZdZdZRS(s2
    Tests for L{policies.ThrottlingFactory}.
    c
smt}gtdD]}t^q\tj|dt}tj|_t	j
d|ddjjfd}fd}fd}fd	}fd
}fd}	|jj
||jj
||jj
||jj
||jj
||jj
|	|jS(sQ
        Full test using a custom server limiting number of connections.
        iiit	interfaces	127.0.0.1csXtjdtjjfdjjfdjS(Ns	127.0.0.1cstjdtS(Ns	127.0.0.1(Rt
connectTCPR(tr(tc2tn(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRXscstjdtS(Ns	127.0.0.1(RR]R(R^(tc3R`(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRXs(RR]RRtaddCallbackR(tresults(R`R_Ratc1(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt_connect123s		csjgfD]}|j^qdddgjgfD]}|j^qNdddgjtjjd|S(Niii(RMRRtlent	protocolstkeys(Rctc(ttServerRRaR_Rd(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt	_check123s88csjjjS(N(R*tloseConnectionR(Rc(Rd(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt_lose1s
cs tjdtjS(Ns	127.0.0.1(RR]RR(Rc(tc4R`(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt	_connect4scs*jjdjjd|S(Nii(RMRR(Rc(RRn(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt_check4scsLx!fD]}|jjq
WtjtjjjjgS(N(R*RlR
tDeferredListt
maybeDeferredt
stopListeningR(RcRi(R R_Rn(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt_cleanups(R,trangeR
RR/R:R
RRZRt	listenTCPtgetHosttportRb(
RROtitwrapTServerReRkRmRoRpRt((RjRRaR_RdRnR`R s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt
test_limits(	.cCs3t}ttj|dd}|jtjddd}t}||_|j	||j
|_|jd|jd|j
|jd|j
|jd	|j|j
j|jjd
|j
|jd|j|j
j|jjd
|j
|jd|j|j
jdS(se
        Check the writeLimit parameter: write data, and check for the pause
        status.
        t
writeLimiti
R9s	127.0.0.1it
0123456789t
abcdefghijt0123456789abcdefghijig?N(R,R.RtClockR"R	R;RRRGR=tproducerRRMtvaluetwrittenThisSecondtassertFalseR%R0tadvanceRH(RRORjRxttr((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_writeLimit%s$			


cCst}ttj|dd}|jtjddd}t}||_|j	||j
d|j
d|j|jd|j|j
d	|jjd
|j|j
d|j|jd|jjd
|j|j
d|j|jd|j|j
d|j
d|j|jd|j|j
d	|jjd
|j|j
d|j|jd|jjd
|j|j
d|j|jdd
S(sb
        Check the readLimit parameter: read data and check for the pause
        status.
        t	readLimiti
R9s	127.0.0.1iR}R~Rig?R%t	producingN(R,R.RRR"R	R;RRRGRRMRtreadThisSecondR0Rt
producerStatetclear(RRORjRxR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_readLimitDs6			





(RRR-R{RR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR[s	;	tTimeoutTestCasecBs2eZdZdZdZdZdZRS(s/
    Tests for L{policies.TimeoutFactory}.
    cCstj|_tj}t|_t|j|d|_|jjt	j
ddd|_t|_
|j|j
_|jj|j
dS(sq
        Create a testable, deterministic clock, and a set of
        server factory/protocol/transport.
        iR9s	127.0.0.1i90N(RRR0Rt
ServerFactoryR
R6R>R"R	R;RJRR*RG(RtwrappedFactory((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytsetUprs		cCse|jjdddddg|j|jjj|jjddg|j|jjjdS(s
        Make sure that when a TimeoutFactory accepts a connection, it will
        time out that connection if no data is read or written within the
        timeout period.
        gg?g?g?g?N(R0tpumptfailIfRJR=Rt
failUnless(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_timeoutscCs|jjdddg|j|jjj|jjd|jjdddg|j|jjj|jjdgd|jjdddg|j|jjj|jjddg|j|jjjdS(	s
        Make sure that writing data to a transport from a protocol
        constructed by a TimeoutFactory resets the timeout countdown.
        gg?g?sbytes bytes bytestbytesig@N(	R0RRRJR=RR+t
writeSequenceR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_sendAvoidsTimeoutscCs|jjdddg|j|jjj|jjd|jjdddg|j|jjj|jjdddg|j|jjjdS(sR
        Make sure that receiving data also resets the timeout countdown.
        gg?g?sbytes bytes bytesN(R0RRRJR=RRR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_receiveAvoidsTimeouts(RRR-RRRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRms
			t
TimeoutTestercBsSeZdZdZeZdZdZdZddZ
dZdZRS(	s
    A testable protocol with timeout facility.

    @ivar timedOut: set to C{True} if a timeout has been detected.
    @type timedOut: C{bool}
    icCs
||_dS(sF
        Initialize the protocol with a C{task.Clock} object.
        N(R0(RR0((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRscCs|j|jdS(s3
        Upon connection, set the timeout.
        N(t
setTimeoutttimeOut(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRscCs!|jtjj||dS(s,
        Reset the timeout on data.
        N(tresetTimeoutRRER(RR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs
cCs|jddS(sD
        On connection lost, cancel all timeout operations.
        N(RRF(RR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRscCs
t|_dS(sX
        Flags the timedOut variable to indicate the timeout of the connection.
        N(R$ttimedOut(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttimeoutConnectionscOs|jj||||S(sD
        Override callLater to use the deterministic clock.
        (R0R3(RttimeoutR5R1R2((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR3sN(
RRR-RR'RRRRRFRRR3(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs				tTestTimeoutcBsMeZdZdZdZdZdZdZdZdZ	RS(s-
    Tests for L{policies.TimeoutMixin}.
    cCs%tj|_t|j|_dS(sY
        Create a testable, deterministic clock and a C{TimeoutTester} instance.
        N(RRR0RRJ(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRscCs0|jjd|jt|jjddS(sg
        Test that the callLater of the clock is used instead of
        C{reactor.callLater}.
        i
iN(RJRRMRfR0tcalls(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_overriddenCallLaterscCso|jjt|jjddddg|j|jj|jjddg|j|jjdS(sq
        Check that the protocol does timeout at the time specified by its
        C{timeOut} attribute.
        ig?g?N(RJRGRR0RRRR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs
cCs|jjt|jjddddg|j|jj|jjd|jjddddg|j|jj|jjddg|j|jjdS(sV
        Check that receiving data is delaying the timeout of the connection.
        ig?g?shello thereN(	RJRGRR0RRRRR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_noTimeoutscCsd|j_|jjt|jjd|j|jjd|jjddg|j	|jj
|jjddg|j|jj
dS(sy
        Check that setting a new value for timeout cancel the previous value
        and install a new timeout.
        iig?g?N(RFRJRRGRRRMR0RRRR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_resetTimeout-scCsxd|j_|jjt|jjd|j|jjd|jjddddg|j	|jj
dS(sO
        Setting the timeout to C{None} cancel any timeout operations.
        iiN(RJRRGRRRFRMR0RRR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_cancelTimeout>scCsd|j_|j|jjdd|j|jjdd|j|jjdd|j|jjd|jjddS(sM
        setTimeout should return the value of the previous timeout.
        ii
iN(RJRRMRRF(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_returnLs(
RRR-RRRRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs							t$LimitTotalConnectionsFactoryTestCasecBs eZdZdZdZRS(s/Tests for policies.LimitTotalConnectionsFactorycCstj}tj|_|jd|j|jd}|jd|j|jd}|jd|j|jd|jd|j|jd|jd|jdS(Niii(	RtLimitTotalConnectionsFactoryRRERMtconnectionCountR"RFR(RR>tp1tp2((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttestConnectionCounting^s

cs6tjtj_d_jd}|jd||jdj	|jdjd|jdj	dtjffdY}|_
t_jd}|j
d|jtj|jdj	|jd|jdj	|jd|jdj	dS(NitOverflowProtocolcseZfdZRS(cs
t_dS(N(R$t
overflowed(R(R>(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs(RRR((R>(s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRsii(RRRREtconnectionLimitR"RFtassertNotEqualRMRtoverflowProtocolR'RRGR$R(RR Rtop((R>s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttestConnectionLimitingrs&			


(RRR-RR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyR\s	tWriteSequenceEchoProtocolcBseZdZRS(cCs?|jddkr+|jj|gntj||dS(Nsvector!i(tfindR*RR#R(RR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs(RRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRstTestLoggingFactorycBseZdZdZRS(cCs.|jdkstdt|_|jS(Nsopen() called too many times(topenFileRFtAssertionErrorR(Rtname((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pytopensN(RRRFRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRstLoggingFactoryTestCasecBs eZdZdZdZRS(s6
    Tests for L{policies.TrafficLoggingFactory}.
    cCsYt}t|_t}t|d}|jd
}||_|j||jj}|j	d|kd|f|j
|j|jd|jj}|j
d||j
d||j|jd|j|jd	|jj}|j
d
||j|jd	|j|jj}|j
d|dS(sQ
        Check the output produced by L{policies.TrafficLoggingFactory}.
        ttests1.2.3.4i.t*s* not found in %rshere are some bytessC 1: 'here are some bytes'sS 1: 'here are some bytes's"prepare for vector! to the extremes,SV 1: ['prepare for vector! to the extreme']tConnectionDoneN(s1.2.3.4i.(R,RRRRR"RGRtgetvalueRRRRtassertInRMRRl(RRttR?R tv((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_thingsGetLoggeds.				




cCst}t|d}|j|jd|jd	|j|jdd|_|jd
|j|jd|j|j|jddS(sG
        Test counter management with the resetCounter method.
        Ris1.2.3.4i.ii/iN(s1.2.3.4i.(s1.2.3.4i/(R,RRMt_counterR"RFRtresetCounter(RRR?((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyttest_counters	
	

(RRR-RR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyRs	#N(+R-tzope.interfaceRRRRt
twisted.trialRttwisted.test.proto_helpersRRttwisted.internetRRR	R
Rttwisted.protocolsRRER
t
ClientFactoryRR#RR,R/R.R7R6tTestCaseR8R:R[RtTimeoutMixinRRRRtTrafficLoggingFactoryRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_policies.pyt<module>s.(	a	W6b<