def test_runsUntilSyncCallback(self): """ L{react} returns quickly if the L{Deferred} returned by the function it is passed has already been called back at the time it is returned. """ def main(reactor): return succeed(None) r = _FakeReactor() react(r, main, []) self.assertEqual(r.seconds(), 0)
def test_arguments(self): """ L{react} passes the elements of the list it is passed as positional arguments to the function it is passed. """ args = [] def main(reactor, x, y, z): args.extend((x, y, z)) return succeed(None) r = _FakeReactor() react(r, main, [1, 2, 3]) self.assertEqual(args, [1, 2, 3])
def test_runsUntilAsyncCallback(self): """ L{react} runs the reactor until the L{Deferred} returned by the function it is passed is called back, then stops it. """ timePassed = [] def main(reactor): finished = Deferred() reactor.callLater(1, timePassed.append, True) reactor.callLater(2, finished.callback, None) return finished r = _FakeReactor() react(r, main, []) self.assertEqual(timePassed, [True]) self.assertEqual(r.seconds(), 2)
def test_singleStopCallback(self): """ L{react} doesn't try to stop the reactor if the L{Deferred} the function it is passed is called back after the reactor has already been stopped. """ def main(reactor): reactor.callLater(1, reactor.stop) finished = Deferred() reactor.addSystemEventTrigger( 'during', 'shutdown', finished.callback, None) return finished r = _FakeReactor() react(r, main, []) self.assertEqual(r.seconds(), 1)
def test_runsUntilSyncErrback(self): """ L{react} returns quickly if the L{Deferred} returned by the function it is passed has already been errbacked at the time it is returned. """ class ExpectedException(Exception): pass def main(reactor): return fail(ExpectedException()) r = _FakeReactor() react(r, main, []) self.assertEqual(r.seconds(), 0) errors = self.flushLoggedErrors(ExpectedException) self.assertEqual(len(errors), 1)
def test_singleStopCallback(self): """ L{react} doesn't try to stop the reactor if the L{Deferred} the function it is passed is called back after the reactor has already been stopped. """ def main(reactor): reactor.callLater(1, reactor.stop) finished = Deferred() reactor.addSystemEventTrigger('during', 'shutdown', finished.callback, None) return finished r = _FakeReactor() react(r, main, []) self.assertEqual(r.seconds(), 1)
def test_runsUntilAsyncErrback(self): """ L{react} runs the reactor until the L{Deferred} returned by the function it is passed is errbacked, then it stops the reactor and reports the error. """ class ExpectedException(Exception): pass def main(reactor): finished = Deferred() reactor.callLater(1, finished.errback, ExpectedException()) return finished r = _FakeReactor() react(r, main, []) errors = self.flushLoggedErrors(ExpectedException) self.assertEqual(len(errors), 1)
def test_singleStopErrback(self): """ L{react} doesn't try to stop the reactor if the L{Deferred} the function it is passed is errbacked after the reactor has already been stopped. """ class ExpectedException(Exception): pass def main(reactor): reactor.callLater(1, reactor.stop) finished = Deferred() reactor.addSystemEventTrigger( 'during', 'shutdown', finished.errback, ExpectedException()) return finished r = _FakeReactor() react(r, main, []) self.assertEqual(r.seconds(), 1) errors = self.flushLoggedErrors(ExpectedException) self.assertEqual(len(errors), 1)
def test_singleStopErrback(self): """ L{react} doesn't try to stop the reactor if the L{Deferred} the function it is passed is errbacked after the reactor has already been stopped. """ class ExpectedException(Exception): pass def main(reactor): reactor.callLater(1, reactor.stop) finished = Deferred() reactor.addSystemEventTrigger('during', 'shutdown', finished.errback, ExpectedException()) return finished r = _FakeReactor() react(r, main, []) self.assertEqual(r.seconds(), 1) errors = self.flushLoggedErrors(ExpectedException) self.assertEqual(len(errors), 1)
d = self.callRemote(Count) d.addCallback(display, self.identifier) counts.append(d) gatherResults(counts).chainDeferred(self.finished) def makeRoutes(proto, router): router.bindRoute(proto, None).connectTo(None) finish = [] for i in range(3): client = CountClient(i) finish.append(connect(proto, router, client)) finish.append(client.finished) return gatherResults(finish) def main(reactor): router = Router() cc = ClientCreator(reactor, AMP, router) d = cc.connectTCP('localhost', 7805) d.addCallback(makeRoutes, router) return d if __name__ == '__main__': from twisted.internet import reactor react(reactor, main, [])
from twisted.internet.protocol import ClientCreator from twisted.cred.credentials import UsernamePassword from twisted.protocols.amp import AMP from epsilon.react import react from epsilon.ampauth import login from auth_server import Add def add(proto): return proto.callRemote(Add, left=17, right=33) def display(result): print result def main(reactor): cc = ClientCreator(reactor, AMP) d = cc.connectTCP('localhost', 7805) d.addCallback(login, UsernamePassword("testuser", "examplepass")) d.addCallback(add) d.addCallback(display) return d if __name__ == '__main__': from twisted.internet import reactor react(reactor, main, [])
def ampBoxReceived(self, box): msg(str(box)) def stopReceivingBoxes(self, reason): pass def sendBox(printer): printer.sender.sendBox(Box({'foo': 'bar'})) return deferLater(reactor, 1, lambda: None) def main(reactor, username, password): startLogging(stdout) router = Router() proto = AMP(router) router.bindRoute(proto, None).connectTo(None) cc = ClientCreator(reactor, lambda: proto) d = cc.connectTCP(username.split('@')[1], 7805) d.addCallback(login, UsernamePassword(username, password)) d.addCallback( connectRoute, router, BoxPrinter(), u'http://divmod.org/ns/echo') d.addCallback(sendBox) return d if __name__ == '__main__': react(reactor, main, [raw_input('Username (localpart@domain): '), getpass('Password: ')])
def ampBoxReceived(self, box): msg(str(box)) def stopReceivingBoxes(self, reason): pass def sendBox(printer): printer.sender.sendBox(Box({'foo': 'bar'})) return deferLater(reactor, 1, lambda: None) def main(reactor, username, password): startLogging(stdout) router = Router() proto = AMP(router) router.bindRoute(proto, None).connectTo(None) cc = ClientCreator(reactor, lambda: proto) d = cc.connectTCP(username.split('@')[1], 7805) d.addCallback(login, UsernamePassword(username, password)) d.addCallback(connectRoute, router, BoxPrinter(), u'http://divmod.org/ns/echo') d.addCallback(sendBox) return d if __name__ == '__main__': react(reactor, main, [raw_input('Username (localpart@domain): '), getpass('Password: ')])