def test_resolve_dupe_by_name(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.alt.Foo") clsregistry.add_class("Foo", f1) clsregistry.add_class("Foo", f2) gc_collect() name_resolver, resolver = clsregistry._resolver(f1, MockProp()) resolver = resolver("Foo") assert_raises_message( exc.InvalidRequestError, 'Multiple classes found for path "Foo" in the ' "registry of this declarative base. Please use a " "fully module-qualified path.", resolver, ) resolver = name_resolver("Foo") assert_raises_message( exc.InvalidRequestError, 'Multiple classes found for path "Foo" in the ' "registry of this declarative base. Please use a " "fully module-qualified path.", resolver, )
def profile(queue, func_args): # give testing.db a brand new pool and don't # touch the existing pool, since closing a socket # in the subprocess can affect the parent testing.db.pool = testing.db.pool.recreate() gc_collect() samples = [] max_ = 0 max_grew_for = 0 success = False until_maxtimes = 0 while True: if until_maxtimes >= maxtimes // 5: break for x in range(5): func(*func_args) gc_collect() samples.append( get_num_objects() if get_num_objects is not None else len(get_objects_skipping_sqlite_issue())) if assert_no_sessions: assert len(_sessions) == 0 # queue.put(('samples', samples)) latest_max = max(samples[-5:]) if latest_max > max_: queue.put(( "status", "Max grew from %s to %s, max has " "grown for %s samples" % (max_, latest_max, max_grew_for), )) max_ = latest_max max_grew_for += 1 until_maxtimes += 1 continue else: queue.put(( "status", "Max remained at %s, %s more attempts left" % (max_, max_grew_for), )) max_grew_for -= 1 if max_grew_for == 0: success = True break if not success: queue.put(( "result", False, "Ran for a total of %d times, memory kept " "growing: %r" % (maxtimes, samples), )) else: queue.put(("result", True, "success"))
def test_fragment_ambiguous(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.alt.Foo") f3 = MockClass(base, "bat.alt.Foo") clsregistry.add_class("Foo", f1) clsregistry.add_class("Foo", f2) clsregistry.add_class("Foo", f3) name_resolver, resolver = clsregistry._resolver(f1, MockProp()) gc_collect() assert_raises_message( exc.InvalidRequestError, 'Multiple classes found for path "alt.Foo" in the registry ' "of this declarative base. Please use a fully " "module-qualified path.", resolver("alt.Foo"), ) assert_raises_message( exc.InvalidRequestError, 'Multiple classes found for path "alt.Foo" in the registry ' "of this declarative base. Please use a fully " "module-qualified path.", name_resolver("alt.Foo"), )
def test_pre_ping_weakref_finalizer(self): pool = self._pool_fixture(pre_ping=True) conn = pool.connect() old_dbapi_conn = conn.connection conn.close() eq_(old_dbapi_conn.mock_calls, [call.cursor(), call.rollback()]) self.dbapi.shutdown("execute", stop=True) self.dbapi.restart() conn = pool.connect() dbapi_conn = conn.connection del conn gc_collect() # new connection was reset on return appropriately eq_(dbapi_conn.mock_calls, [call.cursor(), call.rollback()]) # old connection was just closed - did not get an # erroneous reset on return eq_( old_dbapi_conn.mock_calls, [call.cursor(), call.rollback(), call.cursor(), call.close()], )
def test_listener_collection_removed_cleanup(self): from sqlalchemy_1_3.event import registry Target = self._fixture() m1 = Mock() t1 = Target() event.listen(t1, "event_one", m1) key = (id(t1), "event_one", id(m1)) assert key in registry._key_to_collection collection_ref = list(registry._key_to_collection[key])[0] assert collection_ref in registry._collection_to_key t1.dispatch.event_one("t1") del t1 gc_collect() assert key not in registry._key_to_collection assert collection_ref not in registry._collection_to_key
def test_module_reg_cleanout_cls_to_base(self): base = weakref.WeakValueDictionary() f4 = MockClass(base, "single.Blat") clsregistry.add_class("Blat", f4) reg = base["_sa_module_registry"] assert reg["single"] del f4 gc_collect() assert "single" not in reg
def test_module_reg_cleanout_sub_to_base(self): base = weakref.WeakValueDictionary() f3 = MockClass(base, "bat.bar.Hoho") clsregistry.add_class("Hoho", f3) reg = base["_sa_module_registry"] assert reg["bat"]["bar"] del f3 gc_collect() assert "bat" not in reg
def test_subclass(self): class SubTarget(self.Target): pass st = SubTarget() st.dispatch.some_event(1, 2) del st del SubTarget gc_collect() eq_(self.Target.__subclasses__(), [])
def test_stale_state_positive_gc(self): User = self.classes.User s, u1, a1 = self._fixture() s.expunge(u1) del u1 gc_collect() u1 = s.query(User).first() u1.addresses.remove(a1) self._assert_not_hasparent(a1)
def test_cls_not_strong_ref(self): class User(decl.DeferredReflection, fixtures.ComparableEntity, Base): __tablename__ = "users" class Address( decl.DeferredReflection, fixtures.ComparableEntity, Base ): __tablename__ = "addresses" eq_(len(_DeferredMapperConfig._configs), 2) del Address gc_collect() eq_(len(_DeferredMapperConfig._configs), 1) decl.DeferredReflection.prepare(testing.db) assert not _DeferredMapperConfig._configs
def test_resolve(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.alt.Foo") clsregistry.add_class("Foo", f1) clsregistry.add_class("Foo", f2) name_resolver, resolver = clsregistry._resolver(f1, MockProp()) gc_collect() is_(resolver("foo.bar.Foo")(), f1) is_(resolver("foo.alt.Foo")(), f2) is_(name_resolver("foo.bar.Foo")(), f1) is_(name_resolver("foo.alt.Foo")(), f2)
def test_same_module_same_name(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.bar.Foo") clsregistry.add_class("Foo", f1) gc_collect() assert_raises_message( exc.SAWarning, "This declarative base already contains a class with the " "same class name and module name as foo.bar.Foo, and " "will be replaced in the string-lookup table.", clsregistry.add_class, "Foo", f2, )
def test_dupe_classes_back_to_one(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.alt.Foo") clsregistry.add_class("Foo", f1) clsregistry.add_class("Foo", f2) del f2 gc_collect() # registry restores itself to just the one class name_resolver, resolver = clsregistry._resolver(f1, MockProp()) f_resolver = resolver("Foo") is_(f_resolver(), f1) f_resolver = name_resolver("Foo") is_(f_resolver(), f1)
def test_module_reg_cleanout_two_sub(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") clsregistry.add_class("Foo", f1) reg = base["_sa_module_registry"] f2 = MockClass(base, "foo.alt.Bar") clsregistry.add_class("Bar", f2) assert reg["foo"]["bar"] del f1 gc_collect() assert "bar" not in reg["foo"] assert "alt" in reg["foo"] del f2 gc_collect() assert "foo" not in reg.contents
def go(): fn() # warmup, configure mappers, caches, etc. gc_collect() gc_collect() gc_collect() # multiple calls seem to matter # gc.set_debug(gc.DEBUG_COLLECTABLE) try: return fn() # run for real finally: unreachable = gc_collect() assert unreachable <= expected gc_collect()
def test_stale_state_positive_pk_change(self): """Illustrate that we can't easily link a stale state to a fresh one if the fresh one has a PK change (unless we a. tracked all the previous PKs, wasteful, or b. recycled states - time consuming, breaks lots of edge cases, destabilizes the code) """ User = self.classes.User s, u1, a1 = self._fixture() s._expunge_states([attributes.instance_state(u1)]) del u1 gc_collect() u1 = s.query(User).first() # primary key change. now we # can't rely on state.key as the # identifier. new_id = u1.id + 10 u1.id = new_id a1.user_id = new_id s.flush() assert_raises_message( orm_exc.StaleDataError, "can't be sure this is the most recent parent.", u1.addresses.remove, a1, ) # u1.addresses wasn't actually impacted, because the event was # caught before collection mutation eq_(u1.addresses, [a1]) # expire all and we can continue s.expire_all() u1.addresses.remove(a1) self._assert_not_hasparent(a1)
def test_dupe_classes_cleanout(self): # force this to maintain isolation between tests clsregistry._registries.clear() base = weakref.WeakValueDictionary() for i in range(3): f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.alt.Foo") clsregistry.add_class("Foo", f1) clsregistry.add_class("Foo", f2) eq_(len(clsregistry._registries), 11) del f1 del f2 gc_collect() eq_(len(clsregistry._registries), 1)
def test_stale_state_negative(self): User = self.classes.User s, u1, a1 = self._fixture() u2 = User(addresses=[a1]) s.add(u2) s.flush() s._expunge_states([attributes.instance_state(u2)]) del u2 gc_collect() assert_raises_message( orm_exc.StaleDataError, "can't be sure this is the most recent parent.", u1.addresses.remove, a1, ) s.flush() self._assert_hasparent(a1)
def test_no_fns_in_name_resolve(self): base = weakref.WeakValueDictionary() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.alt.Foo") clsregistry.add_class("Foo", f1) clsregistry.add_class("Foo", f2) name_resolver, resolver = clsregistry._resolver(f1, MockProp()) gc_collect() import sqlalchemy_1_3 is_( resolver("__import__('sqlalchemy_1_3.util').util.EMPTY_SET")(), sqlalchemy_1_3.util.EMPTY_SET, ) assert_raises_message( exc.InvalidRequestError, r"When initializing mapper some_parent, expression " r"\"__import__\('sqlalchemy_1_3.util'\).util.EMPTY_SET\" " "failed to locate a name", name_resolver("__import__('sqlalchemy_1_3.util').util.EMPTY_SET"), )
def generate(): objects = s.query(User).options(joinedload(User.addresses)).all() gc_collect() return objects
def generate(): objects = s.query(User).filter(User.id == 7).all() gc_collect() return objects
def assert_no_mappers(): clear_mappers() gc_collect() assert len(_mapper_registry) == 0