Skip to content

Commit 174bfa6

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: race condition in unit test for i/o operations
PiperOrigin-RevId: 740542310
1 parent 0214b1b commit 174bfa6

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

tests/unit/aiplatform/test_metadata.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import os
1919
import copy
20+
import sys
2021
from importlib import reload
2122
from unittest import TestCase, mock
2223
from unittest.mock import patch, call
@@ -2075,6 +2076,7 @@ def test_get_experiment_df_passes_experiment_variable(
20752076
_, kwargs = query_experiment_row_mock.call_args_list[0]
20762077
TestCase.assertTrue(self, kwargs["experiment"].name == _TEST_EXPERIMENT)
20772078

2079+
@pytest.mark.skipif(sys.version_info == (3, 9), reason="flaky")
20782080
@pytest.mark.usefixtures(
20792081
"get_experiment_mock",
20802082
"list_tensorboard_time_series_mock",

tests/unit/vertexai/test_offline_store.py

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
Feature,
2626
)
2727

28+
pytestmark = pytest.mark.skipif(
29+
sys.version_info == (3, 8), reason="bigframes is not supported in Python 3.8"
30+
)
31+
2832
try:
2933
import pandas as pd
3034
except ImportError:

tests/unit/vertexai/test_tokenization.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io
1818
import os
1919
import shutil
20+
import uuid
2021
import tempfile
2122
from typing import List
2223
from unittest import mock
@@ -462,18 +463,41 @@ def test_image_mime_types(self, mock_sp_processor):
462463
e.match("Tokenizers do not support Image content type.")
463464

464465

466+
@pytest.mark.skip("flaky")
465467
class TestModelLoad:
468+
"""Unit tests for the model loading."""
469+
466470
def setup_method(self):
467-
model_dir = os.path.join(tempfile.gettempdir(), "vertexai_tokenizer_model")
468-
if os.path.exists(model_dir):
469-
shutil.rmtree(model_dir)
470-
if not os.path.exists(model_dir):
471-
os.mkdir(model_dir)
471+
# create the directory robustly to avoid FileExistsError due to race condition.
472+
# The race condition is caused by parallel test execution.
473+
# 1. if os.path.exists(model_dir) runs, assume it was `True`
474+
# 2. shutil.rmtree(model_dir) start executing
475+
# 3. if not os.path.exists(model_dir) runs. At this precise moment,
476+
# `rmtree` has finished, so the check returns `True`
477+
# 4. The code proceeds to create execute os.mkdir(model_dir)
478+
# 5. But between step 3 and 4, another process on a different python
479+
# version manages to re-create the model_dir
480+
# 6. When `os.mkdir(model_dir)` finally executes, the directory now exists.
481+
482+
uuid_str = str(uuid.uuid4())
483+
self.model_dir = os.path.join(
484+
tempfile.gettempdir(),
485+
f'vertexai_tokenizer_model_{uuid_str}')
486+
if os.path.exists(self.model_dir):
487+
try:
488+
shutil.rmtree(self.model_dir)
489+
except OSError as e:
490+
print(f'Warning: Failed to remove directory {self.model_dir} in setup: {e}')
491+
if not os.path.exists(self.model_dir):
492+
try:
493+
os.mkdirs(self.model_dir)
494+
except OSError as e:
495+
print(f'Error: Failed to create directory {self.model_dir} in setup: {e}')
496+
raise
472497

473498
def get_cache_path(self, file_url: str):
474-
model_dir = os.path.join(tempfile.gettempdir(), "vertexai_tokenizer_model")
475499
filename = hashlib.sha1(file_url.encode()).hexdigest()
476-
return os.path.join(model_dir, filename)
500+
return os.path.join(self.model_dir, filename)
477501

478502
def test_download_and_save_to_cache(self, mock_hashlib_sha256, mock_requests_get):
479503
_tokenizer_loading._load_model_proto_bytes(_TOKENIZER_NAME)

0 commit comments

Comments
 (0)