<s> """<STR_LIT>""" <EOL> import sys , os , time , gc <EOL> from cStringIO import StringIO <EOL> from zope . interface import implements , Interface <EOL> from twisted . python . versions import Version <EOL> from twisted . trial import unittest <EOL> from twisted . spread import pb , util , publish , jelly <EOL> from twisted . internet import protocol , main , reactor <EOL> from twisted . internet . error import ConnectionRefusedError <EOL> from twisted . internet . defer import Deferred , gatherResults , succeed <EOL> from twisted . protocols . policies import WrappingFactory <EOL> from twisted . python import failure , log <EOL> from twisted . cred . error import UnauthorizedLogin , UnhandledCredentials <EOL> from twisted . cred import portal , checkers , credentials <EOL> class Dummy ( pb . Viewable ) : <EOL> def view_doNothing ( self , user ) : <EOL> if isinstance ( user , DummyPerspective ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> class DummyPerspective ( pb . Avatar ) : <EOL> """<STR_LIT>""" <EOL> def perspective_getDummyViewPoint ( self ) : <EOL> return Dummy ( ) <EOL> class DummyRealm ( object ) : <EOL> implements ( portal . IRealm ) <EOL> def requestAvatar ( self , avatarId , mind , * interfaces ) : <EOL> for iface in interfaces : <EOL> if iface is pb . IPerspective : <EOL> return iface , DummyPerspective ( avatarId ) , lambda : None <EOL> class IOPump : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , server , clientIO , serverIO ) : <EOL> self . client = client <EOL> self . server = server <EOL> self . clientIO = clientIO <EOL> self . serverIO = serverIO <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> timeout = time . time ( ) + <NUM_LIT:5> <EOL> while self . pump ( ) : <EOL> if time . time ( ) > timeout : <EOL> return <EOL> def pump ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clientIO . seek ( <NUM_LIT:0> ) <EOL> self . serverIO . seek ( <NUM_LIT:0> ) <EOL> cData = self . clientIO . read ( ) <EOL> sData = self . serverIO . read ( ) <EOL> self . clientIO . seek ( <NUM_LIT:0> ) <EOL> self . serverIO . seek ( <NUM_LIT:0> ) <EOL> self . clientIO . truncate ( ) <EOL> self . serverIO . truncate ( ) <EOL> self . client . transport . _checkProducer ( ) <EOL> self . server . transport . _checkProducer ( ) <EOL> for byte in cData : <EOL> self . server . dataReceived ( byte ) <EOL> for byte in sData : <EOL> self . client . dataReceived ( byte ) <EOL> if cData or sData : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def connectedServerAndClient ( ) : <EOL> """<STR_LIT>""" <EOL> clientBroker = pb . Broker ( ) <EOL> checker = checkers . InMemoryUsernamePasswordDatabaseDontUse ( guest = '<STR_LIT>' ) <EOL> factory = pb . PBServerFactory ( portal . Portal ( DummyRealm ( ) , [ checker ] ) ) <EOL> serverBroker = factory . buildProtocol ( ( '<STR_LIT:127.0.0.1>' , ) ) <EOL> clientTransport = StringIO ( ) <EOL> serverTransport = StringIO ( ) <EOL> clientBroker . makeConnection ( protocol . FileWrapper ( clientTransport ) ) <EOL> serverBroker . makeConnection ( protocol . FileWrapper ( serverTransport ) ) <EOL> pump = IOPump ( clientBroker , serverBroker , clientTransport , serverTransport ) <EOL> pump . flush ( ) <EOL> return clientBroker , serverBroker , pump <EOL> class SimpleRemote ( pb . Referenceable ) : <EOL> def remote_thunk ( self , arg ) : <EOL> self . arg = arg <EOL> return arg + <NUM_LIT:1> <EOL> def remote_knuth ( self , arg ) : <EOL> raise Exception ( ) <EOL> class NestedRemote ( pb . Referenceable ) : <EOL> def remote_getSimple ( self ) : <EOL> return SimpleRemote ( ) <EOL> class SimpleCopy ( pb . Copyable ) : <EOL> def __init__ ( self ) : <EOL> self . x = <NUM_LIT:1> <EOL> self . y = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . z = [ '<STR_LIT:test>' ] <EOL> class SimpleLocalCopy ( pb . RemoteCopy ) : <EOL> pass <EOL> pb . setUnjellyableForClass ( SimpleCopy , SimpleLocalCopy ) <EOL> class SimpleFactoryCopy ( pb . Copyable ) : <EOL> """<STR_LIT>""" <EOL> allIDs = { } <EOL> def __init__ ( self , id ) : <EOL> self . id = id <EOL> SimpleFactoryCopy . allIDs [ id ] = self <EOL> def createFactoryCopy ( state ) : <EOL> """<STR_LIT>""" <EOL> stateId = state . get ( "<STR_LIT:id>" , None ) <EOL> if stateId is None : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( repr ( state ) , ) ) <EOL> if not stateId in SimpleFactoryCopy . allIDs : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( SimpleFactoryCopy . allIDs , ) ) <EOL> inst = SimpleFactoryCopy . allIDs [ stateId ] <EOL> if not inst : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return inst <EOL> pb . setUnjellyableFactoryForClass ( SimpleFactoryCopy , createFactoryCopy ) <EOL> class NestedCopy ( pb . Referenceable ) : <EOL> def remote_getCopy ( self ) : <EOL> return SimpleCopy ( ) <EOL> def remote_getFactory ( self , value ) : <EOL> return SimpleFactoryCopy ( value ) <EOL> class SimpleCache ( pb . Cacheable ) : <EOL> def __init___ ( self ) : <EOL> self . x = <NUM_LIT:1> <EOL> self . y = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . z = [ '<STR_LIT:test>' ] <EOL> class NestedComplicatedCache ( pb . Referenceable ) : <EOL> def __init__ ( self ) : <EOL> self . c = VeryVeryComplicatedCacheable ( ) <EOL> def remote_getCache ( self ) : <EOL> return self . c <EOL> class VeryVeryComplicatedCacheable ( pb . Cacheable ) : <EOL> def __init__ ( self ) : <EOL> self . x = <NUM_LIT:1> <EOL> self . y = <NUM_LIT:2> <EOL> self . foo = <NUM_LIT:3> <EOL> def setFoo4 ( self ) : <EOL> self . foo = <NUM_LIT:4> <EOL> self . observer . callRemote ( '<STR_LIT:foo>' , <NUM_LIT:4> ) <EOL> def getStateToCacheAndObserveFor ( self , perspective , observer ) : <EOL> self . observer = observer <EOL> return { "<STR_LIT:x>" : self . x , <EOL> "<STR_LIT:y>" : self . y , <EOL> "<STR_LIT:foo>" : self . foo } <EOL> def stoppedObserving ( self , perspective , observer ) : <EOL> log . msg ( "<STR_LIT>" ) <EOL> observer . callRemote ( "<STR_LIT:end>" ) <EOL> if observer == self . observer : <EOL> self . observer = None <EOL> class RatherBaroqueCache ( pb . RemoteCache ) : <EOL> def observe_foo ( self , newFoo ) : <EOL> self . foo = newFoo <EOL> def observe_end ( self ) : <EOL> log . msg ( "<STR_LIT>" ) <EOL> pb . setUnjellyableForClass ( VeryVeryComplicatedCacheable , RatherBaroqueCache ) <EOL> class SimpleLocalCache ( pb . RemoteCache ) : <EOL> def setCopyableState ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> def checkMethod ( self ) : <EOL> return self . check <EOL> def checkSelf ( self ) : <EOL> return self <EOL> def check ( self ) : <EOL> return <NUM_LIT:1> <EOL> pb . setUnjellyableForClass ( SimpleCache , SimpleLocalCache ) <EOL> class NestedCache ( pb . Referenceable ) : <EOL> def __init__ ( self ) : <EOL> self . x = SimpleCache ( ) <EOL> def remote_getCache ( self ) : <EOL> return [ self . x , self . x ] <EOL> def remote_putCache ( self , cache ) : <EOL> return ( self . x is cache ) <EOL> class Observable ( pb . Referenceable ) : <EOL> def __init__ ( self ) : <EOL> self . observers = [ ] <EOL> def remote_observe ( self , obs ) : <EOL> self . observers . append ( obs ) <EOL> def remote_unobserve ( self , obs ) : <EOL> self . observers . remove ( obs ) <EOL> def notify ( self , obj ) : <EOL> for observer in self . observers : <EOL> observer . callRemote ( '<STR_LIT>' , self , obj ) <EOL> class DeferredRemote ( pb . Referenceable ) : <EOL> def __init__ ( self ) : <EOL> self . run = <NUM_LIT:0> <EOL> def runMe ( self , arg ) : <EOL> self . run = arg <EOL> return arg + <NUM_LIT:1> <EOL> def dontRunMe ( self , arg ) : <EOL> assert <NUM_LIT:0> , "<STR_LIT>" <EOL> def remote_doItLater ( self ) : <EOL> """<STR_LIT>""" <EOL> d = Deferred ( ) <EOL> d . addCallbacks ( self . runMe , self . dontRunMe ) <EOL> self . d = d <EOL> return d <EOL> class Observer ( pb . Referenceable ) : <EOL> notified = <NUM_LIT:0> <EOL> obj = None <EOL> def remote_notify ( self , other , obj ) : <EOL> self . obj = obj <EOL> self . notified = self . notified + <NUM_LIT:1> <EOL> other . callRemote ( '<STR_LIT>' , self ) <EOL> class NewStyleCopy ( pb . Copyable , pb . RemoteCopy , object ) : <EOL> def __init__ ( self , s ) : <EOL> self . s = s <EOL> pb . setUnjellyableForClass ( NewStyleCopy , NewStyleCopy ) <EOL> class NewStyleCopy2 ( pb . Copyable , pb . RemoteCopy , object ) : <EOL> allocated = <NUM_LIT:0> <EOL> initialized = <NUM_LIT:0> <EOL> value = <NUM_LIT:1> <EOL> def __new__ ( self ) : <EOL> NewStyleCopy2 . allocated += <NUM_LIT:1> <EOL> inst = object . __new__ ( self ) <EOL> inst . value = <NUM_LIT:2> <EOL> return inst <EOL> def __init__ ( self ) : <EOL> NewStyleCopy2 . initialized += <NUM_LIT:1> <EOL> pb . setUnjellyableForClass ( NewStyleCopy2 , NewStyleCopy2 ) <EOL> class NewStyleCacheCopy ( pb . Cacheable , pb . RemoteCache , object ) : <EOL> def getStateToCacheAndObserveFor ( self , perspective , observer ) : <EOL> return self . __dict__ <EOL> pb . setUnjellyableForClass ( NewStyleCacheCopy , NewStyleCacheCopy ) <EOL> class Echoer ( pb . Root ) : <EOL> def remote_echo ( self , st ) : <EOL> return st <EOL> class CachedReturner ( pb . Root ) : <EOL> def __init__ ( self , cache ) : <EOL> self . cache = cache <EOL> def remote_giveMeCache ( self , st ) : <EOL> return self . cache <EOL> class NewStyleTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . serverFactory = pb . PBServerFactory ( Echoer ( ) ) <EOL> self . wrapper = WrappingFactory ( self . serverFactory ) <EOL> self . server = reactor . listenTCP ( <NUM_LIT:0> , self . wrapper ) <EOL> clientFactory = pb . PBClientFactory ( ) <EOL> reactor . connectTCP ( "<STR_LIT:localhost>" , self . server . getHost ( ) . port , <EOL> clientFactory ) <EOL> def gotRoot ( ref ) : <EOL> self . ref = ref <EOL> return clientFactory . getRootObject ( ) . addCallback ( gotRoot ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> NewStyleCopy2 . allocated = <NUM_LIT:0> <EOL> NewStyleCopy2 . initialized = <NUM_LIT:0> <EOL> NewStyleCopy2 . value = <NUM_LIT:1> <EOL> self . ref . broker . transport . loseConnection ( ) <EOL> for proto in self . wrapper . protocols : <EOL> proto . transport . loseConnection ( ) <EOL> return self . server . stopListening ( ) <EOL> def test_newStyle ( self ) : <EOL> """<STR_LIT>""" <EOL> orig = NewStyleCopy ( "<STR_LIT:value>" ) <EOL> d = self . ref . callRemote ( "<STR_LIT>" , orig ) <EOL> def cb ( res ) : <EOL> self . failUnless ( isinstance ( res , NewStyleCopy ) ) <EOL> self . failUnlessEqual ( res . s , "<STR_LIT:value>" ) <EOL> self . failIf ( res is orig ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> def test_alloc ( self ) : <EOL> """<STR_LIT>""" <EOL> orig = NewStyleCopy2 ( ) <EOL> self . failUnlessEqual ( NewStyleCopy2 . allocated , <NUM_LIT:1> ) <EOL> self . failUnlessEqual ( NewStyleCopy2 . initialized , <NUM_LIT:1> ) <EOL> d = self . ref . callRemote ( "<STR_LIT>" , orig ) <EOL> def cb ( res ) : <EOL> self . failUnless ( isinstance ( res , NewStyleCopy2 ) ) <EOL> self . failUnlessEqual ( res . value , <NUM_LIT:2> ) <EOL> self . failUnlessEqual ( NewStyleCopy2 . allocated , <NUM_LIT:3> ) <EOL> self . failUnlessEqual ( NewStyleCopy2 . initialized , <NUM_LIT:1> ) <EOL> self . failIf ( res is orig ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> class ConnectionNotifyServerFactory ( pb . PBServerFactory ) : <EOL> """<STR_LIT>""" <EOL> protocolInstance = None <EOL> def __init__ ( self , root ) : <EOL> """<STR_LIT>""" <EOL> pb . PBServerFactory . __init__ ( self , root ) <EOL> self . connectionMade = Deferred ( ) <EOL> def clientConnectionMade ( self , protocol ) : <EOL> """<STR_LIT>""" <EOL> self . protocolInstance = protocol <EOL> d , self . connectionMade = self . connectionMade , None <EOL> if d is not None : <EOL> d . callback ( None ) <EOL> class NewStyleCachedTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . orig = NewStyleCacheCopy ( ) <EOL> self . orig . s = "<STR_LIT:value>" <EOL> self . server = reactor . listenTCP ( <NUM_LIT:0> , <EOL> ConnectionNotifyServerFactory ( CachedReturner ( self . orig ) ) ) <EOL> clientFactory = pb . PBClientFactory ( ) <EOL> reactor . connectTCP ( "<STR_LIT:localhost>" , self . server . getHost ( ) . port , <EOL> clientFactory ) <EOL> def gotRoot ( ref ) : <EOL> self . ref = ref <EOL> d1 = clientFactory . getRootObject ( ) . addCallback ( gotRoot ) <EOL> d2 = self . server . factory . connectionMade <EOL> return gatherResults ( [ d1 , d2 ] ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . server . factory . protocolInstance . transport . loseConnection ( ) <EOL> self . ref . broker . transport . loseConnection ( ) <EOL> return self . server . stopListening ( ) <EOL> def test_newStyleCache ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . ref . callRemote ( "<STR_LIT>" , self . orig ) <EOL> def cb ( res ) : <EOL> self . failUnless ( isinstance ( res , NewStyleCacheCopy ) ) <EOL> self . failUnlessEqual ( res . s , "<STR_LIT:value>" ) <EOL> self . failIf ( res is self . orig ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> class BrokerTestCase ( unittest . TestCase ) : <EOL> thunkResult = None <EOL> def tearDown ( self ) : <EOL> try : <EOL> os . unlink ( '<STR_LIT>' ) <EOL> except OSError : <EOL> pass <EOL> def thunkErrorBad ( self , error ) : <EOL> self . fail ( "<STR_LIT>" % ( error , ) ) <EOL> def thunkResultGood ( self , result ) : <EOL> self . thunkResult = result <EOL> def thunkErrorGood ( self , tb ) : <EOL> pass <EOL> def thunkResultBad ( self , result ) : <EOL> self . fail ( "<STR_LIT>" % ( result , ) ) <EOL> def test_reference ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> class X ( pb . Referenceable ) : <EOL> def remote_catch ( self , arg ) : <EOL> self . caught = arg <EOL> class Y ( pb . Referenceable ) : <EOL> def remote_throw ( self , a , b ) : <EOL> a . callRemote ( '<STR_LIT>' , b ) <EOL> s . setNameForLocal ( "<STR_LIT:y>" , Y ( ) ) <EOL> y = c . remoteForName ( "<STR_LIT:y>" ) <EOL> x = X ( ) <EOL> z = X ( ) <EOL> y . callRemote ( '<STR_LIT>' , x , z ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> self . assertIdentical ( x . caught , z , "<STR_LIT>" ) <EOL> self . assertEquals ( y . remoteMethod ( '<STR_LIT>' ) , y . remoteMethod ( '<STR_LIT>' ) ) <EOL> def test_result ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> for x , y in ( c , s ) , ( s , c ) : <EOL> foo = SimpleRemote ( ) <EOL> x . setNameForLocal ( "<STR_LIT:foo>" , foo ) <EOL> bar = y . remoteForName ( "<STR_LIT:foo>" ) <EOL> self . expectedThunkResult = <NUM_LIT:8> <EOL> bar . callRemote ( '<STR_LIT>' , self . expectedThunkResult - <NUM_LIT:1> <EOL> ) . addCallbacks ( self . thunkResultGood , self . thunkErrorBad ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> self . assertEquals ( self . thunkResult , self . expectedThunkResult , <EOL> "<STR_LIT>" ) <EOL> def refcountResult ( self , result ) : <EOL> self . nestedRemote = result <EOL> def test_tooManyRefs ( self ) : <EOL> l = [ ] <EOL> e = [ ] <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> foo = NestedRemote ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , foo ) <EOL> x = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> for igno in xrange ( pb . MAX_BROKER_REFS + <NUM_LIT:10> ) : <EOL> if s . transport . closed or c . transport . closed : <EOL> break <EOL> x . callRemote ( "<STR_LIT>" ) . addCallbacks ( l . append , e . append ) <EOL> pump . pump ( ) <EOL> expected = ( pb . MAX_BROKER_REFS - <NUM_LIT:1> ) <EOL> self . assertTrue ( s . transport . closed , "<STR_LIT>" ) <EOL> self . assertEquals ( len ( l ) , expected , <EOL> "<STR_LIT>" % ( expected , len ( l ) ) ) <EOL> def test_copy ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> foo = NestedCopy ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , foo ) <EOL> x = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> x . callRemote ( '<STR_LIT>' <EOL> ) . addCallbacks ( self . thunkResultGood , self . thunkErrorBad ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> self . assertEquals ( self . thunkResult . x , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . thunkResult . y [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEquals ( self . thunkResult . z [ <NUM_LIT:0> ] , '<STR_LIT:test>' ) <EOL> def test_observe ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> a = Observable ( ) <EOL> b = Observer ( ) <EOL> s . setNameForLocal ( "<STR_LIT:a>" , a ) <EOL> ra = c . remoteForName ( "<STR_LIT:a>" ) <EOL> ra . callRemote ( '<STR_LIT>' , b ) <EOL> pump . pump ( ) <EOL> a . notify ( <NUM_LIT:1> ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> a . notify ( <NUM_LIT:10> ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> self . assertNotIdentical ( b . obj , None , "<STR_LIT>" ) <EOL> self . assertEquals ( b . obj , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def test_defer ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> d = DeferredRemote ( ) <EOL> s . setNameForLocal ( "<STR_LIT:d>" , d ) <EOL> e = c . remoteForName ( "<STR_LIT:d>" ) <EOL> pump . pump ( ) ; pump . pump ( ) <EOL> results = [ ] <EOL> e . callRemote ( '<STR_LIT>' ) . addCallback ( results . append ) <EOL> pump . pump ( ) ; pump . pump ( ) <EOL> self . assertFalse ( d . run , "<STR_LIT>" ) <EOL> d . d . callback ( <NUM_LIT:5> ) <EOL> self . assertEquals ( d . run , <NUM_LIT:5> , "<STR_LIT>" ) <EOL> pump . pump ( ) ; pump . pump ( ) <EOL> self . assertEquals ( results [ <NUM_LIT:0> ] , <NUM_LIT:6> , "<STR_LIT>" ) <EOL> def test_refcount ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> foo = NestedRemote ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , foo ) <EOL> bar = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> bar . callRemote ( '<STR_LIT>' <EOL> ) . addCallbacks ( self . refcountResult , self . thunkErrorBad ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> rluid = self . nestedRemote . luid <EOL> self . assertIn ( rluid , s . localObjects ) <EOL> del self . nestedRemote <EOL> if sys . hexversion >= <NUM_LIT> : <EOL> gc . collect ( ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> self . assertNotIn ( rluid , s . localObjects ) <EOL> def test_cache ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> obj = NestedCache ( ) <EOL> obj2 = NestedComplicatedCache ( ) <EOL> vcc = obj2 . c <EOL> s . setNameForLocal ( "<STR_LIT>" , obj ) <EOL> s . setNameForLocal ( "<STR_LIT>" , obj2 ) <EOL> o2 = c . remoteForName ( "<STR_LIT>" ) <EOL> o3 = c . remoteForName ( "<STR_LIT>" ) <EOL> coll = [ ] <EOL> o2 . callRemote ( "<STR_LIT>" <EOL> ) . addCallback ( coll . append ) . addErrback ( coll . append ) <EOL> o2 . callRemote ( "<STR_LIT>" <EOL> ) . addCallback ( coll . append ) . addErrback ( coll . append ) <EOL> complex = [ ] <EOL> o3 . callRemote ( "<STR_LIT>" ) . addCallback ( complex . append ) <EOL> o3 . callRemote ( "<STR_LIT>" ) . addCallback ( complex . append ) <EOL> pump . flush ( ) <EOL> self . assertEquals ( complex [ <NUM_LIT:0> ] . x , <NUM_LIT:1> ) <EOL> self . assertEquals ( complex [ <NUM_LIT:0> ] . y , <NUM_LIT:2> ) <EOL> self . assertEquals ( complex [ <NUM_LIT:0> ] . foo , <NUM_LIT:3> ) <EOL> vcc . setFoo4 ( ) <EOL> pump . flush ( ) <EOL> self . assertEquals ( complex [ <NUM_LIT:0> ] . foo , <NUM_LIT:4> ) <EOL> self . assertEquals ( len ( coll ) , <NUM_LIT:2> ) <EOL> cp = coll [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertIdentical ( cp . checkMethod ( ) . im_self , cp , <EOL> "<STR_LIT>" ) <EOL> self . assertIdentical ( cp . checkSelf ( ) , cp , <EOL> "<STR_LIT>" ) <EOL> col2 = [ ] <EOL> o2 . callRemote ( '<STR_LIT>' , cp ) . addCallback ( col2 . append ) <EOL> pump . flush ( ) <EOL> self . assertTrue ( col2 [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( o2 . remoteMethod ( "<STR_LIT>" ) , <EOL> o2 . remoteMethod ( "<STR_LIT>" ) ) <EOL> luid = cp . luid <EOL> baroqueLuid = complex [ <NUM_LIT:0> ] . luid <EOL> self . assertIn ( luid , s . remotelyCachedObjects , <EOL> "<STR_LIT>" ) <EOL> del coll <EOL> del cp <EOL> pump . flush ( ) <EOL> del complex <EOL> del col2 <EOL> pump . flush ( ) <EOL> if sys . hexversion >= <NUM_LIT> : <EOL> gc . collect ( ) <EOL> pump . flush ( ) <EOL> self . assertNotIn ( luid , s . remotelyCachedObjects , <EOL> "<STR_LIT>" ) <EOL> self . assertNotIn ( luid , c . locallyCachedObjects , <EOL> "<STR_LIT>" ) <EOL> self . assertNotIn ( baroqueLuid , s . remotelyCachedObjects , <EOL> "<STR_LIT>" ) <EOL> self . assertNotIn ( baroqueLuid , c . locallyCachedObjects , <EOL> "<STR_LIT>" ) <EOL> self . assertIdentical ( vcc . observer , None , "<STR_LIT>" ) <EOL> def test_publishable ( self ) : <EOL> try : <EOL> os . unlink ( '<STR_LIT>' ) <EOL> except OSError : <EOL> pass <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> foo = GetPublisher ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , foo ) <EOL> bar = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> accum = [ ] <EOL> bar . callRemote ( '<STR_LIT>' ) . addCallbacks ( accum . append , self . thunkErrorBad ) <EOL> pump . flush ( ) <EOL> obj = accum . pop ( ) <EOL> self . assertEquals ( obj . activateCalled , <NUM_LIT:1> ) <EOL> self . assertEquals ( obj . isActivated , <NUM_LIT:1> ) <EOL> self . assertEquals ( obj . yayIGotPublished , <NUM_LIT:1> ) <EOL> self . assertEquals ( obj . _wasCleanWhenLoaded , <NUM_LIT:0> ) <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , foo ) <EOL> bar = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> bar . callRemote ( '<STR_LIT>' ) . addCallbacks ( accum . append , self . thunkErrorBad ) <EOL> pump . flush ( ) <EOL> obj = accum . pop ( ) <EOL> self . assertEquals ( obj . _wasCleanWhenLoaded , <NUM_LIT:1> ) <EOL> def gotCopy ( self , val ) : <EOL> self . thunkResult = val . id <EOL> def test_factoryCopy ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> ID = <NUM_LIT> <EOL> obj = NestedCopy ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , obj ) <EOL> x = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> x . callRemote ( '<STR_LIT>' , ID <EOL> ) . addCallbacks ( self . gotCopy , self . thunkResultBad ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> self . assertEquals ( self . thunkResult , ID , <EOL> "<STR_LIT>" % ( self . thunkResult , ) ) <EOL> bigString = "<STR_LIT>" * <NUM_LIT:50> <EOL> callbackArgs = None <EOL> callbackKeyword = None <EOL> def finishedCallback ( * args , ** kw ) : <EOL> global callbackArgs , callbackKeyword <EOL> callbackArgs = args <EOL> callbackKeyword = kw <EOL> class Pagerizer ( pb . Referenceable ) : <EOL> def __init__ ( self , callback , * args , ** kw ) : <EOL> self . callback , self . args , self . kw = callback , args , kw <EOL> def remote_getPages ( self , collector ) : <EOL> util . StringPager ( collector , bigString , <NUM_LIT:100> , <EOL> self . callback , * self . args , ** self . kw ) <EOL> self . args = self . kw = None <EOL> class FilePagerizer ( pb . Referenceable ) : <EOL> pager = None <EOL> def __init__ ( self , filename , callback , * args , ** kw ) : <EOL> self . filename = filename <EOL> self . callback , self . args , self . kw = callback , args , kw <EOL> def remote_getPages ( self , collector ) : <EOL> self . pager = util . FilePager ( collector , file ( self . filename ) , <EOL> self . callback , * self . args , ** self . kw ) <EOL> self . args = self . kw = None <EOL> class PagingTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . filename = self . mktemp ( ) <EOL> fd = file ( self . filename , '<STR_LIT:w>' ) <EOL> fd . write ( bigString ) <EOL> fd . close ( ) <EOL> def test_pagingWithCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , Pagerizer ( finishedCallback , '<STR_LIT:hello>' , value = <NUM_LIT:10> ) ) <EOL> x = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> l = [ ] <EOL> util . getAllPages ( x , "<STR_LIT>" ) . addCallback ( l . append ) <EOL> while not l : <EOL> pump . pump ( ) <EOL> self . assertEquals ( '<STR_LIT>' . join ( l [ <NUM_LIT:0> ] ) , bigString , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( callbackArgs , ( '<STR_LIT:hello>' , ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( callbackKeyword , { '<STR_LIT:value>' : <NUM_LIT:10> } , <EOL> "<STR_LIT>" ) <EOL> def test_pagingWithoutCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> s . setNameForLocal ( "<STR_LIT:foo>" , Pagerizer ( None ) ) <EOL> x = c . remoteForName ( "<STR_LIT:foo>" ) <EOL> l = [ ] <EOL> util . getAllPages ( x , "<STR_LIT>" ) . addCallback ( l . append ) <EOL> while not l : <EOL> pump . pump ( ) <EOL> self . assertEquals ( '<STR_LIT>' . join ( l [ <NUM_LIT:0> ] ) , bigString , <EOL> "<STR_LIT>" ) <EOL> def test_emptyFilePaging ( self ) : <EOL> """<STR_LIT>""" <EOL> filenameEmpty = self . mktemp ( ) <EOL> fd = file ( filenameEmpty , '<STR_LIT:w>' ) <EOL> fd . close ( ) <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> pagerizer = FilePagerizer ( filenameEmpty , None ) <EOL> s . setNameForLocal ( "<STR_LIT:bar>" , pagerizer ) <EOL> x = c . remoteForName ( "<STR_LIT:bar>" ) <EOL> l = [ ] <EOL> util . getAllPages ( x , "<STR_LIT>" ) . addCallback ( l . append ) <EOL> ttl = <NUM_LIT:10> <EOL> while not l and ttl > <NUM_LIT:0> : <EOL> pump . pump ( ) <EOL> ttl -= <NUM_LIT:1> <EOL> if not ttl : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' . join ( l [ <NUM_LIT:0> ] ) , '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> def test_filePagingWithCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> pagerizer = FilePagerizer ( self . filename , finishedCallback , <EOL> '<STR_LIT>' , value = <NUM_LIT:9> ) <EOL> s . setNameForLocal ( "<STR_LIT:bar>" , pagerizer ) <EOL> x = c . remoteForName ( "<STR_LIT:bar>" ) <EOL> l = [ ] <EOL> util . getAllPages ( x , "<STR_LIT>" ) . addCallback ( l . append ) <EOL> while not l : <EOL> pump . pump ( ) <EOL> self . assertEquals ( '<STR_LIT>' . join ( l [ <NUM_LIT:0> ] ) , bigString , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( callbackArgs , ( '<STR_LIT>' , ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( callbackKeyword , { '<STR_LIT:value>' : <NUM_LIT:9> } , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( pagerizer . pager . chunks , [ ] ) <EOL> def test_filePagingWithoutCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> pagerizer = FilePagerizer ( self . filename , None ) <EOL> s . setNameForLocal ( "<STR_LIT:bar>" , pagerizer ) <EOL> x = c . remoteForName ( "<STR_LIT:bar>" ) <EOL> l = [ ] <EOL> util . getAllPages ( x , "<STR_LIT>" ) . addCallback ( l . append ) <EOL> while not l : <EOL> pump . pump ( ) <EOL> self . assertEquals ( '<STR_LIT>' . join ( l [ <NUM_LIT:0> ] ) , bigString , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( pagerizer . pager . chunks , [ ] ) <EOL> class DumbPublishable ( publish . Publishable ) : <EOL> def getStateToPublish ( self ) : <EOL> return { "<STR_LIT>" : <NUM_LIT:1> } <EOL> class DumbPub ( publish . RemotePublished ) : <EOL> def activated ( self ) : <EOL> self . activateCalled = <NUM_LIT:1> <EOL> class GetPublisher ( pb . Referenceable ) : <EOL> def __init__ ( self ) : <EOL> self . pub = DumbPublishable ( "<STR_LIT>" ) <EOL> def remote_getPub ( self ) : <EOL> return self . pub <EOL> pb . setUnjellyableForClass ( DumbPublishable , DumbPub ) <EOL> class DisconnectionTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def error ( self , * args ) : <EOL> raise RuntimeError ( "<STR_LIT>" % ( args , ) ) <EOL> def gotDisconnected ( self ) : <EOL> """<STR_LIT>""" <EOL> self . gotCallback = <NUM_LIT:1> <EOL> def objectDisconnected ( self , o ) : <EOL> """<STR_LIT>""" <EOL> self . assertEquals ( o , self . remoteObject ) <EOL> self . objectCallback = <NUM_LIT:1> <EOL> def test_badSerialization ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> pump . pump ( ) <EOL> s . setNameForLocal ( "<STR_LIT:o>" , BadCopySet ( ) ) <EOL> g = c . remoteForName ( "<STR_LIT:o>" ) <EOL> l = [ ] <EOL> g . callRemote ( "<STR_LIT>" , BadCopyable ( ) ) . addErrback ( l . append ) <EOL> pump . flush ( ) <EOL> self . assertEquals ( len ( l ) , <NUM_LIT:1> ) <EOL> def test_disconnection ( self ) : <EOL> c , s , pump = connectedServerAndClient ( ) <EOL> pump . pump ( ) <EOL> s . setNameForLocal ( "<STR_LIT:o>" , SimpleRemote ( ) ) <EOL> r = c . remoteForName ( "<STR_LIT:o>" ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> pump . pump ( ) <EOL> c . notifyOnDisconnect ( self . error ) <EOL> self . assertIn ( self . error , c . disconnects ) <EOL> c . dontNotifyOnDisconnect ( self . error ) <EOL> self . assertNotIn ( self . error , c . disconnects ) <EOL> r . notifyOnDisconnect ( self . error ) <EOL> self . assertIn ( r . _disconnected , c . disconnects ) <EOL> self . assertIn ( self . error , r . disconnectCallbacks ) <EOL> r . dontNotifyOnDisconnect ( self . error ) <EOL> self . assertNotIn ( r . _disconnected , c . disconnects ) <EOL> self . assertNotIn ( self . error , r . disconnectCallbacks ) <EOL> c . notifyOnDisconnect ( self . gotDisconnected ) <EOL> r . notifyOnDisconnect ( self . objectDisconnected ) <EOL> self . remoteObject = r <EOL> c . connectionLost ( failure . Failure ( main . CONNECTION_DONE ) ) <EOL> self . assertTrue ( self . gotCallback ) <EOL> self . assertTrue ( self . objectCallback ) <EOL> class FreakOut ( Exception ) : <EOL> pass <EOL> class BadCopyable ( pb . Copyable ) : <EOL> def getStateToCopyFor ( self , p ) : <EOL> raise FreakOut ( ) <EOL> class BadCopySet ( pb . Referenceable ) : <EOL> def remote_setBadCopy ( self , bc ) : <EOL> return None <EOL> class LocalRemoteTest ( util . LocalAsRemote ) : <EOL> reportAllTracebacks = <NUM_LIT:0> <EOL> def sync_add1 ( self , x ) : <EOL> return x + <NUM_LIT:1> <EOL> def async_add ( self , x = <NUM_LIT:0> , y = <NUM_LIT:1> ) : <EOL> return x + y <EOL> def async_fail ( self ) : <EOL> raise RuntimeError ( ) <EOL> class MyPerspective ( pb . Avatar ) : <EOL> """<STR_LIT>""" <EOL> implements ( pb . IPerspective ) <EOL> loggedIn = loggedOut = False <EOL> def __init__ ( self , avatarId ) : <EOL> self . avatarId = avatarId <EOL> def perspective_getAvatarId ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . avatarId <EOL> def perspective_getViewPoint ( self ) : <EOL> return MyView ( ) <EOL> def perspective_add ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> return a + b <EOL> def logout ( self ) : <EOL> self . loggedOut = True <EOL> class TestRealm ( object ) : <EOL> """<STR_LIT>""" <EOL> perspectiveFactory = MyPerspective <EOL> lastPerspective = None <EOL> def requestAvatar ( self , avatarId , mind , interface ) : <EOL> """<STR_LIT>""" <EOL> assert interface == pb . IPerspective <EOL> assert mind == "<STR_LIT>" <EOL> if avatarId is checkers . ANONYMOUS : <EOL> return pb . IPerspective , Echoer ( ) , lambda : None <EOL> else : <EOL> self . lastPerspective = self . perspectiveFactory ( avatarId ) <EOL> self . lastPerspective . loggedIn = True <EOL> return ( <EOL> pb . IPerspective , self . lastPerspective , <EOL> self . lastPerspective . logout ) <EOL> class MyView ( pb . Viewable ) : <EOL> def view_check ( self , user ) : <EOL> return isinstance ( user , MyPerspective ) <EOL> class NewCredTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . realm = TestRealm ( ) <EOL> self . portal = portal . Portal ( self . realm ) <EOL> self . factory = ConnectionNotifyServerFactory ( self . portal ) <EOL> self . port = reactor . listenTCP ( <NUM_LIT:0> , self . factory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . portno = self . port . getHost ( ) . port <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . port . stopListening ( ) <EOL> def getFactoryAndRootObject ( self , clientFactory = pb . PBClientFactory ) : <EOL> """<STR_LIT>""" <EOL> factory = clientFactory ( ) <EOL> rootObjDeferred = factory . getRootObject ( ) <EOL> connector = reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return factory , rootObjDeferred <EOL> def test_getRootObject ( self ) : <EOL> """<STR_LIT>""" <EOL> factory , rootObjDeferred = self . getFactoryAndRootObject ( ) <EOL> def gotRootObject ( rootObj ) : <EOL> self . assertIsInstance ( rootObj , pb . RemoteReference ) <EOL> disconnectedDeferred = Deferred ( ) <EOL> rootObj . notifyOnDisconnect ( disconnectedDeferred . callback ) <EOL> factory . disconnect ( ) <EOL> return disconnectedDeferred <EOL> return rootObjDeferred . addCallback ( gotRootObject ) <EOL> def test_deadReferenceError ( self ) : <EOL> """<STR_LIT>""" <EOL> factory , rootObjDeferred = self . getFactoryAndRootObject ( ) <EOL> def gotRootObject ( rootObj ) : <EOL> disconnectedDeferred = Deferred ( ) <EOL> rootObj . notifyOnDisconnect ( disconnectedDeferred . callback ) <EOL> def lostConnection ( ign ) : <EOL> self . assertRaises ( <EOL> pb . DeadReferenceError , <EOL> rootObj . callRemote , '<STR_LIT>' ) <EOL> disconnectedDeferred . addCallback ( lostConnection ) <EOL> factory . disconnect ( ) <EOL> return disconnectedDeferred <EOL> return rootObjDeferred . addCallback ( gotRootObject ) <EOL> def test_clientConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> class ReconnectOnce ( pb . PBClientFactory ) : <EOL> reconnectedAlready = False <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> reconnecting = not self . reconnectedAlready <EOL> self . reconnectedAlready = True <EOL> if reconnecting : <EOL> connector . connect ( ) <EOL> return pb . PBClientFactory . clientConnectionLost ( <EOL> self , connector , reason , reconnecting ) <EOL> factory , rootObjDeferred = self . getFactoryAndRootObject ( ReconnectOnce ) <EOL> def gotRootObject ( rootObj ) : <EOL> self . assertIsInstance ( rootObj , pb . RemoteReference ) <EOL> d = Deferred ( ) <EOL> rootObj . notifyOnDisconnect ( d . callback ) <EOL> factory . disconnect ( ) <EOL> def disconnected ( ign ) : <EOL> d = factory . getRootObject ( ) <EOL> def gotAnotherRootObject ( anotherRootObj ) : <EOL> self . assertIsInstance ( anotherRootObj , pb . RemoteReference ) <EOL> d = Deferred ( ) <EOL> anotherRootObj . notifyOnDisconnect ( d . callback ) <EOL> factory . disconnect ( ) <EOL> return d <EOL> return d . addCallback ( gotAnotherRootObject ) <EOL> return d . addCallback ( disconnected ) <EOL> return rootObjDeferred . addCallback ( gotRootObject ) <EOL> def test_immediateClose ( self ) : <EOL> """<STR_LIT>""" <EOL> serverProto = self . factory . buildProtocol ( ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) ) <EOL> serverProto . makeConnection ( protocol . FileWrapper ( StringIO ( ) ) ) <EOL> serverProto . connectionLost ( failure . Failure ( main . CONNECTION_DONE ) ) <EOL> def test_loginConnectionRefused ( self ) : <EOL> """<STR_LIT>""" <EOL> clientFactory = pb . PBClientFactory ( ) <EOL> loginDeferred = clientFactory . login ( <EOL> credentials . UsernamePassword ( "<STR_LIT:foo>" , "<STR_LIT:bar>" ) ) <EOL> clientFactory . clientConnectionFailed ( <EOL> None , <EOL> failure . Failure ( <EOL> ConnectionRefusedError ( "<STR_LIT>" ) ) ) <EOL> return self . assertFailure ( loginDeferred , ConnectionRefusedError ) <EOL> def _disconnect ( self , ignore , factory ) : <EOL> """<STR_LIT>""" <EOL> disconnectedDeferred = Deferred ( ) <EOL> self . factory . protocolInstance . notifyOnDisconnect ( <EOL> lambda : disconnectedDeferred . callback ( None ) ) <EOL> factory . disconnect ( ) <EOL> return disconnectedDeferred <EOL> def test_loginLogout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( user = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> creds = credentials . UsernamePassword ( "<STR_LIT:user>" , "<STR_LIT>" ) <EOL> mind = "<STR_LIT>" <EOL> d = factory . login ( creds , mind ) <EOL> def cbLogin ( perspective ) : <EOL> self . assertTrue ( self . realm . lastPerspective . loggedIn ) <EOL> self . assertIsInstance ( perspective , pb . RemoteReference ) <EOL> return self . _disconnect ( None , factory ) <EOL> d . addCallback ( cbLogin ) <EOL> def cbLogout ( ignored ) : <EOL> self . assertTrue ( self . realm . lastPerspective . loggedOut ) <EOL> d . addCallback ( cbLogout ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_logoutAfterDecref ( self ) : <EOL> """<STR_LIT>""" <EOL> loggedOut = Deferred ( ) <EOL> class EventPerspective ( pb . Avatar ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , avatarId ) : <EOL> pass <EOL> def logout ( self ) : <EOL> loggedOut . callback ( None ) <EOL> self . realm . perspectiveFactory = EventPerspective <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( foo = '<STR_LIT:bar>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( <EOL> credentials . UsernamePassword ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , "<STR_LIT>" ) <EOL> def cbLoggedIn ( avatar ) : <EOL> return loggedOut <EOL> d . addCallback ( cbLoggedIn ) <EOL> def cbLoggedOut ( ignored ) : <EOL> self . assertEqual ( self . factory . protocolInstance . _localCleanup , { } ) <EOL> d . addCallback ( cbLoggedOut ) <EOL> d . addCallback ( self . _disconnect , factory ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_concurrentLogin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( <EOL> foo = '<STR_LIT:bar>' , baz = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> firstLogin = factory . login ( <EOL> credentials . UsernamePassword ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , "<STR_LIT>" ) <EOL> secondLogin = factory . login ( <EOL> credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> d = gatherResults ( [ firstLogin , secondLogin ] ) <EOL> def cbLoggedIn ( ( first , second ) ) : <EOL> return gatherResults ( [ <EOL> first . callRemote ( '<STR_LIT>' ) , <EOL> second . callRemote ( '<STR_LIT>' ) ] ) <EOL> d . addCallback ( cbLoggedIn ) <EOL> def cbAvatarIds ( ( first , second ) ) : <EOL> self . assertEqual ( first , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( second , '<STR_LIT>' ) <EOL> d . addCallback ( cbAvatarIds ) <EOL> d . addCallback ( self . _disconnect , factory ) <EOL> connector = reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_badUsernamePasswordLogin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( user = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> firstLogin = factory . login ( <EOL> credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> secondLogin = factory . login ( <EOL> credentials . UsernamePassword ( '<STR_LIT:user>' , '<STR_LIT>' ) ) <EOL> self . assertFailure ( firstLogin , UnauthorizedLogin ) <EOL> self . assertFailure ( secondLogin , UnauthorizedLogin ) <EOL> d = gatherResults ( [ firstLogin , secondLogin ] ) <EOL> def cleanup ( ignore ) : <EOL> errors = self . flushLoggedErrors ( UnauthorizedLogin ) <EOL> self . assertEquals ( len ( errors ) , <NUM_LIT:2> ) <EOL> return self . _disconnect ( None , factory ) <EOL> d . addCallback ( cleanup ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_anonymousLogin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( checkers . AllowAnonymousAccess ( ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( credentials . Anonymous ( ) , "<STR_LIT>" ) <EOL> def cbLoggedIn ( perspective ) : <EOL> return perspective . callRemote ( '<STR_LIT>' , <NUM_LIT> ) <EOL> d . addCallback ( cbLoggedIn ) <EOL> d . addCallback ( self . assertEqual , <NUM_LIT> ) <EOL> d . addCallback ( self . _disconnect , factory ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_anonymousLoginNotPermitted ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( user = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( credentials . Anonymous ( ) , "<STR_LIT>" ) <EOL> self . assertFailure ( d , UnhandledCredentials ) <EOL> def cleanup ( ignore ) : <EOL> errors = self . flushLoggedErrors ( UnhandledCredentials ) <EOL> self . assertEquals ( len ( errors ) , <NUM_LIT:1> ) <EOL> return self . _disconnect ( None , factory ) <EOL> d . addCallback ( cleanup ) <EOL> connector = reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_anonymousLoginWithMultipleCheckers ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( checkers . AllowAnonymousAccess ( ) ) <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( user = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( credentials . Anonymous ( ) , "<STR_LIT>" ) <EOL> def cbLogin ( perspective ) : <EOL> return perspective . callRemote ( '<STR_LIT>' , <NUM_LIT> ) <EOL> d . addCallback ( cbLogin ) <EOL> d . addCallback ( self . assertEqual , <NUM_LIT> ) <EOL> d . addCallback ( self . _disconnect , factory ) <EOL> connector = reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_authenticatedLoginWithMultipleCheckers ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( checkers . AllowAnonymousAccess ( ) ) <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( user = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( <EOL> credentials . UsernamePassword ( '<STR_LIT:user>' , '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> def cbLogin ( perspective ) : <EOL> return perspective . callRemote ( '<STR_LIT>' , <NUM_LIT:100> , <NUM_LIT> ) <EOL> d . addCallback ( cbLogin ) <EOL> d . addCallback ( self . assertEqual , <NUM_LIT> ) <EOL> d . addCallback ( self . _disconnect , factory ) <EOL> connector = reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> def test_view ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portal . registerChecker ( <EOL> checkers . InMemoryUsernamePasswordDatabaseDontUse ( user = '<STR_LIT>' ) ) <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( <EOL> credentials . UsernamePassword ( "<STR_LIT:user>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def cbLogin ( perspective ) : <EOL> return perspective . callRemote ( "<STR_LIT>" ) <EOL> d . addCallback ( cbLogin ) <EOL> def cbView ( viewpoint ) : <EOL> return viewpoint . callRemote ( "<STR_LIT>" ) <EOL> d . addCallback ( cbView ) <EOL> d . addCallback ( self . assertTrue ) <EOL> d . addCallback ( self . _disconnect , factory ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , self . portno , factory ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return d <EOL> class NonSubclassingPerspective : <EOL> implements ( pb . IPerspective ) <EOL> def __init__ ( self , avatarId ) : <EOL> pass <EOL> def perspectiveMessageReceived ( self , broker , message , args , kwargs ) : <EOL> args = broker . unserialize ( args , self ) <EOL> kwargs = broker . unserialize ( kwargs , self ) <EOL> return broker . serialize ( ( message , args , kwargs ) ) <EOL> def logout ( self ) : <EOL> self . loggedOut = True <EOL> class NSPTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . realm = TestRealm ( ) <EOL> self . realm . perspectiveFactory = NonSubclassingPerspective <EOL> self . portal = portal . Portal ( self . realm ) <EOL> self . checker = checkers . InMemoryUsernamePasswordDatabaseDontUse ( ) <EOL> self . checker . addUser ( "<STR_LIT:user>" , "<STR_LIT>" ) <EOL> self . portal . registerChecker ( self . checker ) <EOL> self . factory = WrappingFactory ( pb . PBServerFactory ( self . portal ) ) <EOL> self . port = reactor . listenTCP ( <NUM_LIT:0> , self . factory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( self . port . stopListening ) <EOL> self . portno = self . port . getHost ( ) . port <EOL> def test_NSP ( self ) : <EOL> """<STR_LIT>""" <EOL> factory = pb . PBClientFactory ( ) <EOL> d = factory . login ( credentials . UsernamePassword ( '<STR_LIT:user>' , '<STR_LIT>' ) , <EOL> "<STR_LIT>" ) <EOL> reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , self . portno , factory ) <EOL> d . addCallback ( lambda p : p . callRemote ( '<STR_LIT>' , '<STR_LIT>' , bar = '<STR_LIT>' ) ) <EOL> d . addCallback ( self . assertEquals , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) , { '<STR_LIT:bar>' : '<STR_LIT>' } ) ) <EOL> def cleanup ( ignored ) : <EOL> factory . disconnect ( ) <EOL> for p in self . factory . protocols : <EOL> p . transport . loseConnection ( ) <EOL> d . addCallback ( cleanup ) <EOL> return d <EOL> class IForwarded ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def forwardMe ( ) : <EOL> """<STR_LIT>""" <EOL> def forwardDeferred ( ) : <EOL> """<STR_LIT>""" <EOL> class Forwarded : <EOL> """<STR_LIT>""" <EOL> implements ( IForwarded ) <EOL> forwarded = False <EOL> unforwarded = False <EOL> def forwardMe ( self ) : <EOL> """<STR_LIT>""" <EOL> self . forwarded = True <EOL> def dontForwardMe ( self ) : <EOL> """<STR_LIT>""" <EOL> self . unforwarded = True <EOL> def forwardDeferred ( self ) : <EOL> """<STR_LIT>""" <EOL> return succeed ( True ) <EOL> class SpreadUtilTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_sync ( self ) : <EOL> """<STR_LIT>""" <EOL> o = LocalRemoteTest ( ) <EOL> self . assertEquals ( o . callRemote ( "<STR_LIT>" , <NUM_LIT:2> ) , <NUM_LIT:3> ) <EOL> def test_async ( self ) : <EOL> """<STR_LIT>""" <EOL> o = LocalRemoteTest ( ) <EOL> o = LocalRemoteTest ( ) <EOL> d = o . callRemote ( "<STR_LIT>" , <NUM_LIT:2> , y = <NUM_LIT:4> ) <EOL> self . assertIsInstance ( d , Deferred ) <EOL> d . addCallback ( self . assertEquals , <NUM_LIT:6> ) <EOL> return d <EOL> def test_asyncFail ( self ) : <EOL> """<STR_LIT>""" <EOL> o = LocalRemoteTest ( ) <EOL> d = o . callRemote ( "<STR_LIT>" ) <EOL> def eb ( f ) : <EOL> self . assertTrue ( isinstance ( f , failure . Failure ) ) <EOL> f . trap ( RuntimeError ) <EOL> d . addCallbacks ( lambda res : self . fail ( "<STR_LIT>" ) , eb ) <EOL> return d <EOL> def test_remoteMethod ( self ) : <EOL> """<STR_LIT>""" <EOL> o = LocalRemoteTest ( ) <EOL> m = o . remoteMethod ( "<STR_LIT>" ) <EOL> self . assertEquals ( m ( <NUM_LIT:3> ) , <NUM_LIT:4> ) <EOL> def test_localAsyncForwarder ( self ) : <EOL> """<STR_LIT>""" <EOL> f = Forwarded ( ) <EOL> lf = util . LocalAsyncForwarder ( f , IForwarded ) <EOL> lf . callRemote ( "<STR_LIT>" ) <EOL> self . assertTrue ( f . forwarded ) <EOL> lf . callRemote ( "<STR_LIT>" ) <EOL> self . assertFalse ( f . unforwarded ) <EOL> rr = lf . callRemote ( "<STR_LIT>" ) <EOL> l = [ ] <EOL> rr . addCallback ( l . append ) <EOL> self . assertEqual ( l [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> class PBWithSecurityOptionsTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_clientDefaultSecurityOptions ( self ) : <EOL> """<STR_LIT>""" <EOL> factory = pb . PBClientFactory ( ) <EOL> broker = factory . buildProtocol ( None ) <EOL> self . assertIdentical ( broker . security , jelly . globalSecurity ) <EOL> def test_serverDefaultSecurityOptions ( self ) : <EOL> """<STR_LIT>""" <EOL> factory = pb . PBServerFactory ( Echoer ( ) ) <EOL> broker = factory . buildProtocol ( None ) <EOL> self . assertIdentical ( broker . security , jelly . globalSecurity ) <EOL> def test_clientSecurityCustomization ( self ) : <EOL> """<STR_LIT>""" <EOL> security = jelly . SecurityOptions ( ) <EOL> factory = pb . PBClientFactory ( security = security ) <EOL> broker = factory . buildProtocol ( None ) <EOL> self . assertIdentical ( broker . security , security ) <EOL> def test_serverSecurityCustomization ( self ) : <EOL> """<STR_LIT>""" <EOL> security = jelly . SecurityOptions ( ) <EOL> factory = pb . PBServerFactory ( Echoer ( ) , security = security ) <EOL> broker = factory . buildProtocol ( None ) <EOL> self . assertIdentical ( broker . security , security ) <EOL> class DeprecationTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_noOperationDeprecated ( self ) : <EOL> """<STR_LIT>""" <EOL> self . callDeprecated ( <EOL> Version ( "<STR_LIT>" , <NUM_LIT:8> , <NUM_LIT:2> , <NUM_LIT:0> ) , <EOL> pb . noOperation , <NUM_LIT:1> , <NUM_LIT:2> , x = <NUM_LIT:3> , y = <NUM_LIT:4> ) <EOL> def test_printTraceback ( self ) : <EOL> """<STR_LIT>""" <EOL> self . callDeprecated ( <EOL> Version ( "<STR_LIT>" , <NUM_LIT:8> , <NUM_LIT:2> , <NUM_LIT:0> ) , <EOL> pb . printTraceback , <EOL> "<STR_LIT>" ) </s>
