code: def test_sample(self):
      config = (
          AlgorithmConfig().environment("CartPole-v1")
          # Vectorize x2 and by default, rollout 64 timesteps per individual env.
          .rollouts(num_envs_per_worker=2, rollout_fragment_length=64)
      )
      env_runner = SingleAgentEnvRunner(config=config)
  
      # Expect error if both num_timesteps and num_episodes given.
      self.assertRaises(
          AssertionError,
          lambda: env_runner.sample(
              num_timesteps=10, num_episodes=10, random_actions=True
          ),
      )
  
      # Sample 10 episodes (5 per env) 100 times.
      for _ in range(100):
          episodes = env_runner.sample(
              num_episodes=10, random_actions=True
          )
          self.assertTrue(len(episodes) == 10)
          # Since we sampled complete episodes, there should be no ongoing episodes
          # being returned.
          self.assertTrue(all(e.is_done for e in episodes))
  
      # Sample 10 timesteps (5 per env) 100 times.
      for _ in range(100):
          episodes = env_runner.sample(
              num_timesteps=10, random_actions=True
          )
          # Check, whether the sum of lengths of all episodes returned is 20
          self.assertTrue(sum(len(e) for e in episodes) == 10)
  
      # Sample (by default setting: rollout_fragment_length=64) 10 times.
      for _ in range(100):
          episodes = env_runner.sample(random_actions=True)
          # Check, whether the sum of lengths of all episodes returned is 128
          # 2 (num_env_per_worker) * 64 (rollout_fragment_length).
          self.assertTrue(sum(len(e) for e in episodes) == 128)
  
file_path: ray/rllib/env/tests/test_single_agent_env_runner.py
imports: import unittest
  import ray
  from ray.rllib.algorithms.algorithm_config import AlgorithmConfig
  from ray.rllib.env.single_agent_env_runner import SingleAgentEnvRunner
label_desc: Use image node_id 3 for calling the TestSingleAgentEnvRunner obj's underlying member method code with example usage: obj.test_sample() without return types