def testValues(self): self.result = None def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x * x)) return d def gen(): for i in range(10): d = square_later(i) yield d # .value() gets the result of the deferred assert d.value() == i * i yield d.value() gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == 81 d.addCallback(checkResult) return d
def testValues(self): self.result = None def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x*x)) return d def gen(): for i in range(10): d = square_later(i) yield d # .value() gets the result of the deferred assert d.value() == i*i yield d.value() gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == 81 d.addCallback(checkResult) return d
def testBarfOnNongenerator(self): def nongen(): pass try: nongen = defer_generator(nongen) assert 'not reached' except Exception: pass
def testYieldResultAtFirst(self): self.result = None def gen(): yield 42 gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) assert self.result == 42
def testYieldNothing(self): self.result = 42 def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x*x)) return d def gen(): yield square_later(0) gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == None d.addCallback(checkResult) return d
def testYieldNothing(self): self.result = 42 def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x * x)) return d def gen(): yield square_later(0) gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == None d.addCallback(checkResult) return d
for f in planet.get('flows'): if f.get('name') == avatarId[0]: for c in f.get('components'): if c.get('name') == avatarId[1]: return c print('Could not find component named %s in flow %s' % (avatarId[1], avatarId[0])) return None def get_component_uistate(model, avatarId, component=None, quiet=False): if not component: d = model.callRemote('getPlanetState') yield d planet = d.value() component = find_component(planet, avatarId) if component: d = model.componentCallRemote(component, 'getUIState') yield d try: uistate = d.value() yield uistate except errors.SleepingComponentError: if not quiet: print('Error: Component %s in flow %s is sleeping' % (avatarId[1], avatarId[0])) get_component_uistate = defer_generator(get_component_uistate)
% (avatarId[1], avatarId[0])) return None for f in planet.get('flows'): if f.get('name') == avatarId[0]: for c in f.get('components'): if c.get('name') == avatarId[1]: return c print ('Could not find component named %s in flow %s' % (avatarId[1], avatarId[0])) return None def get_component_uistate(model, avatarId, component=None, quiet=False): if not component: d = model.callRemote('getPlanetState') yield d planet = d.value() component = find_component(planet, avatarId) if component: d = model.componentCallRemote(component, 'getUIState') yield d try: uistate = d.value() yield uistate except errors.SleepingComponentError: if not quiet: print ('Error: Component %s in flow %s is sleeping' % (avatarId[1], avatarId[0])) get_component_uistate = defer_generator(get_component_uistate)
def divide_later(x, y): d = defer.Deferred() def divide(): try: d.callback(x/y) except ZeroDivisionError, e: d.errback(e) reactor.callLater(0.1, divide) return d def gen(): d = divide_later(42, 0) yield d yield d.value() gen = defer_generator(gen) exception_chain = [] def oserrorback(failure): exception_chain.append('oserror') failure.trap(OSError) def zerodivisionerrorback(failure): exception_chain.append('zerodivisionerror') failure.trap(ZeroDivisionError) def runtimeerrorback(failure): exception_chain.append('runtimeerror') failure.trap(RuntimeError)
class TestDefer(testsuite.TestCase): result = None error = None def testYieldResultAtFirst(self): self.result = None def gen(): yield 42 gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) assert self.result == 42 @attr('slow') def testYieldResultAfter(self): self.result = None def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x * x)) return d def gen(): yield square_later(0) yield 42 gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == 42 d.addCallback(checkResult) return d @attr('slow') def testYieldNothing(self): self.result = 42 def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x * x)) return d def gen(): yield square_later(0) gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == None d.addCallback(checkResult) return d @attr('slow') def testValues(self): self.result = None def square_later(x): d = defer.Deferred() reactor.callLater(0.1, lambda: d.callback(x * x)) return d def gen(): for i in range(10): d = square_later(i) yield d # .value() gets the result of the deferred assert d.value() == i * i yield d.value() gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == 81 d.addCallback(checkResult) return d def testBarfOnNongenerator(self): def nongen(): pass try: nongen = defer_generator(nongen) assert 'not reached' except Exception: pass @attr('slow') def testException(self): self.result = None def divide_later(x, y): d = defer.Deferred() def divide(): try: d.callback(x / y) except ZeroDivisionError, e: d.errback(e) reactor.callLater(0.1, divide) return d def gen(): d = divide_later(42, 0) yield d # .value() gets the result of the deferred and raises # if there was an errback try: assert d.value() == 42 except ZeroDivisionError: d = divide_later(42, 1) yield d assert d.value() == 42 yield True gen = defer_generator(gen) d = gen() d.addCallback(lambda x: setattr(self, 'result', x)) def checkResult(res): assert self.result == True d.addCallback(checkResult) return d
def divide(): try: d.callback(x / y) except ZeroDivisionError, e: d.errback(e) reactor.callLater(0.1, divide) return d def gen(): d = divide_later(42, 0) yield d yield d.value() gen = defer_generator(gen) exception_chain = [] def oserrorback(failure): exception_chain.append('oserror') failure.trap(OSError) def zerodivisionerrorback(failure): exception_chain.append('zerodivisionerror') failure.trap(ZeroDivisionError) def runtimeerrorback(failure): exception_chain.append('runtimeerror') failure.trap(RuntimeError)