<s> """<STR_LIT>""" <EOL> import unittest <EOL> import time <EOL> import thespian . test . helpers <EOL> from thespian . actors import * <EOL> from thespian . test import ActorSystemTestCase <EOL> class rosaline ( Actor ) : <EOL> name = '<STR_LIT>' <EOL> class Romeo ( Actor ) : <EOL> def receiveMessage ( self , msg , sender ) : <EOL> if isinstance ( msg , JulietAppears ) : <EOL> self . send ( msg . juliet , "<STR_LIT>" ) <EOL> elif isinstance ( msg , ActorExitRequest ) : <EOL> pass <EOL> elif msg == '<STR_LIT>' : <EOL> self . send ( sender , '<STR_LIT>' ) <EOL> elif msg == '<STR_LIT>' : <EOL> self . send ( sender , '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in msg : <EOL> pass <EOL> elif '<STR_LIT>' in msg : <EOL> self . send ( sender , '<STR_LIT>' ) <EOL> elif '<STR_LIT:hello>' in msg : <EOL> print ( '<STR_LIT>' % ( str ( self ) ) ) <EOL> elif '<STR_LIT>' == msg : <EOL> self . send ( sender , self . myAddress ) <EOL> class Capulet ( Actor ) : <EOL> def receiveMessage ( self , msg , sender ) : <EOL> if msg == "<STR_LIT>" : <EOL> self . send ( sender , self . createActor ( Juliet ) ) <EOL> class Juliet ( Actor ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . nurse = None <EOL> self . recalled = False <EOL> super ( Juliet , self ) . __init__ ( * args , ** kw ) <EOL> def receiveMessage ( self , msg , sender ) : <EOL> if isinstance ( msg , ActorExitRequest ) : <EOL> pass <EOL> elif "<STR_LIT>" in msg : <EOL> self . send ( sender , '<STR_LIT>' ) <EOL> elif msg == '<STR_LIT>' : <EOL> self . send ( sender , '<STR_LIT>' ) <EOL> elif msg == '<STR_LIT>' : <EOL> self . send ( sender , "<STR_LIT>" ) <EOL> self . send ( sender , "<STR_LIT>" ) <EOL> elif msg == '<STR_LIT>' : <EOL> if self . nurse : <EOL> self . send ( self . nurse , '<STR_LIT>' ) <EOL> else : <EOL> self . recalled = True <EOL> elif msg == '<STR_LIT>' : <EOL> self . nurse = sender <EOL> if self . recalled : <EOL> self . send ( self . nurse , '<STR_LIT>' ) <EOL> elif '<STR_LIT>' == msg : <EOL> self . send ( sender , self . myAddress ) <EOL> class Nurse ( Actor ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . heardItAll = False <EOL> super ( Nurse , self ) . __init__ ( * args , ** kw ) <EOL> def receiveMessage ( self , msg , sender ) : <EOL> if type ( msg ) == type ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) and msg [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . send ( msg [ <NUM_LIT:1> ] , JulietAppears ( msg [ <NUM_LIT:2> ] ) ) <EOL> self . send ( msg [ <NUM_LIT:2> ] , '<STR_LIT>' ) <EOL> elif msg == '<STR_LIT>' : <EOL> self . heardItAll = True <EOL> elif msg == '<STR_LIT>' : <EOL> self . send ( sender , '<STR_LIT>' if self . heardItAll else '<STR_LIT>' ) <EOL> class JulietAppears : <EOL> stage = '<STR_LIT>' <EOL> def __init__ ( self , julietAddr ) : <EOL> self . juliet = julietAddr <EOL> class TestASimpleSystem ( ActorSystemTestCase ) : <EOL> testbase = '<STR_LIT>' <EOL> scope = '<STR_LIT>' <EOL> def test01_ActorSystemStartupShutdown ( self ) : <EOL> rosalineA = ActorSystem ( ) . createActor ( rosaline ) <EOL> def test01_1_ActorSystemMultipleShutdown ( self ) : <EOL> rosalineA = ActorSystem ( ) . createActor ( rosaline ) <EOL> ActorSystem ( ) . shutdown ( ) <EOL> ActorSystem ( ) . shutdown ( ) <EOL> def test02_PrimaryActorCreation ( self ) : <EOL> romeo = ActorSystem ( ) . createActor ( Romeo ) <EOL> juliet = ActorSystem ( ) . createActor ( Juliet ) <EOL> self . assertNotEqual ( romeo , juliet ) <EOL> def test03_CreateActorUniqueAddress ( self ) : <EOL> romeo = ActorSystem ( ) . createActor ( Romeo ) <EOL> juliet = ActorSystem ( ) . createActor ( Juliet ) <EOL> self . assertNotEqual ( romeo , juliet ) <EOL> romeo2 = ActorSystem ( ) . createActor ( Romeo ) <EOL> self . assertNotEqual ( romeo , romeo2 ) <EOL> def NOtest04_PossibleActorSystemResourceExhaustion ( self ) : <EOL> asys = ActorSystem ( ) <EOL> try : <EOL> addresses = [ asys . createActor ( Juliet ) for n in range ( <NUM_LIT> ) ] <EOL> except OSError as err : <EOL> import errno <EOL> if err . errno == errno . EGAIN : <EOL> pass <EOL> else : <EOL> raise <EOL> def test05_ManyActorsUniqueAddress ( self ) : <EOL> asys = ActorSystem ( ) <EOL> addresses = [ asys . createActor ( Juliet ) for n in range ( <NUM_LIT:100> ) ] <EOL> uniqueAddresses = set ( addresses ) <EOL> if len ( addresses ) != len ( uniqueAddresses ) : <EOL> duplicates = [ A for A in uniqueAddresses if len ( [ X for X in addresses if X == A ] ) > <NUM_LIT:1> ] <EOL> print ( '<STR_LIT>' % map ( str , duplicates ) ) <EOL> if duplicates : <EOL> for each in duplicates : <EOL> print ( '<STR_LIT>' % ( str ( each ) , str ( [ N for N , A in enumerate ( addresses ) if A == each ] ) ) ) <EOL> print ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( addresses ) , len ( uniqueAddresses ) ) <EOL> def test06_ManyActorsValidAddresses ( self ) : <EOL> import string <EOL> addresses = [ ActorSystem ( ) . createActor ( Juliet ) for n in range ( <NUM_LIT:100> ) ] <EOL> for addr in addresses : <EOL> invchar = '<STR_LIT>' . join ( [ c for c in str ( addr ) <EOL> if c not in string . ascii_letters + string . digits + "<STR_LIT>" ] ) <EOL> self . assertEqual ( str ( addr ) , str ( addr ) + invchar ) <EOL> def test07_SingleNonListeningActorTell ( self ) : <EOL> rosalineA = ActorSystem ( ) . createActor ( rosaline ) <EOL> ActorSystem ( ) . tell ( rosalineA , '<STR_LIT:hello>' ) <EOL> self . assertTrue ( True ) <EOL> def test08_SingleActorTell ( self ) : <EOL> romeoA = ActorSystem ( ) . createActor ( Romeo ) <EOL> ActorSystem ( ) . tell ( romeoA , '<STR_LIT:hello>' ) <EOL> def test09_SingleActorAsk ( self ) : <EOL> romeoA = ActorSystem ( ) . createActor ( Romeo ) <EOL> resp = ActorSystem ( ) . ask ( romeoA , '<STR_LIT>' ) <EOL> self . assertEqual ( resp , '<STR_LIT>' ) <EOL> def test10_ActorAskWithNoResponse ( self ) : <EOL> romeoA = ActorSystem ( ) . createActor ( Romeo ) <EOL> resp = ActorSystem ( ) . ask ( romeoA , "<STR_LIT>" , <NUM_LIT> ) <EOL> self . assertEqual ( resp , None ) <EOL> resp = ActorSystem ( ) . ask ( romeoA , '<STR_LIT>' ) <EOL> self . assertEqual ( resp , '<STR_LIT>' ) <EOL> def test11_SingleActorAskMultipleTimes ( self ) : <EOL> romeoA = ActorSystem ( ) . createActor ( Romeo ) <EOL> self . assertEqual ( ActorSystem ( ) . ask ( romeoA , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( ActorSystem ( ) . ask ( romeoA , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( ActorSystem ( ) . ask ( romeoA , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( ActorSystem ( ) . ask ( romeoA , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> def test12_MultipleActorsAskMultipleTimes ( self ) : <EOL> play = ActorSystem ( ) <EOL> romeo = play . createActor ( Romeo ) <EOL> self . assertEqual ( play . ask ( romeo , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> juliet = play . createActor ( Juliet ) <EOL> self . assertEqual ( play . ask ( romeo , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( play . ask ( romeo , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( play . ask ( juliet , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( play . ask ( romeo , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( play . ask ( juliet , "<STR_LIT>" ) , '<STR_LIT>' ) <EOL> def test13_SubActorCreation ( self ) : <EOL> capulet = ActorSystem ( ) . createActor ( Capulet ) <EOL> juliet = ActorSystem ( ) . ask ( capulet , '<STR_LIT>' , <NUM_LIT> ) <EOL> print ( '<STR_LIT>' % str ( juliet ) ) <EOL> self . assertIsNot ( juliet , None ) <EOL> if juliet : <EOL> self . assertEqual ( ActorSystem ( ) . ask ( juliet , '<STR_LIT>' ) , '<STR_LIT>' , <NUM_LIT> ) <EOL> juliet2 = ActorSystem ( ) . ask ( capulet , '<STR_LIT>' ) <EOL> self . assertIsNot ( juliet2 , None ) <EOL> if juliet2 : <EOL> self . assertEqual ( ActorSystem ( ) . ask ( juliet2 , '<STR_LIT>' ) , '<STR_LIT>' , <NUM_LIT:0.5> ) <EOL> self . assertEqual ( ActorSystem ( ) . ask ( juliet , '<STR_LIT>' ) , '<STR_LIT>' , <NUM_LIT:0.5> ) <EOL> def test14_EntireActWithActorStart ( self ) : <EOL> play = ActorSystem ( ) <EOL> romeo = play . createActor ( Romeo ) <EOL> juliet = play . createActor ( Juliet ) <EOL> nurse = play . createActor ( Nurse ) <EOL> self . assertEqual ( play . ask ( nurse , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> play . tell ( nurse , ( '<STR_LIT>' , romeo , juliet ) ) <EOL> for X in range ( <NUM_LIT:50> ) : <EOL> if play . ask ( nurse , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> break <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . assertEqual ( play . ask ( nurse , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test15_IncompleteActMissingActor ( self ) : <EOL> play = ActorSystem ( ) <EOL> romeo = play . createActor ( Romeo ) <EOL> juliet = play . createActor ( Juliet ) <EOL> play . tell ( romeo , JulietAppears ( juliet ) ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> nurse = play . createActor ( Nurse ) <EOL> self . assertEqual ( play . ask ( nurse , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> play . tell ( nurse , ( '<STR_LIT>' , romeo , juliet ) ) <EOL> for X in range ( <NUM_LIT:50> ) : <EOL> if play . ask ( nurse , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> break <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . assertEqual ( play . ask ( nurse , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test16_ActorProperties ( self ) : <EOL> play = ActorSystem ( ) <EOL> romeo = play . createActor ( Romeo ) <EOL> juliet = play . createActor ( Juliet ) <EOL> self . assertIsNotNone ( play . ask ( romeo , '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertIsNotNone ( play . ask ( juliet , '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertNotEqual ( play . ask ( romeo , '<STR_LIT>' , <NUM_LIT> ) , <EOL> play . ask ( juliet , '<STR_LIT>' , <NUM_LIT> ) ) <EOL> class TestMultiprocUDPSystem ( TestASimpleSystem ) : <EOL> testbase = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . setSystemBase ( '<STR_LIT>' ) <EOL> super ( TestMultiprocUDPSystem , self ) . setUp ( ) <EOL> class TestMultiprocTCPSystem ( TestASimpleSystem ) : <EOL> testbase = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . setSystemBase ( '<STR_LIT>' ) <EOL> super ( TestMultiprocTCPSystem , self ) . setUp ( ) <EOL> class TestMultiprocQueueSystem ( TestASimpleSystem ) : <EOL> testbase = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . setSystemBase ( '<STR_LIT>' ) <EOL> super ( TestMultiprocQueueSystem , self ) . setUp ( ) </s>
