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_memcache.pyc


=Nc@sdZddlmZddlmZmZddlmZmZddlm	Z	ddl
mZddlm
Z
ddlmZmZmZdd	lmZd
fdYZdee	fd
YZdee	fdYZdS(s$
Test the memcache client protocol.
i(tConnectionDone(tMemCacheProtocolt
NoSuchCommand(tClientErrortServerError(tTestCase(t StringTransportWithDisconnection(tClock(tDeferredt
gatherResultstTimeoutError(tDeferredListtCommandMixincBseZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZdZdZdZdZdZdZdZRS(sO
    Setup and tests for basic invocation of L{MemCacheProtocol} commands.
    cCs
tdS(sp
        Helper test method to test the resulting C{Deferred} of a
        L{MemCacheProtocol} command.
        N(tNotImplementedError(tselftdtsendtrecvtresult((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt_testscCs"|j|jjddddS(s
        L{MemCacheProtocol.get} returns a L{Deferred} which is called back with
        the value and the flag associated with the given key if the server
        returns a successful result.
        tfoos	get foo
sVALUE foo 0 3
bar
END
itbar(iR(Rtprototget(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_get"scCs"|j|jjddddS(su
        Test getting a non-available key: it succeeds but return C{None} as
        value and C{0} as flag.
        Rs	get foo
sEND
iN(iN(RRRtNone(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt
test_emptyGet,scCs6|j|jjddgddidd6d	d6S(
s
        L{MemCacheProtocol.getMultiple} returns a L{Deferred} which is called
        back with a dictionary of flag, value for each given key.
        Rtcows
get foo cow
s1VALUE foo 0 3
bar
VALUE cow 0 7
chicken
END
itchickenR(iR(isbar(RRtgetMultiple(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_getMultiple5scCs6|j|jjddgddidd6d
d6S(s
        When L{MemCacheProtocol.getMultiple} is called with non-available keys,
        the corresponding tuples are (0, None).
        RRs
get foo cow
sVALUE cow 1 3
bar
END
iRi(isbarN(iN(RRRR(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_getMultipleWithEmpty@scCs%|j|jjddddtS(s
        L{MemCacheProtocol.set} returns a L{Deferred} which is called back with
        C{True} when the operation succeeds.
        RRsset foo 0 0 3
bar
sSTORED
(RRtsettTrue(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_setKscCs%|j|jjddddtS(s
        L{MemCacheProtocol.add} returns a L{Deferred} which is called back with
        C{True} when the operation succeeds.
        RRsadd foo 0 0 3
bar
sSTORED
(RRtaddR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_addTscCs%|j|jjddddtS(s
        L{MemCacheProtocol.replace} returns a L{Deferred} which is called back
        with C{True} when the operation succeeds.
        RRsreplace foo 0 0 3
bar
sSTORED
(RRtreplaceR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_replace]scCs%|j|jjddddtS(s
        Test an erroneous add: if a L{MemCacheProtocol.add} is called but the
        key already exists on the server, it returns a B{NOT STORED} answer,
        which calls back the resulting L{Deferred} with C{False}.
        RRsadd foo 0 0 3
bar
sNOT STORED
(RRR#tFalse(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt
test_errorAddfscCs%|j|jjddddtS(s
        Test an erroneous replace: if a L{MemCacheProtocol.replace} is called
        but the key doesn't exist on the server, it returns a B{NOT STORED}
        answer, which calls back the resulting L{Deferred} with C{False}.
        RRsreplace foo 0 0 3
bar
sNOT STORED
(RRR%R'(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_errorReplacepscCs"|j|jjdddtS(s
        L{MemCacheProtocol.delete} returns a L{Deferred} which is called back
        with C{True} when the server notifies a success.
        Rsdelete bar
s	DELETED
(RRtdeleteR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_deletezscCs"|j|jjdddtS(s
        Test a error during a delete: if key doesn't exist on the server, it
        returns a B{NOT FOUND} answer which calls back the resulting L{Deferred}
        with C{False}.
        Rsdelete bar
sNOT FOUND
(RRR*R'(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_errorDeletescCs"|j|jjddddS(s
        Test incrementing a variable: L{MemCacheProtocol.increment} returns a
        L{Deferred} which is called back with the incremented value of the
        given key.
        Rsincr foo 1
s4
i(RRt	increment(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_incrementscCs"|j|jjddddS(s
        Test decrementing a variable: L{MemCacheProtocol.decrement} returns a
        L{Deferred} which is called back with the decremented value of the
        given key.
        Rsdecr foo 1
s5
i(RRt	decrement(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_decrementscCs%|j|jjdddddS(s
        L{MemCacheProtocol.increment} takes an optional argument C{value} which
        replaces the default value of 1 when specified.
        Risincr foo 8
s4
i(RRR-(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_incrementValscCs%|j|jjdddddS(s
        L{MemCacheProtocol.decrement} takes an optional argument C{value} which
        replaces the default value of 1 when specified.
        Risdecr foo 3
s5
i(RRR/(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_decrementValscCs-|j|jjddidd6dd6S(s
        Test retrieving server statistics via the L{MemCacheProtocol.stats}
        command: it parses the data sent by the server and calls back the
        resulting L{Deferred} with a dictionary of the received statistics.
        sstats
s"STAT foo bar
STAT egg spam
END
RRtspamtegg(RRtstats(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt
test_statsscCs0|j|jjdddidd6dd6S(s7
        L{MemCacheProtocol.stats} takes an optional C{str} argument which,
        if specified, is sent along with the I{STAT} command.  The I{STAT}
        responses from the server are parsed as key/value pairs and returned
        as a C{dict} (as in the case where the argument is not specified).
        tblahsstats blah
s"STAT foo bar
STAT egg spam
END
RRR3R4(RRR5(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_statsWithArgumentscCs|j|jjdddS(s
        Test version retrieval via the L{MemCacheProtocol.version} command: it
        returns a L{Deferred} which is called back with the version sent by the
        server.
        s	version
s
VERSION 1.1
s1.1(RRtversion(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_versionscCs|j|jjddtS(s
        L{MemCacheProtocol.flushAll} returns a L{Deferred} which is called back
        with C{True} if the server acknowledges success.
        sflush_all
sOK
(RRtflushAllR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt
test_flushAlls(t__name__t
__module__t__doc__RRRRRR"R$R&R(R)R+R,R.R0R1R2R6R8R:R<(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRs*		
											
	
			
	
	
							
tMemCacheTestCasecBs
eZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZRS(s9
    Test client protocol class L{MemCacheProtocol}.
    cCs\t|_t|_|jj|j_t|_|j|j_|jj|jdS(s{
        Create a memcache client, connect it to a string protocol, and make it
        use a deterministic clock.
        N(	RRRtclockt	callLaterRt	transporttprotocoltmakeConnection(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pytsetUpscsLfd}jjj||j|jj||S(s
        Implementation of C{_test} which checks that the command sends C{send}
        data, and that upon reception of C{recv} the result is C{result}.

        @param d: the resulting deferred from the memcache command.
        @type d: C{Deferred}

        @param send: the expected data to be sent.
        @type send: C{str}

        @param recv: the data to simulate as reception.
        @type recv: C{str}

        @param result: the expected result.
        @type result: C{any}
        csj|dS(N(tassertEqual(tres(RR(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pytcbs(RGRCtvaluetaddCallbackRtdataReceived(RRRRRRI((RRs>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRs

cCsC|jjdd}|jt|jjdt||fdS(s
        If the value returned doesn't match the expected key of the current
        C{get} command, an error is raised in L{MemCacheProtocol.dataReceived}.
        RtspameggsVALUE bar 0 %s
%s
END
N(RRtassertRaisestRuntimeErrorRLtlen(Rts((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_invalidGetResponse	s
		cCsI|jjddgd}|jt|jjdt||fdS(s
        If the value returned doesn't match one the expected keys of the
        current multiple C{get} command, an error is raised error in
        L{MemCacheProtocol.dataReceived}.
        RRRMsVALUE egg 0 %s
%s
END
N(RRRNRORLRP(RRQ((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_invalidMultipleGetResponses
		csjjd}jjd}t}|jj_jjjjj|t	j|t	fd}|j
|t|||gS(s
        Test the timeout on outgoing requests: when timeout is detected, all
        current commands fail with a L{TimeoutError}, and the connection is
        closed.
        RRcsjt|ddS(NsConnection timeout(RGtstr(terror(R(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pytcheckMessage0s(RRRtcallbacktconnectionLostRAtadvancetpersistentTimeOutt
assertFailureR
RKR	(Rtd1td2td3RV((Rs>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeOut"s	
cs\jjd}jjjjdjjdfd}|j||S(sY
        When a request gets a response, no pending timeout call remains around.
        RisVALUE foo 0 3
bar
END
cs0j|djtjjddS(NiR(isbar(RGRPRAtcalls(R(R(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pytcheck?s(RRRARYRZRLRK(RRRa((Rs>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeoutRemoved6s
cCsp|jjd}t}|j|j_|jjd|jj|jj|j	|t
t||gS(s
        Test the timeout when raw mode was started: the timeout is not reset
        until all the data has been received, so we can have a L{TimeoutError}
        when waiting for raw data.
        RsVALUE foo 0 10
12345(RRRRWRXRLRARYRZR[R
R	(RR\R]((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeOutRawFs	cCsm|jj}t}|j|j_|jjd|jj|jj|j	|t
t||gS(s
        Test the timeout when stat command has started: the timeout is not
        reset until the final B{END} is received.
        sSTAT foo bar
(RR5RRWRXRLRARYRZR[R
R	(RR\R]((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeOutStatVs	csjjd}jjdt}|jj_jjjjdjjdfd}fd|j	||S(s
        When two requests are sent, a timeout call remains around for the
        second request, and its timeout time is correct.
        RRisVALUE foo 0 3
bar
END
csrj|djtjjdx*tjjD]}jjdq?Wjt	j
S(NiRi(isbar(RGRPRAR`trangeRRZRYR[R
RK(Rti(t	checkTimeRR](s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRars
cs+jjjdjjddS(Nii(RGRAtsecondsRRZ(tignored(R(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRgxs(
RRRRWRXRARYRZRLRK(RR\R^Ra((RgRR]s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeoutPipelininges	
cCs|jjd}t}|j|j_|jj|jjd|jjd}|jjd|j|t	|j|t	t
|||gS(s
        Check that timeout is not resetted for every command, but keep the
        timeout from the first command without response.
        RiR(RRRRWRXRARYRZR[R
R	(RR\R^R]((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeoutNotResets	cCsj|jjd}|jj|jj|j|t|jjd}|j|tt||gS(s
        C{timeoutConnection} cleans the list of commands that it fires with
        C{TimeoutError}: C{connectionLost} doesn't try to fire them again, but
        sets the disconnected state so that future commands fail with a
        C{RuntimeError}.
        RR(	RRRARYRZR[R
ROR	(RR\R]((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_timeoutCleanDeferredsscsejjd}jjd}jjt||gdt}fd}|j|S(sl
        When disconnection occurs while commands are still outstanding, the
        commands fail.
        RRt
consumeErrorscs5x.|D]&\}}j||jtqWdS(N(tassertFalsettrapR(tresultstsuccessR(R(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt
checkFailuress
(RRRCtloseConnectionRR!RK(RR\R]tdoneRr((Rs>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_connectionLosts
cCs|j|jjdddt}|j|jjddt}|j|jjddt}|j|jjdddt}|j|jjdddt}|j|jjdddgt}t	||||||gS(s
        An error is raised when trying to use a too long key: the called
        command returns a L{Deferred} which fails with a L{ClientError}.
        taiRR(
R[RR RR-RtappendtprependRR	(RR\R]R^td4td5td6((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_tooLongKeys%"""cCs^|jjdddddd}|j|jjd|j|t|jjd|S(s
        When an unknown command is sent directly (not through public API), the
        server answers with an B{ERROR} token, and the command fails with
        L{NoSuchCommand}.
        R4RRitsegg foo 0 0 3
bar
sERROR
(Rt_setRGRCRJR[RRL(RR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_invalidCommands
!cstd}jjd|}jjjdj|tfd}|j|jjd|S(s
        Test the L{ClientError} error: when the server sends a B{CLIENT_ERROR}
        token, the originating command fails with L{ClientError}, and the error
        contains the text sent by the server.
        teggspammRsset foo 0 0 8
eggspamm
csjt|ddS(NsWe don't like egg and spam(RGRT(terr(R(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRass)CLIENT_ERROR We don't like egg and spam
(	RR RGRCRJR[RRKRL(RRvRRa((Rs>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_clientErrors
cstd}jjd|}jjjdj|tfd}|j|jjd|S(s
        Test the L{ServerError} error: when the server sends a B{SERVER_ERROR}
        token, the originating command fails with L{ServerError}, and the error
        contains the text sent by the server.
        RRsset foo 0 0 8
eggspamm
csjt|ddS(Ntzomg(RGRT(R(R(s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRassSERVER_ERROR zomg
(	RR RGRCRJR[RRKRL(RRvRRa((Rs>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_serverErrors
cCs|j|jjddt}|j|jjdt}|j|jjdt}|j|jjdt}|j|jjddt}|j|jjddt}|j|jj	ddgt}t
|||||||gS(sQ
        Using a non-string key as argument to commands raises an error.
        ufooRueggiubarR4(R[RR RR-RR*RwRxRR	(RR\R]R^RyRzR{td7((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_unicodeKeys!!!cCs|j|jjddtS(s;
        Using a non-string value raises an error.
        Rubar(R[RR R(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_unicodeValuescCs|jjd}|j|jd	|jjdd}|j|jt|jjd}|j|jd
|j|jjd|jjdt	|||gS(s
        Multiple requests can be sent subsequently to the server, and the
        protocol orders the responses correctly and dispatch to the
        corresponding client command.
        RiRtspamspamspamR4R3s0get foo
set bar 0 0 12
spamspamspam
get egg
s;VALUE foo 0 3
bar
END
STORED
VALUE egg 0 4
spam
END
(isbar(isspam(
RRRKRGR R!RCRJRLR	(RR\R]R^((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_pipelining	scCs|jjd}|j|jd	|j|jjd|jjd|jjd|jjd|jjd|S(
s
        If the value retrieved by a C{get} arrive in chunks, the protocol
        is able to reconstruct it and to produce the good value.
        Rit
0123456789s	get foo
sVALUE foo 0 10
0123456t789s
ENDs
(iR(RRRKRGRCRJRL(RR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_getInChunksscCs%|j|jjddddtS(s
        L{MemCacheProtocol.append} behaves like a L{MemCacheProtocol.set}
        method: it returns a L{Deferred} which is called back with C{True} when
        the operation succeeds.
        RRsappend foo 0 0 3
bar
sSTORED
(RRRwR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_append,scCs%|j|jjddddtS(s
        L{MemCacheProtocol.prepend} behaves like a L{MemCacheProtocol.set}
        method: it returns a L{Deferred} which is called back with C{True} when
        the operation succeeds.
        RRsprepend foo 0 0 3
bar
sSTORED
(RRRxR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_prepend6scCs%|j|jjdtdddS(s
        L{MemCacheProtocol.get} handles an additional cas result when
        C{withIdentifier} is C{True} and forward it in the resulting
        L{Deferred}.
        Rs
gets foo
sVALUE foo 0 3 1234
bar
END
it1234R(iRsbar(RRRR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt	test_gets@scCs%|j|jjdtdddS(s
        Test getting a non-available key with gets: it succeeds but return
        C{None} as value, C{0} as flag and an empty cas value.
        Rs
gets foo
sEND
iR}N(iR}N(RRRR!R(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_emptyGetsJscCs9|j|jjddgtddid
d6dd6S(s
        L{MemCacheProtocol.getMultiple} handles an additional cas field in the
        returned tuples if C{withIdentifier} is C{True}.
        RRsgets foo bar
s8VALUE foo 0 3 1234
egg
VALUE bar 0 4 2345
spam
END
it2345R3RR4(iRsspam(is1234segg(RRRR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_getsMultipleSscCs9|j|jjddgtddid
d6dd6S(s
        When getting a non-available key with L{MemCacheProtocol.getMultiple}
        when C{withIdentifier} is C{True}, the other keys are retrieved
        correctly, and the non-available key gets a tuple of C{0} as flag,
        C{None} as value, and an empty cas value.
        RRsgets foo bar
sVALUE foo 0 3 1234
egg
END
iR}RR4N(iR}N(is1234segg(RRRR!R(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_getsMultipleWithEmpty^scCs+|j|jjddddddtS(s
        L{MemCacheProtocol.checkAndSet} passes an additional cas identifier
        that the server handles to check if the data has to be updated.
        RRtcasRscas foo 0 0 3 1234
bar
sSTORED
(RRtcheckAndSetR!(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_checkAndSetkscCs+|j|jjddddddtS(s
        When L{MemCacheProtocol.checkAndSet} response is C{EXISTS}, the
        resulting L{Deferred} fires with C{False}.
        RRRRscas foo 0 0 3 1234
bar
sEXISTS
(RRRR'(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyttest_casUnknowKeyts(R=R>R?RFRRRRSR_RbRcRdRjRkRlRuR|RRRRRRRRRRRRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyR@s:	
			
										
							
	
	
				
		tCommandFailureTestscBs eZdZdZdZRS(sZ
    Tests for correct failure of commands on a disconnected
    L{MemCacheProtocol}.
    cCsit|_t|_|jj|j_t|_|j|j_|jj|j|jj	dS(sU
        Create a disconnected memcache client, using a deterministic clock.
        N(
RRRRARBRRCRDRERs(R((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRFscCs|j|tS(s
        Implementation of C{_test} which checks that the command fails with
        C{RuntimeError} because the transport is disconnected. All the
        parameters except C{d} are ignored.
        (R[RO(RRRRR((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyRs(R=R>R?RFR(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyR~s	
N(R?ttwisted.internet.errorRttwisted.protocols.memcacheRRRRttwisted.trial.unittestRttwisted.test.proto_helpersRttwisted.internet.taskRttwisted.internet.deferRR	R
RRR@R(((s>/usr/lib/python2.7/dist-packages/twisted/test/test_memcache.pyt<module>s