Skip to content

Commit 020c200

Browse files
author
Jonathan Harper
committed
Add tests for this behavior
1 parent a5ad242 commit 020c200

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

ml-agents/mlagents/trainers/tests/test_subprocess_env_manager.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from mlagents.trainers.env_manager import EnvironmentStep
1313
from mlagents_envs.base_env import BaseEnv
1414
from mlagents_envs.side_channel.engine_configuration_channel import EngineConfig
15+
from mlagents_envs.exception import UnityEnvironmentException
1516
from mlagents.trainers.tests.simple_test_envs import SimpleEnvironment
1617
from mlagents.trainers.stats import StatsReporter
1718
from mlagents.trainers.tests.test_simple_rl import (
@@ -168,13 +169,12 @@ def test_advance(self, mock_create_worker, external_brains_mock, step_mock):
168169
assert agent_manager_mock.policy == mock_policy
169170

170171

171-
def simple_env_factory(worker_id, config):
172-
env = SimpleEnvironment(["1D"], use_discrete=True)
173-
return env
174-
175-
176172
@pytest.mark.parametrize("num_envs", [1, 4])
177173
def test_subprocess_env_endtoend(num_envs):
174+
def simple_env_factory(worker_id, config):
175+
env = SimpleEnvironment(["1D"], use_discrete=True)
176+
return env
177+
178178
env_manager = SubprocessEnvManager(
179179
simple_env_factory, EngineConfig.default_config(), num_envs
180180
)
@@ -193,3 +193,22 @@ def test_subprocess_env_endtoend(num_envs):
193193
val > 0.7 for val in StatsReporter.writers[0].get_last_rewards().values()
194194
)
195195
env_manager.close()
196+
197+
198+
@pytest.mark.parametrize("num_envs", [1, 4])
199+
def test_subprocess_env_raises_errors(num_envs):
200+
def failing_env_factory(worker_id, config):
201+
import time
202+
203+
# Sleep momentarily to allow time for the EnvManager to be waiting for the
204+
# subprocess response. We won't be able to capture failures from the subprocess
205+
# that cause it to close the pipe before we can send the first message.
206+
time.sleep(0.1)
207+
raise UnityEnvironmentException()
208+
209+
env_manager = SubprocessEnvManager(
210+
failing_env_factory, EngineConfig.default_config(), num_envs
211+
)
212+
with pytest.raises(UnityEnvironmentException):
213+
env_manager.reset()
214+
env_manager.close()

0 commit comments

Comments
 (0)