code: def test_retry_failed_trial_callback_repetitive_failure(
      storage_mode: str,
  ) -> None:
      heartbeat_interval = 1
      grace_period = 2
      max_retry = 3
      n_trials = 5
  
      with StorageSupplier(
          storage_mode,
          heartbeat_interval=heartbeat_interval,
          grace_period=grace_period,
          failed_trial_callback=RetryFailedTrialCallback(
              max_retry=max_retry
          ),
      ) as storage:
          assert is_heartbeat_enabled(storage)
          assert isinstance(storage, BaseHeartbeat)
  
          study = optuna.create_study(storage=storage)
  
          # Make repeatedly failed and retried trials by heartbeat.
          for _ in range(n_trials):
              with pytest.warns(UserWarning):
                  trial = study.ask()
              storage.record_heartbeat(trial._trial_id)
              time.sleep(grace_period + 1)
              optuna.storages.fail_stale_trials(study)
  
          trials = study.trials
  
          assert len(trials) == n_trials + 1
  
          assert "failed_trial" not in trials[0].system_attrs
          assert "retry_history" not in trials[0].system_attrs
  
          # The trials 1-3 are retried ones originating from the trial 0.
          assert trials[1].system_attrs["failed_trial"] == 0
          assert trials[1].system_attrs["retry_history"] == [0]
  
          assert trials[2].system_attrs["failed_trial"] == 0
          assert trials[2].system_attrs["retry_history"] == [0, 1]
  
          assert trials[3].system_attrs["failed_trial"] == 0
          assert trials[3].system_attrs["retry_history"] == [0, 1, 2]
  
          # Trials 4 and later are the newly started ones and
          # they are retried after exceeding max_retry.
          assert "failed_trial" not in trials[4].system_attrs
          assert "retry_history" not in trials[4].system_attrs
          assert trials[5].system_attrs["failed_trial"] == 4
          assert trials[5].system_attrs["retry_history"] == [4]
  
imports: import itertools
  import multiprocessing
  import time
  from typing import Any
  from typing import Optional
  from unittest.mock import Mock
  from unittest.mock import patch
  import warnings
  import pytest
  import optuna
  from optuna import Study
  from optuna._callbacks import RetryFailedTrialCallback
  from optuna.storages import RDBStorage
  from optuna.storages._heartbeat import BaseHeartbeat
  from optuna.storages._heartbeat import is_heartbeat_enabled
  from optuna.testing.storages import STORAGE_MODES_HEARTBEAT
  from optuna.testing.storages import StorageSupplier
  from optuna.testing.threading import _TestableThread
  from optuna.trial import FrozenTrial
  from optuna.trial import TrialState
label_desc: Use image node_id 10 for calling a global function with example usage: test_retry_failed_trial_callback_repetitive_failure(storage_mode) without return types