<s> from twisted . trial . unittest import TestCase <EOL> from twisted . internet import reactor <EOL> from twisted . internet . defer import Deferred <EOL> from txconnpool . test . test_basic import ADDRESS , StubReactor , StubConnectionPool <EOL> from txconnpool import memcache <EOL> class CallRecorder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . calls = [ ] <EOL> def call ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . calls . append ( ( args , kwargs ) ) <EOL> class MemCachePoolTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . reactor = StubReactor ( ) <EOL> self . pool = memcache . MemCachePool ( <EOL> ADDRESS , maxClients = <NUM_LIT:5> , reactor = self . reactor ) <EOL> self . call_recorder = CallRecorder ( ) <EOL> self . patch ( memcache . Pool , <EOL> '<STR_LIT>' , <EOL> self . call_recorder . call ) <EOL> def test_get ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pool . get ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , somekwarg = <NUM_LIT:1> ) <EOL> self . assertEqual ( self . call_recorder . calls , <EOL> [ ( ( '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , { '<STR_LIT>' : <NUM_LIT:1> } ) ] ) <EOL> def test_set ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pool . set ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , ttl = <NUM_LIT> ) <EOL> self . assertEqual ( self . call_recorder . calls , <EOL> [ ( ( '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , { '<STR_LIT>' : <NUM_LIT> } ) ] ) <EOL> def test_delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pool . delete ( '<STR_LIT:foo>' , somekwarg = <NUM_LIT:1> ) <EOL> self . assertEqual ( self . call_recorder . calls , <EOL> [ ( ( '<STR_LIT>' , '<STR_LIT:foo>' ) , { '<STR_LIT>' : <NUM_LIT:1> } ) ] ) <EOL> def test_add ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pool . add ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , ttl = <NUM_LIT> ) <EOL> self . assertEqual ( self . call_recorder . calls , <EOL> [ ( ( '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , { '<STR_LIT>' : <NUM_LIT> } ) ] ) <EOL> class PooledMemCacheProtocolTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . protocol = memcache . _PooledMemCacheProtocol ( ) <EOL> self . pool = StubConnectionPool ( ) <EOL> self . factory = memcache . _MemCacheClientFactory ( ) <EOL> self . factory . connectionPool = self . pool <EOL> self . factory . protocol = lambda : self . protocol <EOL> self . factory . buildProtocol ( ADDRESS ) <EOL> def test_connectionMadeCallsBase ( self ) : <EOL> """<STR_LIT>""" <EOL> cm_cr = CallRecorder ( ) <EOL> self . patch ( memcache . MemCacheProtocol , '<STR_LIT>' , cm_cr . call ) <EOL> self . protocol . connectionMade ( ) <EOL> self . assertEqual ( cm_cr . calls , [ ( ( self . protocol , ) , { } ) ] ) <EOL> def test_connectionMadeFreesClient ( self ) : <EOL> """<STR_LIT>""" <EOL> self . protocol . connectionMade ( ) <EOL> self . assertEqual ( self . pool . calls , [ ( '<STR_LIT>' , self . protocol ) ] ) <EOL> def test_connectionMadeFiresFactoryDeferred ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . factory . deferred <EOL> def _checkDeferred ( ign ) : <EOL> self . assertIdentical ( self . factory . deferred , None ) <EOL> def _checkProtocol ( proto ) : <EOL> self . assertEqual ( proto , self . protocol ) <EOL> d2 = Deferred ( ) <EOL> d2 . addCallback ( _checkDeferred ) <EOL> reactor . callLater ( <NUM_LIT:0> , d2 . callback , None ) <EOL> return d2 <EOL> d . addCallback ( _checkProtocol ) <EOL> self . protocol . connectionMade ( ) <EOL> return d <EOL> def test_connectionMadeDoesntFireDeferredMultipleTimes ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . factory . deferred <EOL> def _checkDeferred ( ign ) : <EOL> self . assertIdentical ( self . factory . deferred , None ) <EOL> self . protocol . connectionMade ( ) <EOL> def _checkProtocol ( proto ) : <EOL> self . assertEqual ( proto , self . protocol ) <EOL> d2 = Deferred ( ) <EOL> d2 . addCallback ( _checkDeferred ) <EOL> reactor . callLater ( <NUM_LIT:0> , d2 . callback , None ) <EOL> return d2 <EOL> d . addCallback ( _checkProtocol ) <EOL> self . protocol . connectionMade ( ) <EOL> return d </s>
