Skip to content

Commit f845716

Browse files
committed
test_ocsp parallelism
1 parent 595b440 commit f845716

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/unit/test_ocsp.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,64 @@ def overwrite_ocsp_cache(tmpdir):
7777
THIS_DIR = path.dirname(path.realpath(__file__))
7878

7979

80+
@pytest.fixture(autouse=True)
81+
def worker_specific_cache_dir(tmpdir):
82+
"""Create worker-specific cache directory to avoid file lock conflicts in parallel execution."""
83+
import tempfile
84+
85+
# Get worker ID for parallel execution (pytest-xdist)
86+
worker_id = os.environ.get('PYTEST_XDIST_WORKER', 'master')
87+
88+
# Create unique cache directory for this worker
89+
unique_cache_dir = tmpdir.join(f"ocsp_cache_{worker_id}")
90+
unique_cache_dir.mkdir()
91+
92+
# Set environment variable to use worker-specific cache directory
93+
original_cache_dir = os.environ.get("SF_OCSP_RESPONSE_CACHE_DIR")
94+
os.environ["SF_OCSP_RESPONSE_CACHE_DIR"] = str(unique_cache_dir)
95+
96+
# Reset cache to use new directory
97+
from snowflake.connector.ocsp_snowflake import OCSPCache
98+
OCSPCache.reset_cache_dir()
99+
100+
# Also handle the OCSP_RESPONSE_VALIDATION_CACHE to prevent conflicts
101+
try:
102+
from snowflake.connector.cache import SFDictFileCache
103+
import snowflake.connector.ocsp_snowflake as ocsp_module
104+
105+
# Create worker-specific validation cache file
106+
validation_cache_file = tmpdir.join(f"ocsp_validation_cache_{worker_id}.json")
107+
108+
# Create new cache instance for this worker
109+
worker_validation_cache = SFDictFileCache(
110+
file_path=str(validation_cache_file),
111+
entry_lifetime=3600
112+
)
113+
114+
# Store original cache to restore later
115+
original_validation_cache = getattr(ocsp_module, 'OCSP_RESPONSE_VALIDATION_CACHE', None)
116+
117+
# Replace with worker-specific cache
118+
ocsp_module.OCSP_RESPONSE_VALIDATION_CACHE = worker_validation_cache
119+
120+
yield str(unique_cache_dir)
121+
122+
# Restore original validation cache
123+
if original_validation_cache is not None:
124+
ocsp_module.OCSP_RESPONSE_VALIDATION_CACHE = original_validation_cache
125+
126+
except ImportError:
127+
# If modules not available, just yield the directory
128+
yield str(unique_cache_dir)
129+
130+
# Cleanup: restore original cache directory
131+
if original_cache_dir is not None:
132+
os.environ["SF_OCSP_RESPONSE_CACHE_DIR"] = original_cache_dir
133+
else:
134+
os.environ.pop("SF_OCSP_RESPONSE_CACHE_DIR", None)
135+
OCSPCache.reset_cache_dir()
136+
137+
80138
def create_x509_cert(hash_algorithm):
81139
# Generate a private key
82140
private_key = rsa.generate_private_key(

0 commit comments

Comments
 (0)