code: def test_evaluate_if_necessary(self):
      outdir = tempfile.mkdtemp()
  
      agent = mock.Mock()
      agent.act.return_value = "action"
      agent.get_statistics.return_value = []
  
      env = mock.Mock()
      env.reset.return_value = "obs"
      env.step.return_value = ("obs", 0, True, {})
  
      agent_evaluator = evaluator.AsyncEvaluator(
          n_steps=None,
          n_episodes=self.n_episodes,
          eval_interval=3,
          outdir=outdir,
          max_episode_len=None,
          step_offset=0,
          save_best_so_far_agent=self.save_best_so_far_agent,
      )
  
      agent_evaluator.evaluate_if_necessary(
          t=1, episodes=1, env=env, agent=agent
      )
      self.assertEqual(agent.act.call_count, 0)
  
      agent_evaluator.evaluate_if_necessary(
          t=2, episodes=2, env=env, agent=agent
      )
      self.assertEqual(agent.act.call_count, 0)
  
      # First evaluation
      agent_evaluator.evaluate_if_necessary(
          t=3, episodes=3, env=env, agent=agent
      )
      self.assertEqual(agent.act.call_count, self.n_episodes)
      self.assertEqual(agent.stop_episode.call_count, self.n_episodes)
      if self.save_best_so_far_agent:
          self.assertEqual(agent.save.call_count, 1)
      else:
          self.assertEqual(agent.save.call_count, 0)
  
      # Second evaluation with the same score
      agent_evaluator.evaluate_if_necessary(
          t=6, episodes=6, env=env, agent=agent
      )
      self.assertEqual(agent.act.call_count, 2 * self.n_episodes)
      self.assertEqual(
          agent.stop_episode.call_count, 2 * self.n_episodes
      )
      if self.save_best_so_far_agent:
          self.assertEqual(agent.save.call_count, 1)
      else:
          self.assertEqual(agent.save.call_count, 0)
  
      # Third evaluation with a better score
      env.step.return_value = ("obs", 1, True, {})
      agent_evaluator.evaluate_if_necessary(
          t=9, episodes=9, env=env, agent=agent
      )
      self.assertEqual(agent.act.call_count, 3 * self.n_episodes)
      self.assertEqual(
          agent.stop_episode.call_count, 3 * self.n_episodes
      )
      if self.save_best_so_far_agent:
          self.assertEqual(agent.save.call_count, 2)
      else:
          self.assertEqual(agent.save.call_count, 0)
  
imports: import tempfile
  import unittest
  from unittest import mock
  from chainer import testing
  import chainerrl
  from chainerrl.experiments import evaluator
label_desc: Use image node_id 1 for calling the TestAsyncEvaluator obj's underlying member method code with example usage: obj.test_evaluate_if_necessary() without return types