def testAddProcessSanityCheck(self): reactor = Reactor() try: reactor.addProcess(lambda: None, prio=10) self.fail('Should not come here.') except ValueError, e: self.assertEquals('Invalid priority: 10', str(e))
def testProcessWithSuspend(self): reactor = Reactor() trace = [] def p(): trace.append(reactor.suspend()) trace.append('suspending') yield trace.append('resuming') yield callback = p().next reactor.addProcess(callback) reactor.addProcess(lambda: None) reactor.step() self.assertEquals([callback], reactor._suspended.keys()) self.assertEquals([callback, 'suspending'], trace) reactor.step() self.assertEquals([callback], reactor._suspended.keys()) self.assertEquals([callback, 'suspending'], trace) reactor.resumeProcess(handle=callback) reactor.step() self.assertEquals([], reactor._suspended.keys()) self.assertEquals([callback, 'suspending', 'resuming'], trace)
def testAddProcessGenerator(self): reactor = Reactor() trace = [] @identify def p(): this = yield yield # wait after this trace.append(1) yield trace.append(2) reactor.removeProcess(this.next) yield trace.append('should_not_happen') yield reactor.addProcess(p().next) reactor.step() self.assertEquals([1], trace) reactor.step() self.assertEquals([1, 2], trace) reactor.addProcess(lambda: None) reactor.step() self.assertEquals([1, 2], trace)
def normalAsync(): this = yield reactor.addProcess(this.next) yield # waiting for realReactor's step() result = asProcess(a()) yield # waiting for another step() reactor.removeProcess(this.next) raise Exception(('Stop', result))
def testProcessAddsNotWhenAlreadyInThere(self): reactor = Reactor() aProcess = lambda: None reactor.addProcess(aProcess) try: reactor.addProcess(aProcess) self.fail('Should not come here.') except ValueError, e: self.assertEquals('Process is already in processes', str(e))
def testExceptionsInProcessNotSuppressed(self): reactor = Reactor() def p(): raise RuntimeError('The Error') reactor.addProcess(p) self.assertEquals([p], reactor._processes.keys()) try: reactor.step() self.fail('Should not come here.') except RuntimeError, e: self.assertEquals('The Error', str(e)) self.assertEquals([], reactor._processes.keys())
def testShutdownWithRemainingProcesses(self): reactor = Reactor() lambdaFunc = lambda: None reactor.addProcess(lambdaFunc) self.assertEquals([lambdaFunc], reactor._processes.keys()) self.assertEquals('Reactor shutdown: terminating %s\n' % lambdaFunc, self.withSTDOUTRedirected(reactor.shutdown)) self.assertEquals([], reactor._processes.keys()) reactor = Reactor() lambdaFunc = lambda: reactor.suspend() reactor.addProcess(lambdaFunc) reactor.step() self.assertEquals([lambdaFunc], reactor._suspended.keys()) self.assertEquals('Reactor shutdown: terminating %s\n' % lambdaFunc, self.withSTDOUTRedirected(reactor.shutdown)) self.assertEquals([], reactor._suspended.keys())
def testAddProcessFunction(self): reactor = Reactor() trace = [] processMe = [1, 2] def p(): try: result = processMe.pop(0) * 2 trace.append(result) except IndexError: reactor.removeProcess() trace.append('removedProcess') reactor.addProcess(p) reactor.step() self.assertEquals([2], trace) reactor.step() self.assertEquals([2, 4], trace) reactor.step() self.assertEquals([2, 4, 'removedProcess'], trace) reactor.addProcess(lambda: None) reactor.step() self.assertEquals([2, 4, 'removedProcess'], trace)
def testProcessPriority(self): reactor = Reactor() trace = [] defaultPass = iter(xrange(99)) def defaultPrio(): trace.append('default_%s' % defaultPass.next()) highPass = iter(xrange(99)) def highPrio(): trace.append('high_%s' % highPass.next()) lowPass = iter(xrange(99)) def lowPrio(): trace.append('low_%s' % lowPass.next()) reactor.addProcess(defaultPrio) # prio will be 0, "very high" reactor.addProcess(highPrio, prio=1) reactor.addProcess(lowPrio, prio=3) reactor.step() self.assertEquals([ 'default_0', ], trace) reactor.step() self.assertEquals(set([ 'default_0', 'high_0', 'default_1', ]), set(trace)) reactor.step() self.assertEquals( set([ 'default_0', 'high_0', 'default_1', 'high_1', 'default_2', ]), set(trace)) reactor.step() self.assertEquals( set([ 'default_0', 'high_0', 'default_1', 'high_1', 'default_2', 'high_2', 'low_0', 'default_3', ]), set(trace))
def testProcessPriority(self): reactor = Reactor() trace = [] defaultPass = iter(xrange(99)) def defaultPrio(): trace.append('default_%s' % defaultPass.next()) highPass = iter(xrange(99)) def highPrio(): trace.append('high_%s' % highPass.next()) lowPass = iter(xrange(99)) def lowPrio(): trace.append('low_%s' % lowPass.next()) reactor.addProcess(defaultPrio) # prio will be 0, "very high" reactor.addProcess(highPrio, prio=1) reactor.addProcess(lowPrio, prio=3) reactor.step() self.assertEquals([ 'default_0', ], trace) reactor.step() self.assertEquals(set([ 'default_0', 'high_0', 'default_1', ]), set(trace)) reactor.step() self.assertEquals(set([ 'default_0', 'high_0', 'default_1', 'high_1', 'default_2', ]), set(trace)) reactor.step() self.assertEquals(set([ 'default_0', 'high_0', 'default_1', 'high_1', 'default_2', 'high_2', 'low_0', 'default_3', ]), set(trace))
self.assertEquals([2, 4, 'removedProcess'], trace) reactor.addProcess(lambda: None) reactor.step() self.assertEquals([2, 4, 'removedProcess'], trace) def testAddProcessSanityCheck(self): reactor = Reactor() try: reactor.addProcess(lambda: None, prio=10) self.fail('Should not come here.') except ValueError, e: self.assertEquals('Invalid priority: 10', str(e)) try: reactor.addProcess(lambda: None, prio=-1) self.fail('Should not come here.') except ValueError, e: self.assertEquals('Invalid priority: -1', str(e)) lambdaFunc = lambda: reactor.suspend() reactor.addProcess(lambdaFunc) reactor.step() try: reactor.addProcess(lambdaFunc) self.fail('Should not come here.') except ValueError, e: self.assertEquals('Process is suspended', str(e)) def testProcessAddsNotWhenAlreadyInThere(self): reactor = Reactor()