code: def test_failure_handler_rerun(named_model_space):
      name, model_space = named_model_space
      engine = SequentialExecutionEngine(
          max_model_count=30, continue_on_failure=True
      )
  
      def wrap(fn):
          def wrapper(*args, **kwargs):
              fn(*args, **kwargs)
              if random.random() < 0.5:
                  raise RuntimeError("failed")
  
          return wrapper
  
      model_space.evaluator.function = wrap(
          model_space.evaluator.function
      )
  
      mock_strategy = MockStrategy(auto_check=False)
      strategy = Chain(
          mock_strategy,
          FailureHandler(retry_patience=100, retain_history=True),
      )
      strategy(model_space, engine)
      assert len(list(engine.list_models())) > len(
          list(strategy.list_models(sort=False))
      )
  
      any_failed = False
      failure_status_count = 0
      for model in strategy.list_models(sort=False):
          if len(mock_strategy._check_final_events[model]) == 1:
              assert (
                  model.metrics.intermediates
                  == mock_strategy._check_intermediate_events[model]
              )
              assert [
                  model.metrics.final
              ] == mock_strategy._check_final_events[model]
              assert [
                  model.status
              ] == mock_strategy._check_status_events[model]
          else:
              retried = len(mock_strategy._check_final_events[model])
              any_failed = True
              assert (
                  model.metrics.intermediates * retried
                  == mock_strategy._check_intermediate_events[model]
              )
              assert [
                  model.metrics.final
              ] * retried == mock_strategy._check_final_events[model]
              assert [
                  model.status
              ] == mock_strategy._check_status_events[
                  model
              ]  # only one status
              if model.status != ModelStatus.Trained:
                  failure_status_count += 1
  
      assert any_failed
      assert failure_status_count <= 1
  
imports: import random
  from collections import defaultdict, Counter
  import pytest
  from nni.mutable import SampleValidationError
  from nni.nas.execution import SequentialExecutionEngine
  from nni.nas.space import ModelStatus
  from nni.nas.strategy import Random, RegularizedEvolution, PolicyBasedRL, GridSearch, Strategy
  from nni.nas.strategy.base import StrategyStatus
  from nni.nas.strategy.middleware import Filter, Chain, Deduplication, FailureHandler, MultipleEvaluation
  from .test_sanity import named_model_space, engine, naive_policy
label_desc: Use image node_id 7 for calling a global function with example usage: test_failure_handler_rerun(named_model_space) and returns: wrapper