示例工程 19-threading-gil-multiprocessing

19-threading-gil-multiprocessing/05-pickle-constraint/test_pickle_constraint.py

python · 0.6 KB

"""test_pickle_constraint.py"""
 
from __future__ import annotations
 
import threading
from concurrent.futures import ProcessPoolExecutor
 
import pytest
from pickle_constraint import identity, main
 
 
def test_lock_is_not_picklable() -> None:
    with ProcessPoolExecutor(max_workers=1) as pool, pytest.raises(TypeError, match="pickle"):
        pool.submit(identity, threading.Lock()).result()
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "error: TypeError" in out
    assert "mentions pickle: True" in out