code: def test_ipcluster_start_stop(request, ipython_dir, daemonize):
      default_profile = ProfileDir.find_profile_dir_by_name(ipython_dir)
      default_profile_dir = default_profile.location
  
      # cleanup the security directory to avoid leaking files from one test to the next
      def cleanup_security():
          for f in glob.glob(
              os.path.join(default_profile.security_dir, "*.json")
          ):
              print(f"Cleaning up {f}")
              try:
                  os.remove(f)
              except Exception as e:
                  print(f"Error removing {f}: {e}")
  
      request.addfinalizer(cleanup_security)
  
      n = 2
      start_args = ["-n", str(n)]
      if daemonize:
          start_args.append("--daemonize")
      start = Popen(
          [
              sys.executable,
              "-m",
              "ipyparallel.cluster",
              "start",
              "--debug",
          ]
          + start_args
      )
      request.addfinalizer(start.terminate)
      if daemonize:
          # if daemonize, should exit after starting
          start.wait(30)
      else:
          # wait for file to appear
          # only need this if not daemonize
          cluster_file = ipp.Cluster(
              profile_dir=default_profile_dir, cluster_id=""
          ).cluster_file
          for i in range(100):
              if (
                  os.path.isfile(cluster_file)
                  or start.poll() is not None
              ):
                  break
              else:
                  time.sleep(0.1)
          assert os.path.isfile(cluster_file)
  
      # list should show a file
      out = ipcluster_list()
      assert len(out.splitlines()) == 2
  
      # cluster running, try to connect with default args
      cluster = ipp.Cluster.from_file(log_level=10)
  
      try:
          with cluster.connect_client_sync() as rc:
              rc.wait_for_engines(n=2, timeout=60)
              rc[:].apply_async(os.getpid).get(timeout=10)
      except Exception:
          print("controller output")
          print(cluster.controller.get_output())
          print("engine output")
          for engine_set in cluster.engines.values():
              print(engine_set.get_output())
          raise
  
      # stop with ipcluster stop
      check_call([sys.executable, "-m", "ipyparallel.cluster", "stop"])
      # start should exit when this happens
      start.wait(30)
  
      # and ipcluster list should return empty
      out = ipcluster_list()
      assert len(out.splitlines()) == 1
  
      # stop all succeeds even if there's nothing to stop
      check_call(
          [sys.executable, "-m", "ipyparallel.cluster", "stop", "--all"]
      )
  
imports: import glob
  import json
  import os
  import sys
  import time
  import types
  from subprocess import Popen, check_call, check_output
  from unittest.mock import MagicMock, create_autospec, patch
  import pytest
  import zmq
  from ipykernel import iostream, kernelapp
  from ipykernel.ipkernel import IPythonKernel
  from IPython.core.profiledir import ProfileDir
  from tornado import ioloop
  from zmq.eventloop import zmqstream
  import ipyparallel
  import ipyparallel.engine.app
label_desc: Use image node_id 9 for calling a global function with example usage: test_ipcluster_start_stop(request, ipython_dir, daemonize) without return types