import time
import queue
import pytest
-import shutil
-import tempfile
import threading
import subprocess as sp
@pytest.mark.slow
-def test_multiprocess_close(dsn):
+def test_multiprocess_close(dsn, tmpdir):
# Check the problem reported in psycopg2#829
# Subprocess gcs the copy of the fd after fork so it closes connection.
module = f"""\
import psycopg3
def thread():
- conn = psycopg3.connect({repr(dsn)})
+ conn = psycopg3.connect({dsn!r})
curs = conn.cursor()
for i in range(10):
curs.execute("select 1")
t.join()
"""
- dir = tempfile.mkdtemp()
- try:
- with open(os.path.join(dir, "mptest.py"), "w") as f:
- f.write(module)
- env = dict(os.environ)
- env["PYTHONPATH"] = dir + os.pathsep + env.get("PYTHONPATH", "")
- # TODO: debug this. Importing c module fails on travis in this scenario
- env.pop("PSYCOPG3_IMPL", None)
- out = sp.check_output(
- [sys.executable, "-c", script], stderr=sp.STDOUT, env=env
- ).decode("utf8", "replace")
- assert out == "", out.strip().splitlines()[-1]
- finally:
- shutil.rmtree(dir, ignore_errors=True)
+ with (tmpdir / "mptest.py").open("w") as f:
+ f.write(module)
+ env = dict(os.environ)
+ env["PYTHONPATH"] = tmpdir + os.pathsep + env.get("PYTHONPATH", "")
+ # TODO: debug this. Importing c module fails on travis in this scenario
+ env.pop("PSYCOPG3_IMPL", None)
+ out = sp.check_output(
+ [sys.executable, "-c", script], stderr=sp.STDOUT, env=env
+ ).decode("utf8", "replace")
+ assert out == "", out.strip().splitlines()[-1]
@pytest.mark.slow