def test_alter_topic_config_works(topic_controller: TopicController, topic_id: str): initial_topic = Topic(topic_id, config={"cleanup.policy": "delete"}) topic_controller.create_topics([initial_topic]) topic_controller.update_from_cluster(initial_topic) config = initial_topic.config assert config.get("cleanup.policy") == "delete" change_topic = Topic(topic_id, config={"cleanup.policy": "compact"}) topic_controller.alter_configs([change_topic]) topic_controller.update_from_cluster(change_topic) after_changes_applied_topic = topic_controller.get_cluster_topic(topic_id) final_config = after_changes_applied_topic.config assert final_config.get("cleanup.policy") == "compact"
def ping(state, times, wait): topic_controller = state.cluster.topic_controller deltas = [] try: try: topic_controller.create_topics( [topic_controller.get_cluster_topic(PING_TOPIC)]) except TopicAlreadyExistsException: click.echo("Topic already exists.") producer = PingProducer() consumer = PingConsumer(PING_GROUP_ID, PING_TOPIC, True) click.echo(f"Ping with {state.cluster.bootstrap_servers}") for i in range(times): producer.produce(PING_TOPIC) _, delta = consumer.consume() deltas.append(delta) click.echo(f"m_seq={i} time={delta:.2f}ms") sleep(wait) except KeyboardInterrupt: pass finally: topic_controller.delete_topic(Topic(PING_TOPIC)) click.echo("--- statistics ---") click.echo(f"{len(deltas)} messages sent/received") click.echo( f"min/avg/max = {min(deltas):.2f}/{(sum(deltas) / len(deltas)):.2f}/{max(deltas):.2f} ms" )
def delete_topic(state: State, topic_name: str): topic_controller = state.cluster.topic_controller if ensure_approval("Are you sure?", no_verify=state.no_verify): topic_controller.delete_topic(Topic(topic_name)) assert topic_name not in (t.name for t in topic_controller.list_topics())
def create_topic(state: State, topic_name: str): if not ensure_approval("Are you sure?", no_verify=state.no_verify): click.echo("Aborted") return topic_controller = state.cluster.topic_controller topic_controller.create_topics([Topic(topic_name)])
def test_topic_creation_raises_for_wrong_config( topic_controller: TopicController, confluent_admin_client: confluent_kafka.admin.AdminClient, topic_id: str): topics = confluent_admin_client.list_topics(timeout=5).topics.keys() assert topic_id not in topics # We only have 1 broker for tests, so a higher replication should fail with pytest.raises(KafkaException): topic_controller.create_topics([Topic(topic_id, replication_factor=2)])
def test_topic_deletion_works( topic_controller: TopicController, confluent_admin_client: confluent_kafka.admin.AdminClient, topic: str): topics = confluent_admin_client.list_topics(timeout=5).topics.keys() assert topic in topics topic_controller.delete_topic(Topic(topic)) # Invalidate cache confluent_admin_client.poll(timeout=1) topics = confluent_admin_client.list_topics(timeout=5).topics.keys() assert topic not in topics
def test_topic_creation_works( topic_controller: TopicController, confluent_admin_client: confluent_kafka.admin.AdminClient, topic_id: str): topics = confluent_admin_client.list_topics(timeout=5).topics.keys() assert topic_id not in topics topic_controller.create_topics([Topic(topic_id, replication_factor=1)]) # invalidate cache confluent_admin_client.poll(timeout=1) topics = confluent_admin_client.list_topics(timeout=5).topics.keys() assert topic_id in topics
def changed_topic_object(cluster, topic): yield Topic(topic, 1, 3, {"cleanup.policy": "compact"})
def get_cluster_topic(self, topic_name: str) -> Topic: """Convenience function getting an existing topic based on topic_name""" return self.update_from_cluster(Topic(topic_name))