code: def test_batched_close_timeout_exception():
      while True:
          try:
              # Expect a timeout at BatchedCommand.close() if the process runs
              # longer than timeout and the config for
              # "datalad.runtime.stalled-external" is "abandon".
              # In most cases the next commands until `bc.close()` will execute
              # faster than `timeout`. If not we just restart the process
              bc = BatchedCommand(
                  cmd=[sys.executable, "-i", "-u", "-q", "-"],
                  timeout=0.5,
                  exception_on_timeout=True,
              )
  
              # Send at least one instruction to start the subprocess
              response = bc("import time; print('a')")
              assert_equal(response, "a")
  
              # Send process to sleep for two seconds to trigger a timeout in
              # bc.close().
              bc.stdin_queue.put("time.sleep(2); exit(1)\n".encode())
              with unittest.mock.patch("datalad.cfg") as cfg_mock:
                  cfg_mock.obtain.return_value = "abandon"
                  try:
                      bc.close()
                      pytest.fail(
                          "bc.close() did not generate a timeout"
                      )
                  except TimeoutExpired:
                      return
          except TimeoutExpired:
              # Timeout occurred early, try again
              continue
  
file_path: datalad/datalad/tests/test_cmd.py
imports: import sys
  import unittest.mock
  from subprocess import TimeoutExpired
  import pytest
  from datalad.cmd import BatchedCommand, readline_rstripped
  from datalad.cmd import BatchedCommandError
  from datalad.runner.tests.utils import py2cmd
  from datalad.tests.utils_pytest import assert_equal, assert_is_none, assert_is_not_none, assert_not_equal, assert_true
label_desc: Use image node_id 4 for calling a global function with example usage: test_batched_close_timeout_exception() without return types