code: def test_attach_on_custom_event():
      n_epochs = 10
      data = list(range(150))
  
      def _test(event, n_calls, cpe):
          losses = torch.rand(n_epochs * len(data))
          losses_iter = iter(losses)
  
          def update_fn(engine, batch):
              return next(losses_iter)
  
          trainer = Engine(update_fn)
          cpe.attach(trainer)
  
          logger = DummyLogger()
  
          mock_log_handler = MagicMock()
  
          logger.attach(
              trainer, log_handler=mock_log_handler, event_name=event
          )
  
          trainer.run(data, max_epochs=n_epochs)
  
          mock_log_handler.assert_called_with(trainer, logger, event)
          assert mock_log_handler.call_count == n_calls
  
imports: from typing import Any, Union
  from unittest.mock import call, MagicMock
  import pytest
  import torch
  from ignite.contrib.handlers.base_logger import BaseLogger, BaseOptimizerParamsHandler, BaseOutputHandler, BaseWeightsHandler, BaseWeightsScalarHandler
  from ignite.engine import Engine, Events, EventsList, State
  from tests.ignite.contrib.handlers import MockFP16DeepSpeedZeroOptimizer
label_desc: Use image node_id 7 for calling a global function with example usage: test_attach_on_custom_event() and returns: next