code: def test_use_dynamic_function_and_class():
      # Test use dynamically defined functions
      # and classes for remote tasks and actors.
      # See https://github.com/ray-project/ray/issues/12834.
      ray.shutdown()
      current_path = os.path.dirname(__file__)
      job_config = ray.job_config.JobConfig(
          code_search_path=[current_path]
      )
      ray.init(job_config=job_config)
  
      def foo1():
          @ray.remote
          def foo2():
              return "OK"
  
          return foo2
  
      @ray.remote
      class Foo:
          @ray.method(num_returns=1)
          def foo(self):
              return "OK"
  
      f = foo1()
      assert ray.get(f.remote()) == "OK"
      # Check whether the dynamic function is exported to GCS.
      # Note, the key format should be kept
      # the same as in `FunctionActorManager.export`.
      key_func = (
          b"RemoteFunction:"
          + ray._private.worker.global_worker.current_job_id.hex().encode()
          + b":"
          + f._function_descriptor.function_id.binary()
      )
      assert ray._private.worker.global_worker.gcs_client.internal_kv_exists(
          key_func, KV_NAMESPACE_FUNCTION_TABLE
      )
      foo_actor = Foo.remote()
  
      assert ray.get(foo_actor.foo.remote()) == "OK"
      # Check whether the dynamic class is exported to GCS.
      # Note, the key format should be kept
      # the same as in `FunctionActorManager.export_actor_class`.
      key_cls = (
          b"ActorClass:"
          + ray._private.worker.global_worker.current_job_id.hex().encode()
          + b":"
          + foo_actor._ray_actor_creation_function_descriptor.function_id.binary()
      )
      assert ray._private.worker.global_worker.gcs_client.internal_kv_exists(
          key_cls, namespace=KV_NAMESPACE_FUNCTION_TABLE
      )
  
imports: import array
  import logging
  import os
  import random
  import subprocess
  import sys
  import tempfile
  import threading
  import time
  from unittest.mock import MagicMock, patch
  import pytest
  from ray._private.ray_constants import KV_NAMESPACE_FUNCTION_TABLE
  from ray._private.test_utils import client_test_enabled
  from ray.cluster_utils import Cluster, cluster_not_supported
  from ray.exceptions import GetTimeoutError, RayTaskError
  from ray.tests.client_test_utils import create_remote_signal_actor
label_desc: Use image node_id 23 for calling a global function with example usage: test_use_dynamic_function_and_class() and returns: foo2, str, str