code: def test_as_context_manager(event, n_calls):
      n_epochs = 5
      data = list(range(50))
  
      class _DummyLogger(DummyLogger):
          def __init__(self, writer):
              self.writer = writer
  
          def close(self):
              self.writer.close()
  
      global close_counter
      close_counter = 0
  
      losses = torch.rand(n_epochs * len(data))
      losses_iter = iter(losses)
  
      def update_fn(engine, batch):
          return next(losses_iter)
  
      writer = MagicMock()
      writer.close = MagicMock()
  
      with _DummyLogger(writer) as logger:
          assert isinstance(logger, _DummyLogger)
  
          trainer = Engine(update_fn)
          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
  
      writer.close.assert_called_once_with()
  
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 8 for calling a global function with example usage: test_as_context_manager(event, n_calls) and returns: next