def test_delayedCallResetToEarlier(self): """ L{DelayedCall.reset()} properly reschedules timer to earlier time """ if hasWindowsSelectorEventLoopPolicy: set_event_loop_policy(WindowsSelectorEventLoopPolicy()) reactor = AsyncioSelectorReactor() timer_called_at = [None] def on_timer(): timer_called_at[0] = reactor.seconds() start_time = reactor.seconds() dc = reactor.callLater(0.5, on_timer) dc.reset(0) reactor.callLater(1, reactor.stop) import io from contextlib import redirect_stderr stderr = io.StringIO() with redirect_stderr(stderr): reactor.run() self.assertEqual(stderr.getvalue(), "") self.assertIsNotNone(timer_called_at[0]) self.assertLess(timer_called_at[0] - start_time, 0.4) if hasWindowsSelectorEventLoopPolicy: set_event_loop_policy(None)
class Application(object): def __init__(self, configuration: Configuration): self.__configuration = configuration self.logger = Logger(__file__) self.__event_loop = asyncio.get_event_loop() self.reactor = AsyncioSelectorReactor(eventloop=self.__event_loop) def run(self): build_information: BuildInformation = self.__configuration.build_information( ) return self.__initialize(name=build_information.name(), version=build_information.version()) def __initialize(self, name: str, version: str) -> None: self.logger.info("Starting {0} VER: {1}".format(name, version)) self.reactor.listenTCP( self.__configuration.port(), ServiceFactory(configuration=self.__configuration, event_loop=self.__event_loop)) asyncio.set_event_loop(self.__event_loop) self.reactor.callLater(seconds=5, f=get_client().start_up) self.reactor.run() async def _initializer(self, reactor: AsyncioSelectorReactor): reactor.listenTCP( self.__configuration.port(), ServiceFactory(configuration=self.__configuration, event_loop=asyncio.get_event_loop())) asyncio.get_event_loop().create_task(coro=get_client().start_up(), name="start-nats") reactor.run()
def test_delayedCallResetToLater(self): """ L{DelayedCall.reset()} properly reschedules timer to later time """ reactor = AsyncioSelectorReactor() timer_called_at = [None] def on_timer(): timer_called_at[0] = reactor.seconds() start_time = reactor.seconds() dc = reactor.callLater(0, on_timer) dc.reset(0.5) reactor.callLater(1, reactor.stop) reactor.run() self.assertIsNotNone(timer_called_at[0]) self.assertGreater(timer_called_at[0] - start_time, 0.4)
def test_noCycleReferencesInCallLater(self): """ L{AsyncioSelectorReactor.callLater()} doesn't leave cyclic references """ gc_was_enabled = gc.isenabled() gc.disable() try: objects_before = len(gc.get_objects()) timer_count = 1000 reactor = AsyncioSelectorReactor() for _ in range(timer_count): reactor.callLater(0, lambda: None) reactor.runUntilCurrent() objects_after = len(gc.get_objects()) self.assertLess((objects_after - objects_before) / timer_count, 1) finally: if gc_was_enabled: gc.enable()
def test_delayedCallResetToLater(self): """ L{DelayedCall.reset()} properly reschedules timer to later time """ if hasWindowsSelectorEventLoopPolicy: set_event_loop_policy(WindowsSelectorEventLoopPolicy()) reactor = AsyncioSelectorReactor() timer_called_at = [None] def on_timer(): timer_called_at[0] = reactor.seconds() start_time = reactor.seconds() dc = reactor.callLater(0, on_timer) dc.reset(0.5) reactor.callLater(1, reactor.stop) reactor.run() self.assertIsNotNone(timer_called_at[0]) self.assertGreater(timer_called_at[0] - start_time, 0.4) if hasWindowsSelectorEventLoopPolicy: set_event_loop_policy(None)