code: def test_kafka_batch():
      j = random.randint(0, 10000)
      ARGS = {
          "bootstrap.servers": "localhost:9092",
          "group.id": "streamz-test%i" % j,
          "auto.offset.reset": "latest",
      }
      with kafka_service() as kafka:
          kafka, TOPIC = kafka
          # These messages aren't read since Stream starts reading from latest offsets
          for i in range(10):
              kafka.produce(TOPIC, b"value-%d" % i, b"%d" % i)
          kafka.flush()
          stream = Stream.from_kafka_batched(
              TOPIC, ARGS, max_batch_size=4, keys=True
          )
          out = stream.sink_to_list()
          stream.start()
          wait_for(lambda: stream.upstream.started, 10, 0.1)
          for i in range(10):
              kafka.produce(TOPIC, b"value-%d" % i, b"%d" % i)
          kafka.flush()
          # out may still be empty or first item of out may be []
          wait_for(
              lambda: any(out) and out[-1][-1]["value"] == b"value-9",
              10,
              period=0.2,
          )
          assert out[-1][-1]["key"] == b"9"
          # max_batch_size checks
          assert len(out[0]) == len(out[1]) == 4 and len(out) == 3
          stream.upstream.stopped = True
  
imports: import asyncio
  import atexit
  from contextlib import contextmanager
  from flaky import flaky
  import os
  import pytest
  import random
  import shlex
  import subprocess
  import time
  from tornado import gen
  from ..core import Stream
  from ..dask import DaskStream
  from streamz.utils_test import gen_test, wait_for, await_for
  from distributed.utils_test import gen_cluster
label_desc: Use image node_id 8 for calling a global function with example usage: test_kafka_batch() without return types