<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> from cStringIO import StringIO <EOL> from twisted . internet . protocol import ProcessProtocol <EOL> from twisted . internet . defer import fail , succeed <EOL> from twisted . internet . task import Cooperator , deferLater <EOL> from twisted . internet . main import CONNECTION_DONE <EOL> from twisted . internet import reactor <EOL> from twisted . python . failure import Failure <EOL> from twisted . python . lockfile import FilesystemLock <EOL> from twisted . test . test_cooperator import FakeScheduler <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . trial . reporter import Reporter , TreeReporter <EOL> from twisted . trial . reporter import UncleanWarningsReporterWrapper <EOL> from twisted . trial . runner import TrialSuite , ErrorHolder <EOL> from twisted . trial . _dist . disttrial import DistTrialRunner <EOL> from twisted . trial . _dist . distreporter import DistReporter <EOL> from twisted . trial . _dist . worker import LocalWorker <EOL> class FakeTransport ( object ) : <EOL> """<STR_LIT>""" <EOL> def writeToChild ( self , fd , data ) : <EOL> """<STR_LIT>""" <EOL> class FakeReactor ( object ) : <EOL> """<STR_LIT>""" <EOL> spawnCount = <NUM_LIT:0> <EOL> stopCount = <NUM_LIT:0> <EOL> runCount = <NUM_LIT:0> <EOL> def spawnProcess ( self , worker , * args , ** kwargs ) : <EOL> worker . makeConnection ( FakeTransport ( ) ) <EOL> self . spawnCount += <NUM_LIT:1> <EOL> def stop ( self ) : <EOL> self . stopCount += <NUM_LIT:1> <EOL> def run ( self ) : <EOL> self . runCount += <NUM_LIT:1> <EOL> def addSystemEventTrigger ( self , * args , ** kw ) : <EOL> pass <EOL> class EternalTerminationPredicateFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> class DistTrialRunnerTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . runner = DistTrialRunner ( TreeReporter , <NUM_LIT:4> , [ ] , <EOL> workingDirectory = self . mktemp ( ) ) <EOL> self . runner . _stream = StringIO ( ) <EOL> def getFakeSchedulerAndEternalCooperator ( self ) : <EOL> """<STR_LIT>""" <EOL> scheduler = FakeScheduler ( ) <EOL> cooperator = Cooperator ( <EOL> scheduler = scheduler , <EOL> terminationPredicateFactory = EternalTerminationPredicateFactory , <EOL> ) <EOL> return scheduler , cooperator <EOL> def test_writeResults ( self ) : <EOL> """<STR_LIT>""" <EOL> stringIO = StringIO ( ) <EOL> result = DistReporter ( Reporter ( stringIO ) ) <EOL> self . runner . writeResults ( result ) <EOL> self . assertTrue ( stringIO . tell ( ) > <NUM_LIT:0> ) <EOL> def test_createLocalWorkers ( self ) : <EOL> """<STR_LIT>""" <EOL> protocols = [ object ( ) for x in xrange ( <NUM_LIT:4> ) ] <EOL> workers = self . runner . createLocalWorkers ( protocols , "<STR_LIT:path>" ) <EOL> for s in workers : <EOL> self . assertIsInstance ( s , LocalWorker ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( workers ) ) <EOL> def test_launchWorkerProcesses ( self ) : <EOL> """<STR_LIT>""" <EOL> protocols = [ ProcessProtocol ( ) for i in range ( <NUM_LIT:4> ) ] <EOL> arguments = [ ] <EOL> environment = { } <EOL> def fakeSpawnProcess ( processProtocol , executable , args = ( ) , env = { } , <EOL> path = None , uid = None , gid = None , usePTY = <NUM_LIT:0> , <EOL> childFDs = None ) : <EOL> arguments . append ( executable ) <EOL> arguments . extend ( args ) <EOL> environment . update ( env ) <EOL> self . runner . launchWorkerProcesses ( <EOL> fakeSpawnProcess , protocols , [ "<STR_LIT:foo>" ] ) <EOL> self . assertEqual ( arguments [ <NUM_LIT:0> ] , arguments [ <NUM_LIT:1> ] ) <EOL> self . assertTrue ( os . path . exists ( arguments [ <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( "<STR_LIT:foo>" , arguments [ <NUM_LIT:3> ] ) <EOL> self . assertEqual ( os . pathsep . join ( sys . path ) , <EOL> environment [ "<STR_LIT>" ] ) <EOL> def test_run ( self ) : <EOL> """<STR_LIT>""" <EOL> fakeReactor = FakeReactor ( ) <EOL> suite = TrialSuite ( ) <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> suite . addTest ( TestCase ( ) ) <EOL> self . runner . run ( suite , fakeReactor ) <EOL> self . assertEqual ( fakeReactor . runCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( fakeReactor . spawnCount , self . runner . _workerNumber ) <EOL> def test_runUsedDirectory ( self ) : <EOL> """<STR_LIT>""" <EOL> class FakeReactorWithLock ( FakeReactor ) : <EOL> def spawnProcess ( oself , worker , * args , ** kwargs ) : <EOL> self . assertEqual ( os . path . abspath ( worker . _logDirectory ) , <EOL> os . path . abspath ( <EOL> os . path . join ( workingDirectory + "<STR_LIT>" , <EOL> str ( oself . spawnCount ) ) ) ) <EOL> localLock = FilesystemLock ( workingDirectory + "<STR_LIT>" ) <EOL> self . assertFalse ( localLock . lock ( ) ) <EOL> oself . spawnCount += <NUM_LIT:1> <EOL> worker . makeConnection ( FakeTransport ( ) ) <EOL> worker . _ampProtocol . run = lambda * args : succeed ( None ) <EOL> newDirectory = self . mktemp ( ) <EOL> os . mkdir ( newDirectory ) <EOL> workingDirectory = os . path . join ( newDirectory , "<STR_LIT>" ) <EOL> lock = FilesystemLock ( workingDirectory + "<STR_LIT>" ) <EOL> lock . lock ( ) <EOL> self . addCleanup ( lock . unlock ) <EOL> self . runner . _workingDirectory = workingDirectory <EOL> fakeReactor = FakeReactorWithLock ( ) <EOL> suite = TrialSuite ( ) <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> suite . addTest ( TestCase ( ) ) <EOL> self . runner . run ( suite , fakeReactor ) <EOL> def test_minimalWorker ( self ) : <EOL> """<STR_LIT>""" <EOL> fakeReactor = FakeReactor ( ) <EOL> self . runner . run ( TestCase ( ) , fakeReactor ) <EOL> self . assertEqual ( fakeReactor . runCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( fakeReactor . spawnCount , <NUM_LIT:1> ) <EOL> def test_runUncleanWarnings ( self ) : <EOL> """<STR_LIT>""" <EOL> fakeReactor = FakeReactor ( ) <EOL> self . runner . _uncleanWarnings = True <EOL> result = self . runner . run ( TestCase ( ) , fakeReactor ) <EOL> self . assertIsInstance ( result , DistReporter ) <EOL> self . assertIsInstance ( result . original , <EOL> UncleanWarningsReporterWrapper ) <EOL> def test_runWithoutTest ( self ) : <EOL> """<STR_LIT>""" <EOL> fakeReactor = object ( ) <EOL> suite = TrialSuite ( ) <EOL> result = self . runner . run ( suite , fakeReactor ) <EOL> self . assertIsInstance ( result , DistReporter ) <EOL> output = self . runner . _stream . getvalue ( ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> def test_runWithoutTestButWithAnError ( self ) : <EOL> """<STR_LIT>""" <EOL> fakeReactor = object ( ) <EOL> error = ErrorHolder ( "<STR_LIT>" , Failure ( RuntimeError ( "<STR_LIT>" ) ) ) <EOL> result = self . runner . run ( error , fakeReactor ) <EOL> self . assertIsInstance ( result , DistReporter ) <EOL> output = self . runner . _stream . getvalue ( ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> def test_runUnexpectedError ( self ) : <EOL> """<STR_LIT>""" <EOL> class FakeReactorWithFail ( FakeReactor ) : <EOL> def spawnProcess ( self , worker , * args , ** kwargs ) : <EOL> worker . makeConnection ( FakeTransport ( ) ) <EOL> self . spawnCount += <NUM_LIT:1> <EOL> worker . _ampProtocol . run = self . failingRun <EOL> def failingRun ( self , case , result ) : <EOL> return fail ( RuntimeError ( "<STR_LIT>" ) ) <EOL> scheduler , cooperator = self . getFakeSchedulerAndEternalCooperator ( ) <EOL> fakeReactor = FakeReactorWithFail ( ) <EOL> result = self . runner . run ( TestCase ( ) , fakeReactor , <EOL> cooperator . cooperate ) <EOL> self . assertEqual ( fakeReactor . runCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( fakeReactor . spawnCount , <NUM_LIT:1> ) <EOL> scheduler . pump ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result . original . failures ) ) <EOL> def test_runStopAfterTests ( self ) : <EOL> """<STR_LIT>""" <EOL> functions = [ ] <EOL> class FakeReactorWithSuccess ( FakeReactor ) : <EOL> def spawnProcess ( self , worker , * args , ** kwargs ) : <EOL> worker . makeConnection ( FakeTransport ( ) ) <EOL> self . spawnCount += <NUM_LIT:1> <EOL> worker . _ampProtocol . run = self . succeedingRun <EOL> def succeedingRun ( self , case , result ) : <EOL> return succeed ( None ) <EOL> def addSystemEventTrigger ( oself , phase , event , function ) : <EOL> self . assertEqual ( '<STR_LIT>' , phase ) <EOL> self . assertEqual ( '<STR_LIT>' , event ) <EOL> functions . append ( function ) <EOL> workingDirectory = self . runner . _workingDirectory <EOL> fakeReactor = FakeReactorWithSuccess ( ) <EOL> self . runner . run ( TestCase ( ) , fakeReactor ) <EOL> def check ( ) : <EOL> localLock = FilesystemLock ( workingDirectory + "<STR_LIT>" ) <EOL> self . assertTrue ( localLock . lock ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , fakeReactor . stopCount ) <EOL> self . assertIdentical ( None , functions [ <NUM_LIT:0> ] ( ) ) <EOL> return deferLater ( reactor , <NUM_LIT:0> , check ) <EOL> def test_runWaitForProcessesDeferreds ( self ) : <EOL> """<STR_LIT>""" <EOL> functions = [ ] <EOL> workers = [ ] <EOL> class FakeReactorWithEvent ( FakeReactor ) : <EOL> def spawnProcess ( self , worker , * args , ** kwargs ) : <EOL> worker . makeConnection ( FakeTransport ( ) ) <EOL> workers . append ( worker ) <EOL> def addSystemEventTrigger ( oself , phase , event , function ) : <EOL> self . assertEqual ( '<STR_LIT>' , phase ) <EOL> self . assertEqual ( '<STR_LIT>' , event ) <EOL> functions . append ( function ) <EOL> workingDirectory = self . runner . _workingDirectory <EOL> fakeReactor = FakeReactorWithEvent ( ) <EOL> self . runner . run ( TestCase ( ) , fakeReactor ) <EOL> def check ( ign ) : <EOL> return deferLater ( reactor , <NUM_LIT:0> , realCheck ) <EOL> def realCheck ( ) : <EOL> localLock = FilesystemLock ( workingDirectory + "<STR_LIT>" ) <EOL> self . assertTrue ( localLock . lock ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , fakeReactor . stopCount ) <EOL> workers [ <NUM_LIT:0> ] . processEnded ( Failure ( CONNECTION_DONE ) ) <EOL> return functions [ <NUM_LIT:0> ] ( ) . addCallback ( check ) <EOL> def test_runUntilFailure ( self ) : <EOL> """<STR_LIT>""" <EOL> called = [ ] <EOL> class FakeReactorWithSuccess ( FakeReactor ) : <EOL> def spawnProcess ( self , worker , * args , ** kwargs ) : <EOL> worker . makeConnection ( FakeTransport ( ) ) <EOL> self . spawnCount += <NUM_LIT:1> <EOL> worker . _ampProtocol . run = self . succeedingRun <EOL> def succeedingRun ( self , case , result ) : <EOL> called . append ( None ) <EOL> if len ( called ) == <NUM_LIT:5> : <EOL> return fail ( RuntimeError ( "<STR_LIT>" ) ) <EOL> return succeed ( None ) <EOL> fakeReactor = FakeReactorWithSuccess ( ) <EOL> scheduler , cooperator = self . getFakeSchedulerAndEternalCooperator ( ) <EOL> result = self . runner . run ( <EOL> TestCase ( ) , fakeReactor , cooperate = cooperator . cooperate , <EOL> untilFailure = True ) <EOL> scheduler . pump ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( called ) ) <EOL> self . assertFalse ( result . wasSuccessful ( ) ) <EOL> output = self . runner . _stream . getvalue ( ) <EOL> self . assertIn ( "<STR_LIT>" , output ) <EOL> self . assertIn ( "<STR_LIT>" , output ) </s>
