ialarmedalien commented on code in PR #4464:
URL: https://github.com/apache/iceberg-python/pull/4464#discussion_r4653444735


##########
tests/catalog/test_sql.py:
##########
@@ -288,6 +294,129 @@ def test_sql_catalog_multiple_close_calls(self, 
catalog_sqlite: SqlCatalog) -> N
         # Second close should not raise an exception
         catalog_sqlite.close()
 
+    def test_sql_catalog_cross_thread_sqlite_close(self, warehouse: Path, 
caplog: pytest.LogCaptureFixture) -> None:
+        """Closing a SQLite SqlCatalog on a different thread than the one that 
created succeeds."""
+        gc.collect()
+
+        def create_in_worker() -> SqlCatalog:
+            return SqlCatalog("test", uri="sqlite:///:memory:", 
warehouse=f"file://{warehouse}")
+
+        with ThreadPoolExecutor(max_workers=1) as executor:
+            catalog = executor.submit(create_in_worker).result()
+
+        with warnings.catch_warnings(record=True) as caught_warnings:
+            warnings.simplefilter("always", ResourceWarning)
+            with caplog.at_level(logging.ERROR, logger="sqlalchemy.pool"):
+                catalog.close()
+
+            pool_errors = [
+                (record.exc_info[0], str(record.exc_info[1]))
+                for record in caplog.records
+                if record.exc_info and record.exc_info[0] is not None and 
record.exc_info[1] is not None
+            ]
+            caplog.clear()
+            del catalog
+            gc.collect()
+
+        assert pool_errors == []
+        assert [str(w.message) for w in caught_warnings] == []
+
+
[email protected]("uri", ["sqlite:///:memory:", 
"sqlite+pysqlite:///:memory:"])
+def test_sql_catalog_sqlite_memory_preserves_thread_isolation(
+    warehouse: Path, caplog: pytest.LogCaptureFixture, uri: str
+) -> None:
+    """check_same_thread=False keeps SingletonThreadPool isolation, and only 
affects close and garbage collection."""
+    gc.collect()
+    catalog = SqlCatalog("test", uri=uri, warehouse=f"file://{warehouse}")
+    assert isinstance(catalog.engine.pool, SingletonThreadPool)
+
+    catalog.create_namespace("ns")
+    assert catalog.list_namespaces() == [("ns",)]
+
+    # worker thread cannot see the main thread DB
+    with ThreadPoolExecutor(max_workers=1) as executor:
+        with pytest.raises(OperationalError, match="no such table"):
+            executor.submit(catalog.list_namespaces).result()
+
+    # closing the main thread connection also closes the worker thread's 
connection
+    with warnings.catch_warnings(record=True) as caught_warnings:

Review Comment:
   Note: these checks, which ensure that cross-thread close() and garbage 
collection complete successfully, are the same as those performed in the 
previous test (`test_sql_catalog_cross_thread_sqlite_close`). Depending on what 
the policy is on repetition and what a single test should cover, I can delete 
the previous test / delete the close/garbage collection code in this test / etc.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to