def tearDown(self): """Called after the test method.""" # Reset lazy properties for entity in db.entities.values(): for key in dir(entity): value = getattr(entity, key) if isinstance(value, LazyPropertyDescriptor): value.memory.clear() db_session.__exit__() db.drop_all_tables(with_all_data=True)
def stop_db_session(exc=None): """Stops the last db_session""" # print('==> Stop session', type(exc)) if has_db_session(): exc_type = None tb = None if exc: exc_type, exc, tb = get_exc_info(exc) db_session.__exit__(exc_type, exc, tb)
def stop_db_session(exc=None): """Stops the last db_session""" # print('==> Stop session', type(exc)) if has_db_session(): exc_type = None tb = None if exc: exc_type, tb = type(exc), exc.__traceback__ db_session.__exit__(exc_type, exc, tb)
def checkReactor(self, phase, *_): delayed_calls = reactor.getDelayedCalls() if delayed_calls: self._logger.error("The reactor was dirty during %s:", phase) for dc in delayed_calls: self._logger.error("> %s", dc) dc.cancel() from pony.orm.core import local if local.db_context_counter > 0: self._logger.error("Leftover pony db sessions found!") from pony.orm import db_session for _ in range(local.db_context_counter): db_session.__exit__() has_network_selectables = False for item in reactor.getReaders() + reactor.getWriters(): if isinstance(item, HTTPChannel) or isinstance(item, Client): has_network_selectables = True break if has_network_selectables: # TODO(Martijn): we wait a while before we continue the check since network selectables # might take some time to cleanup. I'm not sure what's causing this. yield deferLater(reactor, 0.2, lambda: None) # This is the same check as in the _cleanReactor method of Twisted's Trial selectable_strings = [] for sel in reactor.removeAll(): if interfaces.IProcessTransport.providedBy(sel): self._logger.error("Sending kill signal to %s", repr(sel)) sel.signalProcess('KILL') selectable_strings.append(repr(sel)) self.assertFalse( selectable_strings, "The reactor has leftover readers/writers during %s: %r" % (phase, selectable_strings)) # Check whether we have closed all the sockets open_readers = reactor.getReaders() for reader in open_readers: self.assertNotIsInstance(reader, BasePort) # Check whether the threadpool is clean tp_items = len(reactor.getThreadPool().working) if tp_items > 0: # Print all stacks to debug this issue self.watchdog.print_all_stacks() self.assertEqual(tp_items, 0, "Still items left in the threadpool")
def checkReactor(self, phase, *_): delayed_calls = reactor.getDelayedCalls() if delayed_calls: self._logger.error("The reactor was dirty during %s:", phase) for dc in delayed_calls: self._logger.error("> %s", dc) dc.cancel() from pony.orm.core import local if local.db_context_counter > 0: self._logger.error("Leftover pony db sessions found!") from pony.orm import db_session for _ in range(local.db_context_counter): db_session.__exit__() has_network_selectables = False for item in reactor.getReaders() + reactor.getWriters(): if isinstance(item, HTTPChannel) or isinstance(item, Client): has_network_selectables = True break if has_network_selectables: # TODO(Martijn): we wait a while before we continue the check since network selectables # might take some time to cleanup. I'm not sure what's causing this. yield deferLater(reactor, 0.2, lambda: None) # This is the same check as in the _cleanReactor method of Twisted's Trial selectable_strings = [] for sel in reactor.removeAll(): if interfaces.IProcessTransport.providedBy(sel): self._logger.error("Sending kill signal to %s", repr(sel)) sel.signalProcess('KILL') selectable_strings.append(repr(sel)) self.assertFalse(selectable_strings, "The reactor has leftover readers/writers during %s: %r" % (phase, selectable_strings)) # Check whether we have closed all the sockets open_readers = reactor.getReaders() for reader in open_readers: self.assertNotIsInstance(reader, BasePort) # Check whether the threadpool is clean tp_items = len(reactor.getThreadPool().working) if tp_items > 0: # Print all stacks to debug this issue self.watchdog.print_all_stacks() self.assertEqual(tp_items, 0, "Still items left in the threadpool")
def pop(sender, *args, **kwargs): while local.db_context_counter: db_session.__exit__()
def pop(request): db_session.__exit__() return request
def ddb(ddbr): """database reset with ddb_sesion""" db_session.__enter__() yield ddbr db_session.__exit__()
def on_error(self, response: http.Response): if pony.orm.core.local.db_session is not None: db_session.__exit__() return response
async def cleanup(self): """Clean the service up before shutting down.""" if self.init_task: self.init_task.cancel() Delay.persist(db) db_session.__exit__()