Skip to content

Commit 4ba139e

Browse files
authored
[core] Deflake test_runtime_env_pip_and_conda_4.py (#52750)
Test was [timing out](https://buildkite.com/ray-project/postmerge/builds/9892#019691bf-f352-4fbf-a92c-ff277cf7a901/176-1944) sometimes -- let's make it faster. Updated the slowest test condition to avoid restarting ray each time, which allows the runtime_env cache to be hit and not have to install the env 3 times. Before: ```bash ================= 8 passed, 1 skipped in 96.56s (0:01:36) ================== ``` After: ```bash ================= 8 passed, 1 skipped in 62.14s (0:01:02) ================== ``` --------- Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
1 parent ffedb2c commit 4ba139e

File tree

2 files changed

+54
-100
lines changed

2 files changed

+54
-100
lines changed

python/ray/tests/test_runtime_env_conda_and_pip.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pytest
33
import sys
44
import platform
5-
import time
65
from ray._private.test_utils import (
76
wait_for_condition,
87
chdir,
@@ -16,6 +15,7 @@
1615
MAX_INTERNAL_PIP_FILENAME_TRIES,
1716
)
1817
from ray.runtime_env import RuntimeEnv
18+
from ray.util.state import list_tasks
1919

2020
import yaml
2121
import tempfile
@@ -116,7 +116,7 @@ class TestGC:
116116
reason="Needs PR wheels built in CI, so only run on linux CI machines.",
117117
)
118118
@pytest.mark.parametrize("field", ["conda", "pip"])
119-
@pytest.mark.parametrize("spec_format", ["file", "python_object"])
119+
@pytest.mark.parametrize("spec_format", ["python_object"])
120120
def test_job_level_gc(
121121
self, runtime_env_disable_URI_cache, start_cluster, field, spec_format, tmp_path
122122
):
@@ -139,10 +139,12 @@ def f():
139139

140140
# Ensure that the runtime env has been installed.
141141
assert ray.get(f.remote())
142-
# Sleep some seconds before checking that we didn't GC. Otherwise this
143-
# check may spuriously pass.
144-
time.sleep(2)
145-
assert not check_local_files_gced(cluster)
142+
143+
# Check that after the task is finished, the runtime_env is not GC'd
144+
# because the job is still alive.
145+
wait_for_condition(lambda: list_tasks()[0].state == "FINISHED")
146+
for _ in range(5):
147+
assert not check_local_files_gced(cluster)
146148

147149
ray.shutdown()
148150

@@ -163,7 +165,7 @@ def f():
163165
reason="Requires PR wheels built in CI, so only run on linux CI machines.",
164166
)
165167
@pytest.mark.parametrize("field", ["conda", "pip"])
166-
@pytest.mark.parametrize("spec_format", ["file", "python_object"])
168+
@pytest.mark.parametrize("spec_format", ["python_object"])
167169
def test_detached_actor_gc(
168170
self, runtime_env_disable_URI_cache, start_cluster, field, spec_format, tmp_path
169171
):

python/ray/tests/test_runtime_env_conda_and_pip_4.py

+45-93
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@
1212
os.environ["RAY_RUNTIME_ENV_LOCAL_DEV_MODE"] = "1"
1313

1414

15-
def test_in_virtualenv(start_cluster):
15+
def test_in_virtualenv(ray_start_regular_shared):
1616
assert (
1717
virtualenv_utils.is_in_virtualenv() is False
1818
and "IN_VIRTUALENV" not in os.environ
1919
) or (virtualenv_utils.is_in_virtualenv() is True and "IN_VIRTUALENV" in os.environ)
20-
cluster, address = start_cluster
21-
runtime_env = {"pip": ["pip-install-test==0.5"]}
2220

23-
ray.init(address, runtime_env=runtime_env)
24-
25-
@ray.remote
21+
@ray.remote(runtime_env={"pip": ["pip-install-test==0.5"]})
2622
def f():
2723
import pip_install_test # noqa: F401
2824

@@ -33,102 +29,58 @@ def f():
3329
assert ray.get(f.remote())
3430

3531

36-
def test_multiple_pip_installs(start_cluster, monkeypatch):
32+
@pytest.mark.skipif(
33+
sys.platform == "win32", reason="python.exe in use during deletion."
34+
)
35+
def test_multiple_pip_installs(ray_start_regular_shared):
3736
"""Test that multiple pip installs don't interfere with each other."""
38-
monkeypatch.setenv("RUNTIME_ENV_RETRY_TIMES", "0")
39-
cluster, address = start_cluster
40-
41-
if sys.platform == "win32" and "ray" not in address:
42-
pytest.skip(
43-
"Failing on windows, as python.exe is in use during deletion attempt."
44-
)
45-
46-
ray.init(
47-
address,
48-
runtime_env={
49-
"pip": ["pip-install-test"],
50-
"env_vars": {"TEST_VAR_1": "test_1"},
51-
},
52-
)
5337

