|
3 | 3 | import time |
4 | 4 | import unittest |
5 | 5 | from datetime import datetime, timedelta |
6 | | -from typing import List |
| 6 | +from typing import List, Optional |
7 | 7 |
|
8 | 8 | import docker |
9 | 9 | import pytest |
@@ -59,6 +59,31 @@ def test_round_trip(self): |
59 | 59 | self.assertEqual(msg.topic(), topic) |
60 | 60 | self.assertEqual(msg.value(), b"can you hear me?") |
61 | 61 |
|
| 62 | + def test_message_with_key(self): |
| 63 | + """Try writing a message into the Kafka broker, and try pulling the same |
| 64 | + message back out. |
| 65 | +
|
| 66 | + """ |
| 67 | + topic = "test_message_with_key" |
| 68 | + # Push one message in... |
| 69 | + simple_write_msg(self.kafka, topic, "can you hear me?", key="test_msg") |
| 70 | + # ... and pull it back out. |
| 71 | + consumer = adc.consumer.Consumer(adc.consumer.ConsumerConfig( |
| 72 | + broker_urls=[self.kafka.address], |
| 73 | + group_id="test_consumer", |
| 74 | + auth=self.kafka.auth, |
| 75 | + )) |
| 76 | + consumer.subscribe(topic) |
| 77 | + stream = consumer.stream() |
| 78 | + |
| 79 | + msg = next(stream) |
| 80 | + if msg.error() is not None: |
| 81 | + raise Exception(msg.error()) |
| 82 | + |
| 83 | + self.assertEqual(msg.topic(), topic) |
| 84 | + self.assertEqual(msg.value(), b"can you hear me?") |
| 85 | + self.assertEqual(msg.key(), b"test_msg") |
| 86 | + |
62 | 87 | def test_reset_to_end(self): |
63 | 88 | # Write a few messages. |
64 | 89 | topic = "test_reset_to_end" |
@@ -569,13 +594,13 @@ def get_or_create_docker_network(self): |
569 | 594 | return self.docker_client.networks.create(name="adc-integration-test") |
570 | 595 |
|
571 | 596 |
|
572 | | -def simple_write_msg(conn: KafkaDockerConnection, topic: str, msg: str): |
| 597 | +def simple_write_msg(conn: KafkaDockerConnection, topic: str, msg: str, key: Optional[str] = None): |
573 | 598 | producer = adc.producer.Producer(adc.producer.ProducerConfig( |
574 | 599 | broker_urls=[conn.address], |
575 | 600 | topic=topic, |
576 | 601 | auth=conn.auth, |
577 | 602 | )) |
578 | | - producer.write(msg) |
| 603 | + producer.write(msg, key=key) |
579 | 604 | producer.flush() |
580 | 605 |
|
581 | 606 |
|
|
0 commit comments