From: Mike Bayer Date: Fri, 26 Nov 2021 23:43:23 +0000 (-0500) Subject: "graceful fetch" test should....clean up gracefully! X-Git-Tag: rel_2_0_0b1~632 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c5dfddfbd405a72d33b72ed097690a6c2c88b3a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git "graceful fetch" test should....clean up gracefully! this test was relying on gc to close out the connection. this would lead to problems with aiosqlite as we invalidate async connetions that aren't gracefully closed, and this test suite was create tables per suite, so we'd get into a spot where a new sqlite memory connection without the tables would get set up. would only occur for full test run + -n2 + C extensions + python 3.7, but we assume that is all related to just getting gc to trigger or not trigger at exactly the right moment in this situation. confirmed if we add a gc.collect() to the test without explcitly closing out the conenction, the connection is gc'ed and detached, and we get the error reproduced on the subsequent test. Change-Id: Icc9d4bc703f0842c27600f532f34bc4c7d3baf21 --- diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py index c5dd35ce15..4aa932b471 100644 --- a/test/sql/test_resultset.py +++ b/test/sql/test_resultset.py @@ -775,31 +775,31 @@ class CursorResultTest(fixtures.TablesTest): users = self.tables.users - conn = testing.db.connect() - keys_lambda = lambda r: r.keys() # noqa: E731 - - for meth in [ - lambda r: r.fetchone(), - lambda r: r.fetchall(), - lambda r: r.first(), - lambda r: r.scalar(), - lambda r: r.fetchmany(), - lambda r: r._getter("user"), - keys_lambda, - lambda r: r.columns("user"), - lambda r: r.cursor_strategy.fetchone(r, r.cursor), - ]: - trans = conn.begin() - result = conn.execute(users.insert(), dict(user_id=1)) - - assert_raises_message( - exc.ResourceClosedError, - "This result object does not return rows. " - "It has been closed automatically.", - meth, - result, - ) - trans.rollback() + with testing.db.connect() as conn: + keys_lambda = lambda r: r.keys() # noqa: E731 + + for meth in [ + lambda r: r.fetchone(), + lambda r: r.fetchall(), + lambda r: r.first(), + lambda r: r.scalar(), + lambda r: r.fetchmany(), + lambda r: r._getter("user"), + keys_lambda, + lambda r: r.columns("user"), + lambda r: r.cursor_strategy.fetchone(r, r.cursor), + ]: + trans = conn.begin() + result = conn.execute(users.insert(), dict(user_id=1)) + + assert_raises_message( + exc.ResourceClosedError, + "This result object does not return rows. " + "It has been closed automatically.", + meth, + result, + ) + trans.rollback() def test_fetchone_til_end(self, connection): result = connection.exec_driver_sql("select * from users")