code: def test_kill_cancel_metadata(ray_start_regular):
      """
      Verifies that client worker's terminate_actor and terminate_task methods
      pass worker's metadata attribute server to the grpc stub's Terminate
      method.
  
      This is done by mocking the grpc stub's Terminate method to raise an
      exception with argument equal to the key of the metadata. We then verify
      that the exception is raised when calling ray.kill and ray.cancel.
      """
      with ray_start_client_server(metadata=[("key", "value")]) as ray:
  
          @ray.remote
          class A:
              pass
  
          @ray.remote
          def f():
              time.sleep(1000)
  
          class MetadataIsCorrectlyPassedException(Exception):
              pass
  
          def mock_terminate(self, term):
              raise MetadataIsCorrectlyPassedException(
                  self._metadata[1][0]
              )
  
          # Mock stub's Terminate method to raise an exception.
          stub = ray.get_context().api.worker.data_client
          stub.Terminate = mock_terminate.__get__(stub)
  
          # Verify the expected exception is raised with ray.kill.
          # Check that argument of the exception matches "key" from the
          # metadata above.
          actor = A.remote()
          with pytest.raises(
              MetadataIsCorrectlyPassedException, match="key"
          ):
              ray.kill(actor)
  
          # Verify the expected exception is raised with ray.cancel.
          task_ref = f.remote()
          with pytest.raises(
              MetadataIsCorrectlyPassedException, match="key"
          ):
              ray.cancel(task_ref)
  
file_path: ray/python/ray/tests/test_client_terminate.py
imports: import sys
  import time
  import pytest
  from ray._private.test_utils import convert_actor_state, wait_for_condition
  from ray.exceptions import GetTimeoutError, ObjectLostError, RayTaskError, TaskCancelledError, WorkerCrashedError
  from ray.tests.client_test_utils import create_remote_signal_actor
  from ray.util.client.ray_client_helpers import ray_start_client_server
label_desc: Use image node_id 5 for calling a global function with example usage: test_kill_cancel_metadata(ray_start_regular) without return types