def test_cleanReactor_delayed_calls_all_active(self): """ It will cancel any delayed calls in the reactor queue. """ reactor.callLater(1, self.ignoreFailure) reactor.callLater(2, self.ignoreFailure) self.assertIsNotEmpty(reactor.getDelayedCalls()) self._cleanReactor() self.assertIsEmpty(reactor.getDelayedCalls())
def test_delayedCallCleanup(self): """Checking to make sure Sessions do not leave extra DelayedCalls. """ from twisted.internet import reactor delayedCallsBeforeSession = repr(reactor.getDelayedCalls()) session = self.site.makeSession() session.touch() session.expire() self.failUnlessEqual(delayedCallsBeforeSession, repr(reactor.getDelayedCalls()))
def check_clean(_): delayed_calls = reactor.getDelayedCalls() if delayed_calls: print "DIRTY" else: print "CLEAN" reactor.stop()
def runtime_info(): delayed = reactor.getDelayedCalls() readers = reactor.getReaders() writers = reactor.getWriters() servers = [] clients = [] other = [] for reader in readers: if isinstance(reader, tcp.Server): servers.append({ 'transport': reader, 'host': reader.getHost(), 'peer': reader.getPeer() }) elif isinstance(reader, tcp.Client): clients.append({ 'transport': reader, 'host': reader.getHost(), 'peer': reader.getPeer() }) else: other.append(reader) return { 'num_clients': len(clients), 'num_servers': len(servers), 'num_other': len(other), 'num_writers': len(writers), 'num_delayed': len(delayed), 'clients': clients, 'servers': servers, 'other': other, 'writers': writers, 'delayed': delayed, }
def umount(self): self.readonly = True if self.shutting_down: trace("shutdown", "called twice") return try: self.shutting_down = True trace("shutdown", "stopping services") yield self.services.stopService() trace("shutdown", "disconnect peers") self.disconnect_all() for k in self.remote.keys(): del self.remote[k] n = 0 for c in reactor.getDelayedCalls(): n += 1 trace("shutdown", "speed-up %s", c) c.reset(0) if n: trace("shutdown", "speed-up wait %d", n) yield reactor.callLater(n / 10, lambda: None) trace("shutdown", "run idle") yield IdleWorker.run() trace("shutdown", "flush inodes") yield self.db(flush_inodes) trace("shutdown", "super") yield super(SqlFuse, self).stop(False) trace("shutdown", "stop DB") yield self.db.stopService() trace("shutdown", "done") except Exception as e: log.err(e, "Shutting down") traceback.print_exc()
def tearDown(self): """!Tear down a test """ delayedCalls = reactor.getDelayedCalls() for call in delayedCalls: call.cancel() return self.dw.close()
def testQuestionLoadSpeed(self): "Check if trivia questions load fast enough." session = self.trivia_plugin.Session() #Create questions for i in range(40000): #There can be a lot of trivia questions question_string = generate_random_sentence(5, 10, 5, 20) answer_string = generate_random_sentence(1, 6, 1, 20) category_string = generate_random_word(10, 30) question = RegularQuestion(question = question_string, answer = answer_string, category = category_string) session.save_or_update(question) session.commit() #Load questions and check the amount of time it takes questions_asked = 5000 before = datetime.now() for i in range(questions_asked): self.trivia_plugin.on_next_question(self.vtkbot, self.channel) timers = reactor.getDelayedCalls() for timer in timers: try: timer.cancel() except: pass after = datetime.now() diff = after - before self.assert_(diff.seconds < questions_asked/20) #Questions need to be asked as fast as possible, to avoid long delays for the trivia players
def shutdown(): global main_xmlrpc_handler global _xmlrpc_listener global _xmlrpc_site try: site = _xmlrpc_site logging.info("shutting down, first closing listening ports...") print("Shutting down, hold on a moment...") yield stop_listening() # This doesn't work, site.session is always empty logging.info("Ports closed, waiting for current sessions to close...") logging.debug("Clients still connected: {}".format(len(site.sessions))) while not len(site.sessions)==0: logging.debug("Waiting, {} sessions still active".format(len(site.sessions))) yield task.deferLater(reactor, 1, lambda _:0, 0) logging.info("No more sessions, waiting for locked hosts...") while not utils.none_waiting(): logging.info("Waiting to shut down, {} hosts still blocked".format(utils.count_waiting())) yield task.deferLater(reactor, 1, lambda _:0, 0) logging.debug("reactor.getDelayedCalls: {}".format([c.func for c in reactor.getDelayedCalls()])) logging.info("All hosts unlocked, waiting 3 more seconds...") yield task.deferLater(reactor, 1, lambda _:0, 0) logging.debug("Waiting 2 more seconds...") yield task.deferLater(reactor, 1, lambda _:0, 0) logging.debug("Waiting 1 more second...") yield task.deferLater(reactor, 1, lambda _:0, 0) logging.info("Continuing shutdown") except: logging.exception("Error in shutdown callback")
def shutdown(): global main_xmlrpc_handler global _xmlrpc_listener global _xmlrpc_site try: site = _xmlrpc_site logging.info("shutting down, first closing listening ports...") print("Shutting down, hold on a moment...") yield stop_listening() # This doesn't work, site.session is always empty logging.info("Ports closed, waiting for current sessions to close...") logging.debug("Clients still connected: {}".format(len(site.sessions))) while not len(site.sessions) == 0: logging.debug("Waiting, {} sessions still active".format( len(site.sessions))) yield task.deferLater(reactor, 1, lambda _: 0, 0) logging.info("No more sessions, waiting for locked hosts...") while not utils.none_waiting(): logging.info("Waiting to shut down, {} hosts still blocked".format( utils.count_waiting())) yield task.deferLater(reactor, 1, lambda _: 0, 0) logging.debug("reactor.getDelayedCalls: {}".format( [c.func for c in reactor.getDelayedCalls()])) logging.info("All hosts unlocked, waiting 3 more seconds...") yield task.deferLater(reactor, 1, lambda _: 0, 0) logging.debug("Waiting 2 more seconds...") yield task.deferLater(reactor, 1, lambda _: 0, 0) logging.debug("Waiting 1 more second...") yield task.deferLater(reactor, 1, lambda _: 0, 0) logging.info("Continuing shutdown") except: logging.exception("Error in shutdown callback")
def tearDown(self): for dc in reactor.getDelayedCalls(): dc.cancel() res = final_checks(self.wallet_services, self.cj_amount, self.manager.final_psbt.get_fee(), self.ssb, self.rsb) assert res, "final checks failed"
def tearDown(self): super(DNSQueryMonitoringProtocolTestCase, self).tearDown() # There doesn't seem to be any sane way to avoid the delayed call to # maybeParseConfig: https://twistedmatrix.com/trac/ticket/3745 for call in reactor.getDelayedCalls(): if call.func.func_name == 'maybeParseConfig': call.cancel()
def print_runtime_info(sig, frame): if sig in [signal.SIGUSR1, signal.SIGUSR2]: delayed = reactor.getDelayedCalls() readers = reactor.getReaders() writers = reactor.getWriters() clients = [] http_conn_num = 0 for reader in readers: if isinstance(reader, twisted.internet.tcp.Server): clients.append(reader.getPeer()) if isinstance(reader, twisted.internet.tcp.Client): http_conn_num += 1 log.msg( "[Clients: %(client_num)s] [HTTP Conns: %(http_conn_num)s] " "[Readers: %(reader_num)s] [Writers: %(writer_num)s] " "[DelayedCalls: %(delayed_num)s]" % { "client_num": len(clients), "http_conn_num": http_conn_num, "reader_num": len(readers), "writer_num": len(writers), "delayed_num": len(delayed), } ) log.msg("[Connected Clients]: %s" % clients) if sig == signal.SIGUSR2: for d in delayed: log.msg("SIGUSR2[delayed]: %s" % d) for r in readers: log.msg("SIGUSR2[reader]: %s" % r) for w in writers: log.msg("SIGUSR2[writer]: %s" % w)
def checkSessionCreated(request): cookie = request.getCookie(fresources.COOKIE_NAME) self.failIf(cookie is None) sessionID = base64.b64decode(cookie).split(':')[0] session = self.site.sessions.get(sessionID, None) self.failIf(session is None) for d in reactor.getDelayedCalls(): d.cancel()
def test_adpay_task_call(self): # Disabled with patch('adpay.stats.consts.CALCULATE_PAYMENTS_PERIODICALLY', 0): yield stats_tasks.adpay_task() ret = reactor.getDelayedCalls() self.assertEqual(len(ret), 0) # Enabled with patch('adpay.stats.consts.CALCULATE_PAYMENTS_PERIODICALLY', 1): yield stats_tasks.adpay_task() ret = reactor.getDelayedCalls() self.assertEqual(len(ret), 1) call_time = ret[0].getTime() self.assertGreaterEqual(call_time, time.time()) ret[0].cancel()
def stop(self): for dc in reactor.getDelayedCalls(): dc.cancel() d1 = defer.maybeDeferred(self.listener_ws.stopListening) d2 = defer.maybeDeferred(self.listener_rpc.stopListening) if self.client_connector: self.client_connector.disconnect() # only fire if everything is finished: return defer.gatherResults([d1, d2])
def tearDown(self): pending = reactor.getDelayedCalls() active = bool(pending) for p in pending: print p if p.active(): print "cancel" p.cancel() print "fini"
def _cleanPending(self): """Cancel all pending calls and return their string representations. Delayed calls belonging to crochet and twisted internals are ignored. """ for call in reactor.getDelayedCalls(): if call.active() and not call_belongs_to_internals(call): yield call call.cancel()
def setUp(self): self.finished = 0 self.counter = 0 self.timers = {} if not hasattr(reactor, "getDelayedCalls"): return for t in reactor.getDelayedCalls(): t.cancel() reactor.iterate() # flush timers
def tearDown(self): yield self.client.remove('test', CF) yield self.client.remove('test2', CF) yield self.client.remove('test', SCF) yield self.client.remove('test2', SCF) self.cmanager.shutdown() for c in reactor.getDelayedCalls(): c.cancel() reactor.removeAll()
def setup_cj(): load_test_config() jm_single().config.set('POLICY', 'tx_broadcast', 'self') jm_single().bc_interface.tick_forward_chain_interval = 5 jm_single().bc_interface.simulate_blocks() yield None # teardown for dc in reactor.getDelayedCalls(): dc.cancel()
def clientConnectionLost(self, connector, reason): logging.warning("Lost connection: {0}".format(reason)) #Stop pending probe requests - new requests will be started upon reconnection. for delayed_call in reactor.getDelayedCalls(): delayed_call.cancel() #Any connection loss is failure; reconnect. protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def _reactorQueueToString(self): """ Return a string representation of all delayed calls from reactor queue. """ result = [] for delayed in reactor.getDelayedCalls(): result.append(str(delayed.func)) return '\n'.join(result)
def checkTimers(self): l1 = self.timers.values() l2 = list(reactor.getDelayedCalls()) missing = [] for dc in l1: if dc not in l2: missing.append(dc) if missing: self.finished = 1 self.failIf(missing, "Should have been missing no calls, instead was missing " + repr(missing))
def test_configure_tasks(self): stats_tasks.configure_tasks() ret = reactor.getDelayedCalls() self.assertEqual(len(ret), 2) call_time = ret[0].getTime() self.assertGreaterEqual(call_time, time.time()) ret[0].cancel() ret[1].cancel()
def clean_reactor(): """ Cleans the reactor of any uncalled DelayedCall objects created by reactor.callLater(). This is useful for tests that run without the reactor and do not actually need the DelayedCall to run. """ # clean reactor for call in reactor.getDelayedCalls(): call.cancel()
def _delayed_calls_done(self): # We're done when the only remaining DelayedCalls fire after threshold. # (These will be associated with the test timeout, or else they *should* # cause an unclean reactor error because the test should have waited for # them.) threshold = time.time() + 10 for delayed in reactor.getDelayedCalls(): if delayed.getTime() < threshold: return False return True
def setup_cj(): load_test_config() jm_single().config.set('POLICY', 'tx_broadcast', 'self') jm_single().bc_interface.tick_forward_chain_interval = 5 jm_single().bc_interface.simulate_blocks() #see note in cryptoengine.py: cryptoengine.BTC_P2WPKH.VBYTE = 100 yield None # teardown for dc in reactor.getDelayedCalls(): dc.cancel()
def tearDown(self): """Cleanup method after each ``test_*`` method runs; removes timed out connections on the reactor and clears the :ivar:`transport`. """ self.transport.clear() # Clear bytes from the transport. for delay in reactor.getDelayedCalls(): try: delay.cancel() except (AlreadyCalled, AlreadyCancelled): pass
def checkSessionID(request): # The auth is not valid anymore and has been renewed, # but the session should stay the same cookie = request.getCookie(fresources.COOKIE_NAME) self.failIf(cookie is None) sessionID, authExpiracy, none = \ base64.b64decode(cookie).split(':') self.assertEquals(authExpiracy, '0') self.assertEquals(sessionID, self.sessionID) for d in reactor.getDelayedCalls(): d.cancel()
def tearDown(self): super(TXACMETestCase, self).tearDown() # Make sure the main reactor is clean after each test. junk = [] for delayed_call in reactor.getDelayedCalls(): junk.append(delayed_call.func) delayed_call.cancel() if junk: raise AssertionError('Reactor is not clean. DelayedCalls: %s' % (junk, ))
def tearDown(self): self.clean_out_wallet_files() for dc in reactor.getDelayedCalls(): if not dc.cancelled: dc.cancel() d1 = defer.maybeDeferred(self.listener_ws.stopListening) d2 = defer.maybeDeferred(self.listener_rpc.stopListening) if self.client_connector: self.client_connector.disconnect() # only fire if everything is finished: return defer.gatherResults([d1, d2])
def handle_refuse(self, message): """This method should be overridden when implementing a protocol. This method is always executed when the agent receives a FIPA_REFUSE type message :param message: FIPA-ACL message """ self.received_qty += 1 if self.received_qty == self.cfp_qty: delayed_calls = reactor.getDelayedCalls() for call in delayed_calls: call.cancel()
def stop(self): from twisted.internet import reactor # Kill Binance library's Twisted server reactor.callFromThread(lambda: reactor.stop()) for p in reactor.getDelayedCalls(): if p.active(): p.cancel() new_loop = asyncio.new_event_loop() new_loop.run_until_complete(super().stop())
def handle_refuse(self, message): """Este método deve ser sobrescrito quando na implementação do protocolo, sendo executado sempre que o agente receber uma mensagem do tipo FIPA_REFUSE :param message: Mensagem FIPA-ACL """ self.received_qtd += 1 if self.received_qtd == self.cfp_qtd: delayed_calls = reactor.getDelayedCalls() for call in delayed_calls: call.cancel()
def die(self): # we don't want zombie thread do we ? ;) print "Del SocksTools" # dirty but yeah for i in xrange(0,2): for calls in reactor.getDelayedCalls(): if calls.func == self._twistedLoop and calls.active(): print "cancel", calls try: calls.cancel() except Exception as e: print e time.sleep(1) for port, values in self.proxies.items(): print "close port ", port values[3].reply('Closing proxy on port '+str(port)) values[5].stopListening() values[4].closeConnection() for calls in reactor.getDelayedCalls(): if calls.func == self._twistedLoop and calls.active(): print "cancel", calls try: calls.cancel() except Exception as e: print e
def cleanSessions(self, arg): # # twisted Session code has leftovers : disable the hanging delayed call warnings # of trial by nuking all what's left. # pending = reactor.getDelayedCalls() if pending: for p in pending: if p.active(): # print "still pending:" + str(p) p.cancel() return arg
def stop_reactor(): """Stop the reactor and join the reactor thread until it stops. Call this function in teardown at the module or package level to reset the twisted system after your tests. You *must* do this if you mix tests using these tools and tests using twisted.trial. """ global _twisted_thread reactor.stop() reactor_thread.join() for p in reactor.getDelayedCalls(): if p.active(): p.cancel() _twisted_thread = None
def advanceTime(seconds): """ Advance the reactor time by the number of seconds or partial seconds specified. """ yield _pump() now = reactor.seconds() for call in reactor.getDelayedCalls(): currentSecondsFromNow = call.getTime() - now newSecondsFromNow = max(0, currentSecondsFromNow - seconds) call.reset(newSecondsFromNow) # give the reactor a chance to run calls we're brought forward: yield _pump()
def tearDown(self): """Cleanup method for removing timed out connections on the reactor. This seems to be the solution for the dirty reactor due to ``DelayedCall``s which is mentioned at the beginning of this file. There doesn't seem to be any documentation anywhere which proposes this solution, although this seems to solve the problem. """ for delay in reactor.getDelayedCalls(): try: delay.cancel() except (AlreadyCalled, AlreadyCancelled): pass
def cleanup_reactor(force=False): log.debug('comptest', 'running cleanup_reactor...') delayed = reactor.getDelayedCalls() for dc in delayed: dc.cancel() # the rest is taken from twisted trial... sels = reactor.removeAll() if sels: log.info('comptest', 'leftover selectables...: %r %r' % (sels, reactor.waker)) for sel in sels: if interfaces.IProcessTransport.providedBy(sel): sel.signalProcess('KILL')
def shutdown(self): for call in reactor.getDelayedCalls(): if call.func == self.send_it: call.cancel() if self.test == False: if self.resend_notify_loop.running: self.resend_notify_loop.stop() if self.check_valid_loop.running: self.check_valid_loop.stop() '''Make sure we send out the byebye notifications.''' for st in self.known: if self.known[st]['MANIFESTATION'] == 'local': self.doByebye(st)
def shutdown(self): for call in reactor.getDelayedCalls(): if call.func == self.send_it: call.cancel() if self.resend_notify_loop.running: self.resend_notify_loop.stop() if self.check_valid_loop.running: self.check_valid_loop.stop() '''Make sure we send out the byebye notifications.''' if self.test == False: for st in self.known: if self.known[st]['MANIFESTATION'] == 'local': self.doByebye(st)
def process_metrics(self): metrics = [] # Compute frequency of measurements if 'packet_time' in self.metrics and self.metrics[ 'packet_time'] is not None: self.metrics.setdefault('packet_starttime', self.metrics.packet_time) # Convert nanos to seconds packet_duration = ( self.metrics.packet_time - self.metrics.packet_starttime) / 1000.0 / 1000.0 / 1000.0 packet_duration = packet_duration or self.metrics.starttime if packet_duration != 0: packet_frequency = self.metrics.tx_count / float( packet_duration) else: packet_frequency = 0.0 metrics.append('measurements: %.02f Hz' % packet_frequency) # Reset for next round self.metrics.packet_starttime = self.metrics.packet_time # Compute frequency of transactions now = time.time() transaction_duration = now - self.metrics.starttime if transaction_duration != 0: transaction_frequency = self.metrics.tx_count / float( transaction_duration) else: transaction_frequency = 0.0 metrics.append('transactions: %.02f tps' % transaction_frequency) # Reset for next round self.metrics.tx_count = 0 self.metrics.starttime = now # Add information from the Twisted reactor pending_calls = reactor.getDelayedCalls() pending_count = len(pending_calls) #metrics.append('pending: %d' % pending_count) metrics_info = ', '.join(metrics) log.info('[{realm:12s}] {metrics_info}', realm=self.channel.realm, metrics_info=metrics_info)
def test_cleanReactor_delayed_calls_some_called(self): """ It will not break if a call is already called and will continue canceling the . """ delayed_call_1 = reactor.callLater(1, self.ignoreFailure) delayed_call_2 = reactor.callLater(2, self.ignoreFailure) # Fake that deferred was called and make sure it is first in the list # so that we can can check that the operation will continue. delayed_call_1.called = True self.assertEqual( [delayed_call_1, delayed_call_2], reactor.getDelayedCalls()) self._cleanReactor() self.assertTrue(delayed_call_2.cancelled) # Since we are messing with the reactor, we are leaving it in an # inconsistent state as no called delayed call should be part of the # list... since when called, the delayed called is removed right # away, yet we are not removing it but only faking its call. delayed_call_1.called = False self._cleanReactor() self.assertIsEmpty(reactor.getDelayedCalls())
def setUp(self): self.finished = 0 self.counter = 0 self.timers = {} # ick. Sometimes there are magic timers already running: # popsicle.Freezer.tick . Kill off all such timers now so they won't # interfere with the test. Of course, this kind of requires that # getDelayedCalls already works, so certain failure modes won't be # noticed. if not hasattr(reactor, "getDelayedCalls"): return for t in reactor.getDelayedCalls(): t.cancel() reactor.iterate() # flush timers
def parse_message(self, message): """ Right now, Scripty won't support many commands. Hopefully this will change in the future :-) """ message = message.split() length = len(message) if length <= 0 or self.bot_name not in message[0]: return None if length == 1: self.respond('Yes, can I help you?') elif 'stop' in message[1]: for call_object in reactor.getDelayedCalls(): try: call_object.cancel() except AlreadyCalled: continue self.respond('Fine, be that way. I can really feel the love :/') self.display_title('Nothing...') self.set_out_of_show() elif self.get_show_status(): self.respond("Shhhh! I'm trying to read the show!") self.respond('so rude.') elif ('help' or 'Help' or 'HELP') in message[1]: self.respond(self.help_message) elif 'play' in message[1]: if length == 2: self.respond("Hey, you've gotta give me a filename to play!") self.respond("Here are the available options: %s" % self.get_available_files()) else: self.respond("Got it!") self.play(message[2]) elif 'list' in message[1] and length >= 3 and 'files' in message[2]: self.respond("Alright, here's what I have: %s " % self.get_available_files()) elif 'set' in message[1] and length >= 4 and 'delay' in message[2]: self.respond('Setting delay between lines to %s seconds' % message[3]) self.set_delay_time(int(message[3])) else: self.respond("Uhhh, what'd you say?")
def shutdown(self): '''Shutdowns the server :class:`SSDPServer` and sends out the bye bye notifications via method :meth:`doByebye`.''' for call in reactor.getDelayedCalls(): if call.func == self.send_it: call.cancel() if not self.test: if self.resend_notify_loop.running: self.resend_notify_loop.stop() if self.check_valid_loop.running: self.check_valid_loop.stop() for st in self.known: if self.known[st]['MANIFESTATION'] == 'local': self.doByebye(st)