code: def test_completed_next_ready_is_finished(shutdown_only):
      @ray.remote
      def f():
          for _ in range(3):
              time.sleep(1)
              yield 1
  
      gen = f.remote()
      assert not gen.is_finished()
      assert not gen.next_ready()
      r, _ = ray.wait([gen])
      gen = r[0]
      assert gen.next_ready()
      _, ur = ray.wait([gen.completed()], timeout=0)
      assert len(ur) == 1
  
      # Consume object refs
      next(gen)
      assert not gen.is_finished()
      _, ur = ray.wait([gen.completed()], timeout=0)
      assert len(ur) == 1
  
      next(gen)
      assert not gen.is_finished()
      _, ur = ray.wait([gen.completed()], timeout=0)
      assert len(ur) == 1
  
      next(gen)
      with pytest.raises(StopIteration):
          next(gen)
  
      assert gen.is_finished()
      # Since the next should raise StopIteration,
      # it should be False.
      assert not gen.next_ready()
      r, _ = ray.wait([gen.completed()], timeout=0)
      assert len(r) == 1
  
      # Test the failed case.
      gen = f.remote()
      next(gen)
      ray.cancel(gen, force=True)
      r, _ = ray.wait([gen])
      assert len(r) == 1
      # The last exception is not taken yet.
      assert gen.next_ready()
      assert not gen.is_finished()
      with pytest.raises(WorkerCrashedError):
          ray.get(gen.completed())
      with pytest.raises(WorkerCrashedError):
          ray.get(next(gen))
      assert not gen.next_ready()
      assert gen.is_finished()
  
imports: import asyncio
  import pytest
  import numpy
  import sys
  import time
  from collections import Counter
  from starlette.responses import StreamingResponse
  from starlette.requests import Request
  from fastapi import FastAPI
  from ray import serve
  from pydantic import BaseModel
  import ray
  from ray._raylet import ObjectRefGenerator
  from ray.exceptions import WorkerCrashedError
  from ray._private.test_utils import run_string_as_driver_nonblocking
  from ray.util.state import list_actors
label_desc: Use image node_id 5 for calling a global function with example usage: test_completed_next_ready_is_finished(shutdown_only) without return types