5438
@ray.remote
5539
def f():
56-
return True
57-
58-
@ray.remote(
59-
runtime_env={
60-
"pip": ["pip-install-test"],
61-
"env_vars": {"TEST_VAR_2": "test_2"},
62-
}
63-
)
64-
def f2():
65-
return True
40+
return os.environ["TEST_VAR"]
41+
42+
assert ray.get(
43+
[
44+
f.options(
45+
runtime_env={
46+
"pip": ["pip-install-test"],
47+
"env_vars": {"TEST_VAR": "1"},
48+
}
49+
).remote(),
50+
f.options(
51+
runtime_env={
52+
"pip": ["pip-install-test"],
53+
"env_vars": {"TEST_VAR": "2"},
54+
}
55+
).remote(),
56+
]
57+
) == ["1", "2"]
6658

67-
@ray.remote(
68-
runtime_env={
69-
"pip": ["pip-install-test"],
70-
"env_vars": {"TEST_VAR_3": "test_3"},
71-
}
72-
)
73-
def f3():
74-
return True
7559

76-
assert all(ray.get([f.remote(), f2.remote(), f3.remote()]))
60+
@pytest.mark.skipif(
61+
os.environ.get("CI") and sys.platform != "linux",
62+
reason="Requires PR wheels built in CI, so only run on linux CI machines.",
63+
)
64+
def test_pip_ray_is_overwritten(ray_start_regular_shared):
65+
@ray.remote
66+
def f():
67+
import pip_install_test # noqa: F401
7768

69+
# Test an unconstrained "ray" dependency (should work).
70+
ray.get(f.options(runtime_env={"pip": ["pip-install-test==0.5", "ray"]}).remote())
7871

79-
class TestGC:
80-
@pytest.mark.skipif(
81-
os.environ.get("CI") and sys.platform != "linux",
82-
reason="Requires PR wheels built in CI, so only run on linux CI machines.",
72+
# Test a constrained "ray" dependency that matches the env (should work).
73+
ray.get(
74+
f.options(runtime_env={"pip": ["pip-install-test==0.5", "ray>=2.0"]}).remote()
8375
)
84-
@pytest.mark.parametrize("field", ["pip"])
85-
def test_pip_ray_is_overwritten(self, start_cluster, field):
86-
cluster, address = start_cluster
87-
88-
# It should be OK to install packages with ray dependency.
89-
ray.init(address, runtime_env={"pip": ["pip-install-test==0.5", "ray"]})
90-
91-
@ray.remote
92-
def f():
93-
import pip_install_test # noqa: F401
94-
95-
return True
96-
97-
# Ensure that the runtime env has been installed.
98-
assert ray.get(f.remote())
99-
100-
ray.shutdown()
101-
102-
# It should be OK if cluster ray meets the installing ray version.
103-
ray.init(address, runtime_env={"pip": ["pip-install-test==0.5", "ray>=1.12.0"]})
10476

105-
@ray.remote
106-
def f():
107-
import pip_install_test # noqa: F401
108-
109-
return True
110-
111-
# Ensure that the runtime env has been installed.
112-
assert ray.get(f.remote())
113-
114-
ray.shutdown()
115-
116-
# It will raise exceptions if ray is overwritten.
117-
with pytest.raises(Exception):
118-
ray.init(
119-
address, runtime_env={"pip": ["pip-install-test==0.5", "ray<=1.6.0"]}
120-
)
121-
122-
@ray.remote
123-
def f():
124-
import pip_install_test # noqa: F401
125-
126-
return True
127-
128-
# Ensure that the runtime env has been installed.
129-
assert ray.get(f.remote())
130-
131-
ray.shutdown()
77+
# Test a constrained "ray" dependency that doesn't match the env (shouldn't work).
78+
with pytest.raises(Exception):
79+
ray.get(
80+
f.options(
81+
runtime_env={"pip": ["pip-install-test==0.5", "ray<2.0"]}
82+
).remote()
83+
)
13284

13385

13486
# pytest-virtualenv doesn't support Python 3.12 as of now, see more details here:
@@ -158,7 +110,7 @@ def test_run_in_virtualenv(cloned_virtualenv):
158110
@pytest.mark.skipif(
159111
"IN_VIRTUALENV" in os.environ, reason="Pip option not supported in virtual env."
160112
)
161-
def test_runtime_env_with_pip_config(start_cluster):
113+
def test_runtime_env_with_pip_config(ray_start_regular_shared):
162114
@ray.remote(
163115
runtime_env={
164116
"pip": {"packages": ["pip-install-test==0.5"], "pip_version": "==24.1.2"}

0 commit comments

Comments
 (0)