userver: kafka::utest::KafkaCluster Class Reference
Loading...
Searching...
No Matches
kafka::utest::KafkaCluster Class Reference

#include <userver/kafka/utest/kafka_fixture.hpp>

Detailed Description

Helper for Kafka unit testing.

KafkaCluster is useful for

  • inline producer, consumer and their configuration creation;
  • generation of unique topic names;
  • sending and receiving batched of messages in sync form;

KafkaCluster expects that topic names used in test are already exist in Kafka broker or auto-created on produce to it.

It is recommended to create all topics before tests started, because topic creation leads to disk IO operation and maybe too slow in tests' runtime.

If one run tests with Kafka testsuite plugin, use TESTSUITE_KAFKA_CUSTOM_TOPICS environment variable to create topics before tests started.

Note
KafkaCluster determines the broker's connection string based on TESTSUITE_KAFKA_SERVER_HOST (by default localhost) and TESTSUITE_KAFKA_SERVER_PORT (by default 9099) environment variables.
See also
https://yandex.github.io/yandex-taxi-testsuite/kafka
Examples
samples/kafka_service/unittest/kafka_test.cpp.

Definition at line 56 of file kafka_fixture.hpp.

+ Inheritance diagram for kafka::utest::KafkaCluster:

Public Member Functions

std::string GenerateTopic ()
 
std::vector< std::string > GenerateTopics (std::size_t count)
 
impl::Configuration MakeProducerConfiguration (const std::string &name, impl::ProducerConfiguration configuration={}, impl::Secret secrets={})
 
impl::Configuration MakeConsumerConfiguration (const std::string &name, impl::ConsumerConfiguration configuration={}, impl::Secret secrets={})
 
Producer MakeProducer (const std::string &name, impl::ProducerConfiguration configuration={})
 
std::deque< ProducerMakeProducers (std::size_t count, std::function< std::string(std::size_t)> nameGenerator, impl::ProducerConfiguration configuration={})
 
void SendMessages (utils::span< const Message > messages)
 Creates temporary producer and send messages span.
 
impl::Consumer MakeConsumer (const std::string &name, const std::vector< std::string > &topics, impl::ConsumerConfiguration configuration={}, impl::ConsumerExecutionParams params={})
 
std::vector< MessageReceiveMessages (impl::Consumer &consumer, std::size_t expected_messages_count, bool commit_after_receive=true, std::optional< std::function< void(MessageBatchView)> > user_callback={})
 Starts consumer, waits until expected_message_count messages are consumed, calls user_callback if set, stops consumer.
 

Static Public Attributes

static constexpr const std::chrono::milliseconds kDefaultTestProducerTimeout
 

Member Data Documentation

◆ kDefaultTestProducerTimeout

constexpr const std::chrono::milliseconds kafka::utest::KafkaCluster::kDefaultTestProducerTimeout
staticconstexpr
Initial value:

Kafka broker has some cold start issues on its. To stabilize tests KafkaCluster patches producer's default delivery timeout. To use custom delivery timeout in test, pass configuration argument to MakeProducer.

Definition at line 62 of file kafka_fixture.hpp.


The documentation for this class was generated from the following file: