12
12
from mlagents .trainers .env_manager import EnvironmentStep
13
13
from mlagents_envs .base_env import BaseEnv
14
14
from mlagents_envs .side_channel .engine_configuration_channel import EngineConfig
15
+ from mlagents_envs .exception import UnityEnvironmentException
15
16
from mlagents .trainers .tests .simple_test_envs import SimpleEnvironment
16
17
from mlagents .trainers .stats import StatsReporter
17
18
from mlagents .trainers .tests .test_simple_rl import (
@@ -168,13 +169,12 @@ def test_advance(self, mock_create_worker, external_brains_mock, step_mock):
168
169
assert agent_manager_mock .policy == mock_policy
169
170
170
171
171
- def simple_env_factory (worker_id , config ):
172
- env = SimpleEnvironment (["1D" ], use_discrete = True )
173
- return env
174
-
175
-
176
172
@pytest .mark .parametrize ("num_envs" , [1 , 4 ])
177
173
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
+
178
178
env_manager = SubprocessEnvManager (
179
179
simple_env_factory , EngineConfig .default_config (), num_envs
180
180
)
@@ -193,3 +193,22 @@ def test_subprocess_env_endtoend(num_envs):
193
193
val > 0.7 for val in StatsReporter .writers [0 ].get_last_rewards ().values ()
194
194
)
195
195
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